-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.go
201 lines (172 loc) · 4.94 KB
/
interpreter.go
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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"path"
"reflect"
"strings"
)
/*
func SprintRS(str string, a ...any) string {
interopCount := strings.Count(str, "{}")
if interopCount != len(a) {
panic(fmt.Sprintf("expected %d substituitions, but was given %d instead", interopCount, len(a)))
}
for {
ind := strings.Index(str, "{}")
if ind == -1 {
return str
}
}
}
*/
func isFile404Err(err string) bool {
return strings.HasPrefix(err, "open ") && strings.HasSuffix(err, ": no such file or directory")
}
func ImportModule(modpath string, allowFile404Recursion, printVars, printVarsEachCycle bool) (ModuleImport, error) {
modcontent, readErr := readFile(modpath)
if readErr != nil {
if isFile404Err(readErr.Error()) {
if allowFile404Recursion {
mod, err := ImportModule(path.Join("scripts", modpath), false, printVars, printVarsEachCycle)
if err != nil {
if !isFile404Err(readErr.Error()) {
return ModuleImport{}, err
}
}
return mod, nil
}
return ModuleImport{}, fmt.Errorf("module '%s' does not exist", modpath)
}
return ModuleImport{}, readErr
}
mod, modErr := RunGor(modcontent, modpath, true, false, false, printVars, printVarsEachCycle)
if modErr != nil {
return ModuleImport{}, modErr
}
return mod, nil
}
func NewGorError(t Token, msg string) error {
return fmt.Errorf("error on line %d, col %d-%d: %s", t.Ln, t.Start+1, t.End+2, msg)
}
func AssignVar(vars *map[string]any, funcs *map[string]any, identTok Token, value any) error {
if _, ok := (*funcs)[identTok.Lit]; ok {
return NewGorError(identTok, fmt.Sprintf("cannot assign value '%v' to function '%s'", value, identTok.Lit))
} else if e, isErr := value.(error); isErr {
return e
}
(*vars)[identTok.Lit] = value
return nil
}
func CallFunc(vars *map[string]any, funcs *map[string]any, identTok Token, value any) error {
return nil
}
func AddLabel(labels *map[string]uint, i uint, nameTok Token) error {
if _, ok := (*labels)[nameTok.Lit]; ok {
return NewGorError(nameTok, fmt.Sprintf("cannot create label '%v' as it already exists", nameTok.Lit))
}
(*labels)[nameTok.Lit] = i
return nil
}
func LabelJump(i *uint, labelTok Token, labels map[string]uint) error {
if _, ok := labels[labelTok.Lit]; !ok {
return NewGorError(labelTok, fmt.Sprintf("cannot jump to label '%v' as it doesn't exist", labelTok.Lit))
}
*i = labels[labelTok.Lit]
return nil
}
type ModuleImport struct {
vars, funcs map[string]any
}
func Interpret(nodes []Node, file string, printVars, printVarsEachCycle bool) (ModuleImport, error) {
var vars = make(map[string]any)
var funcs = make(map[string]any)
funcs["puts"] = func(a ...any) {
fmt.Println(a...)
}
funcs["getStr"] = func(prompt string) string {
scanner := bufio.NewScanner(os.Stdin)
fmt.Print(prompt)
scanner.Scan()
return scanner.Text()
}
var labels = make(map[string]uint)
for i, node := range nodes {
if n, ok := node.(LabelNode); ok {
err := AddLabel(&labels, uint(i), n.Name)
if err != nil {
return ModuleImport{}, err
}
}
}
var i uint = 0
for i < uint(len(nodes)) {
node := nodes[i]
if n, ok := node.(AssignmentNode); ok {
err := AssignVar(&vars, &funcs, n.Ident, n.Value.Generate(&vars, &funcs))
if err != nil {
return ModuleImport{}, err
}
i++
} else if n, ok := node.(FunccallNode); ok {
err := CallFunc(&vars, &funcs, n.Ident, n.GenerateArgs(&vars, &funcs))
if err != nil {
return ModuleImport{}, err
}
i++
} else if _, ok := node.(LabelNode); ok {
i++
} else if n, ok := node.(JumptoNode); ok {
err := LabelJump(&i, n.LabelIdent, labels)
if err != nil {
return ModuleImport{}, err
}
i++
} else if n, ok := node.(ModuleImportNode); ok {
modpath := path.Join(path.Dir(file), n.PathIdent.Lit)
if pathExt := path.Ext(modpath); pathExt == "" {
modpath += ".gor"
} else if pathExt != ".gor" {
return ModuleImport{}, fmt.Errorf("path '%s' is not a Gor file", modpath)
}
mod, err := ImportModule(modpath, true, printVars, printVarsEachCycle)
if err != nil {
return ModuleImport{}, err
}
for name, val := range mod.vars {
vars[name] = val
}
for name, fun := range mod.funcs {
funcs[name] = fun
}
i++
} else if n, ok := node.(IfStatementNode); ok {
res := n.Expr.Generate(&vars, &funcs)
if _, ok := res.(bool); !ok {
return ModuleImport{}, errors.New("expected boolean value")
}
if res.(bool) {
nodes = append(nodes, n.Nodes...)
}
i++
} else {
return ModuleImport{}, errors.New("unknown node '" + reflect.TypeOf(node).Name() + "'")
}
if printVarsEachCycle {
fmt.Println(vars)
for vname, vval := range vars {
fmt.Printf("'%s': %v, '%s'\n", vname, vval, reflect.TypeOf(vval).Name())
}
fmt.Println("")
}
}
if printVars && !printVarsEachCycle {
fmt.Println(vars)
for vname, vval := range vars {
fmt.Printf("'%s': %v, '%s'\n", vname, vval, reflect.TypeOf(vval).Name())
}
}
return ModuleImport{vars: vars, funcs: funcs}, nil
}