summaryrefslogtreecommitdiff
path: root/src/texec/libtnsl.go
blob: 8ab214b34eee635d679c0454db2c85c006f2082f (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
/*
	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
*/

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

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

	return false
}

// evaluate a function call.
// out is the variable out (if any)
// in is the variable in (if any)
// callPath is the function being called.
func tnslEval(out, in *TVariable, callPath TPath) {

}

// evaluate a call on a file object
func tnslFileEval(file, out, in *TVariable, callPath TPath) {
	
}

// 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, out *TVariable) {
	if in.Type != "string" {
		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))
	}
	out.Type = "tnsl.io.File"
	out.Data = fd
}

func tclose_file(in TVariable, out *TVariable) {
	if in.Type != "string" {
		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))
	}
	out.Type = "tnsl.io.File"
	out.Data = fd
}


// File API

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

// tnsl.io.File.read
func tfile_read(file, out *TVariable) {
	b := []byte{1}
	(file.Data).(*os.File).Read(b)
	if out.Data == "uint8" || out.Data == "int8" {
		out.Data = b[0]
	}
}

// tnsl.io.File.write
func tfile_write(file, in *TVariable) {
	b := []byte{1}
	if in.Data == "uint8" || in.Data == "int8" {
		b[0] = (in.Data).(byte)
	}
	(file.Data).(*os.File).Write(b)
}