-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
158 lines (116 loc) · 3.2 KB
/
exec.go
File metadata and controls
158 lines (116 loc) · 3.2 KB
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
package main
import (
"fmt"
)
func evalString(s string, programStack *stack, programVariableScope *variableScope, programSymbolTable *symbolTable, printStack bool) error {
_, items := lex("test", s)
main, err := parseCodeBlock(&parser{items, 0,})
if err != nil {
return err
}
execErr := main.exec(programStack, programVariableScope, programSymbolTable, false)
if execErr != nil {
return execErr
}
if printStack {
programStack.print()
}
return nil
}
func printExecError(err error) {
fmt.Printf("Error: %s\n", err.Error())
}
func handleIdentifier(v *variableScope, st *symbolTable, ident *langObjectIdentifier) (langObject, error) {
if ident.typ == identifierName {
return &langObjectIdentifier{
identifierReference,
ident.name,
}, nil
}
stKey, ok := v.get(ident.name)
switch ident.typ {
case identifierDefault:
if ok == false {
return nil, fmt.Errorf("Variable '%s' undefined in the local scope.", ident.name)
}
obj, stOk := st.retrieve(stKey)
if stOk == false {
return nil, fmt.Errorf("Unable to retrieve object with ID %X from the symbol table.", stKey)
}
/* Whenever we push a reference onto the stack, we must handle garbage collection */
if obj.getType() == objectTypeReference {
stErr := st.incReference(obj.(*langObjectReference).key)
if stErr != nil {
return nil, fmt.Errorf("Unable to retrieve object with ID %X from the symbol table.", stKey)
}
}
//s.push(obj.copy())
return obj.copy(), nil
case identifierReference:
if ok == true {
stErr := st.incReference(stKey)
if stErr != nil {
return nil, stErr
}
//s.push(&langObjectReference{stKey,})
return &langObjectReference{stKey,}, nil
} else {
//s.push(ident)
return ident, nil
}
}
return nil, nil
}
func (l *langObjectCodeBlock) exec(s *stack, v *variableScope, st *symbolTable, cleanUpLocal bool) error {
for _, o := range l.code {
/*fmt.Println(o.toString())
fmt.Println("---")
s.print()
fmt.Println("---")*/
switch {
case o.getType() == objectTypeNumber|| o.getType() == objectTypeString || o.getType() == objectTypeBoolean:
s.push(o)
case o.getType() == objectTypeList:
if o.(*langObjectList).empty {
s.push(o)
} else {
s.push(o.copy())
}
case o.getType() == objectTypeCodeBlock:
codeBlock := o.(*langObjectCodeBlock)
/* A code block has its parent scope set once, when it is first placed onto the stack.
This allows recursive functions to work. */
if codeBlock.parentScope == nil {
codeBlock.parentScope = v
}
handleErr := o.(*langObjectCodeBlock).handleParentVariables(st)
if handleErr != nil {
return handleErr
}
s.push(o)
case o.getType() == objectTypeIdentifier:
identifier, identErr := handleIdentifier(v, st, o.(*langObjectIdentifier))
if identErr != nil {
return identErr
}
s.push(identifier)
case o.getType() == objectTypeOperation:
err := performOperation(o.getValue().(operationType), s, v, st)
if err != nil {
return err
}
}
/*s.print()
fmt.Println("---\n")*/
}
if cleanUpLocal {
for _, langVar := range v.variables {
stErr := st.decReference(langVar.key)
if stErr != nil {
return stErr
}
}
}
st.cleanUp()
return nil
}