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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/*
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.readFile
- io.writeFile
- 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: ""}
tString = TType{Pre: []string{"{}"}, T: TArtifact{Path: []string{}, Name:"uint8"}, Post: ""}
tInt = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"int"}, Post: ""}
tUint = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"uint"}, Post: ""}
tByte = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"uint8"}, Post: ""}
tFloat = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"float"}, Post: ""}
tNull = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name: "null"}, Post: ""}
tBool = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name: "bool"}, Post: ""}
// used only in module definintion
tEnum = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name: "enum"}, Post: ""}
tStruct = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name: "struct"}, Post: ""}
// Special types for if chain checking
tIF = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name: "if"}, Post: ""}
)
// 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
} else 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 == "readFile" || callPath.Name == "writeFile" {
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 "readFile":
return topenReadFile(in)
case "writeFile":
return topenWriteFile(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) {
if equateType(in.Type, tString) {
fmt.Print(datToString(in.Data))
} else {
fmt.Print(in.Data)
}
}
func tprintln(in TVariable) {
if equateType(in.Type, tString) {
fmt.Println(datToString(in.Data))
} else {
fmt.Println(in.Data)
}
}
func datToString(dat interface{}) string {
out := []byte{}
in := dat.([]interface{})
for i := 0; i < len(in); i++ {
out = append(out, in[i].(byte))
}
return string(out)
}
func topenWriteFile(in TVariable) TVariable {
if !equateType(in.Type, tString) {
panic("Tried to open a file (for writing), but did not use a string type for the file name.")
}
fd, err := os.Create(datToString(in.Data))
if err != nil {
panic(fmt.Sprintf("Failed to open file (for writing) %v as requested by the program. Aborting.\n%v", in.Data, err))
}
return TVariable{tFile, fd}
}
func topenReadFile(in TVariable) TVariable {
if !equateType(in.Type, tString) {
panic("Tried to open a file (for reading), but did not use a string type for the file name.")
}
fd, err := os.Open(datToString(in.Data))
if err != nil {
panic(fmt.Sprintf("Failed to open file (for reading) %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}
_, err := (file.Data).(*os.File).Read(b)
if err != nil {
return TVariable{tInt, -1}
}
return TVariable{tInt, int(b[0])}
}
// tnsl.io.File.write
func tfile_write(file, in TVariable) {
if equateType(file.Type, tFile) {
if equateType(in.Type, tByte) {
b := []byte{0}
b[0] = (in.Data).(byte)
(file.Data).(*os.File).Write(b)
} else if equateType(in.Type, tString) {
dat := (in.Data).([]interface{})
wrt := []byte{}
for i := 0; i < len(dat); i++ {
wrt = append(wrt, dat[i].(byte))
}
(file.Data).(*os.File).Write(wrt)
}
} else {
(file.Data).(*os.File).Close()
panic(fmt.Sprintf("Failed to write to file, attempted to use unsupported type (%v)\n", in.Type))
}
}
|