summaryrefslogtreecommitdiff
path: root/src/texec/libtnsl.go
blob: bff8d489c8f95859d58bc24343184e875a2ea110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
	Copyright 2020 Kyle Gunger

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

package texec

import (
	"fmt"
	"os"
)

/**
	libtnsl module stub.  Contains only parts of the io sub-module.
	Parts included:
		- io.print
		- io.println
		- io.open_file
		- io.File API for file objects
	
	Types included:
		- tnsl.io.File
		- string ({}charp)
		- charp
		- int
		- float
		- null
*/

// I really hope this works.

// Generic in-built types
var (
	
	tFile = TType{Pre: []string{}, T: TArtifact{Path: []string{"tnsl", "io"}, Name: "File"}, Post: []string{}}
	tString = TType{Pre: []string{"{}"}, T: TArtifact{Path: []string{}, Name:"charp"}, Post: []string{}}
	tInt = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"int"}, Post: []string{}}
	tFloat = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"float"}, Post: []string{}}
	tCharp = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"charp"}, Post: []string{}}
	tNull = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name: "null"}, Post: []string{}}
)

// tells if the stub supports a function
func tnslResolve(callPath TArtifact) int {
	l := len(callPath.Path)
	if l < 2 || l > 3 || callPath.Path[0] != "tnsl" || callPath.Path[1] != "io" {
		return -1
	}
	if l > 2 && callPath.Path[2] != "File" {
		return -1
	}

	if l > 2 {
		if callPath.Name == "write" || callPath.Name == "read" || callPath.Name == "close" {
			return 1;
		}
	} else {
		if callPath.Name == "print" || callPath.Name == "println" || callPath.Name == "open_file" {
			return 0;
		}
	}

	return -1
}

// evaluate a function call.
// in is the variable in (if any)
// out is the variable out (if any)
// function is the name of the function
func tnslEval(in TVariable, function string) TVariable {
	switch function {
	case "print":
		tprint(in)
	case "println":
		tprintln(in)
	case "open_file":
		return topen_file(in)
	}
	return TVariable{tNull, nil}
}

// evaluate a call on a file object
func tnslFileEval(file, in TVariable, function string) TVariable {
	switch function {
	case "close":
		tfile_close(file)
	case "read":
		return tfile_read(file)
	case "write":
		tfile_write(file, in)
	}
	return TVariable{tNull, nil}
}

// Generic IO funcs

func tprint(in TVariable) {
	fmt.Printf("%v", in.Data)
}

func tprintln(in TVariable) {
	fmt.Printf("%v\n", in.Data)
}

func topen_file(in TVariable) TVariable {
	if equateType(in.Type, tString) {
		panic("Tried to open a file, but did not use a string type for the file name.")
	}
	fd, err := os.Create(in.Data.(string))
	if err != nil {
		panic(fmt.Sprintf("Failed to open file %v as requested by the program. Aborting.\n%v", in.Data, err))
	}
	return  TVariable{tFile, fd}
}


// File API

// tnsl.io.File.close
func tfile_close(file TVariable) {
	if equateType(file.Type, tFile) {
		(file.Data).(*os.File).Close()
	}
}

// tnsl.io.File.read
func tfile_read(file TVariable) TVariable {
	b := []byte{1}
	(file.Data).(*os.File).Read(b)
	return TVariable{tCharp, b[0]}
}

// tnsl.io.File.write
func tfile_write(file, in TVariable) {
	b := []byte{0}
	if equateType(file.Type, tFile) && (equateType(in.Type, tCharp) || equateType(in.Type, tInt)) {
		b[0] = (in.Data).(byte)
		(file.Data).(*os.File).Write(b)
	} else {
		(file.Data).(*os.File).Close()
		panic(fmt.Sprintf("Failed to write to file, attempted to use unsupported type (%v)\n", in.Type))
	}
}