This repository was archived by the owner on Aug 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy patheval.go
More file actions
344 lines (285 loc) · 7.37 KB
/
Copy patheval.go
File metadata and controls
344 lines (285 loc) · 7.37 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// part6 - allow if, and implement more built-ins to make that useful.
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
// Word is the structure for a single word
type Word struct {
// Name is the name of the function "+", "print", etc.
Name string
// Function is the function-pointer to call to invoke it.
//
// If this is nil then instead we interpret the known-codes
// from previously defined words.
Function func()
// Words holds the words we execute if the function-pointer
// is empty.
//
// The indexes here are relative to the Dictionary our evaluator
// holds/maintains
Words []float64
}
// Eval is our evaluation structure
type Eval struct {
// Internal stack
Stack Stack
// Dictionary entries
Dictionary []Word
// Are we in a compiling mode?
compiling bool
// Temporary word we're compiling
tmp Word
// open of the last do
doOpen int
// offset of the argument to any IF
ifOffset int
}
// NewEval returns a simple evaluator
func NewEval() *Eval {
// Empty structure
e := &Eval{}
// Populate the dictionary of words we have implemented
// which are hard-coded.
e.Dictionary = []Word{
{Name: "*", Function: e.mul},
{Name: "+", Function: e.add},
{Name: "-", Function: e.sub},
{Name: ".", Function: e.print},
{Name: "/", Function: e.div},
{Name: ":", Function: e.startDefinition},
{Name: "<", Function: e.lt},
{Name: "<=", Function: e.ltEq},
{Name: "=", Function: e.eq},
{Name: "==", Function: e.eq},
{Name: ">", Function: e.gt},
{Name: ">=", Function: e.gtEq},
{Name: "do", Function: e.do}, // NOP
{Name: "if", Function: e.iff}, // NOP
{Name: "invert", Function: e.invert},
{Name: "drop", Function: e.drop},
{Name: "dup", Function: e.dup},
{Name: "emit", Function: e.emit},
{Name: "loop", Function: e.loop},
{Name: "print", Function: e.print},
{Name: "swap", Function: e.swap},
{Name: "then", Function: e.then}, // NOP
{Name: "words", Function: e.words},
}
return e
}
// Eval processes a list of tokens.
//
// This is invoked by our repl with a line of input at the time.
func (e *Eval) Eval(args []string) {
for _, tok := range args {
// Trim the leading/trailing spaces,
// and skip any empty tokens
tok = strings.TrimSpace(tok)
if tok == "" {
continue
}
// Are we in compiling mode?
if e.compiling {
// If we don't have a name
if e.tmp.Name == "" {
// is the name used?
idx := e.findWord(tok)
if idx != -1 {
fmt.Printf("word %s already defined\n", tok)
os.Exit(1)
}
// save the name
e.tmp.Name = tok
continue
}
// End of a definition?
if tok == ";" {
e.Dictionary = append(e.Dictionary, e.tmp)
e.tmp.Name = ""
e.tmp.Words = []float64{}
e.compiling = false
continue
}
// OK we have a name, so lookup the word definition
// for it.
idx := e.findWord(tok)
if idx >= 0 {
// Found it
e.tmp.Words = append(e.tmp.Words, float64(idx))
// If the word was a "DO"
if tok == "do" {
e.doOpen = len(e.tmp.Words) - 1
}
// if the word was a "LOOP"
if tok == "loop" {
// offset of do must be present
e.tmp.Words = append(e.tmp.Words, -2)
e.tmp.Words = append(e.tmp.Words, float64(e.doOpen))
}
// If the word was a "if"
if tok == "if" {
// we add the conditional-jump opcode
e.tmp.Words = append(e.tmp.Words, -3)
// placeholder jump-offset
e.tmp.Words = append(e.tmp.Words, 99)
// save the address of our stub,
// so we can back-patch
e.ifOffset = len(e.tmp.Words)
}
if tok == "then" {
// back - patch the jump offset to the position of this word
e.tmp.Words[e.ifOffset-1] = float64(len(e.tmp.Words) - 1)
}
} else {
// OK we assume the user entered a number
// so we save a magic "-1" flag in our
// definition, and then the number itself
e.tmp.Words = append(e.tmp.Words, -1)
// Convert to float
val, err := strconv.ParseFloat(tok, 64)
if err != nil {
fmt.Printf("%s: %s\n", tok, err.Error())
return
}
e.tmp.Words = append(e.tmp.Words, val)
}
continue
}
// Did we handle this as a dictionary item?
handled := false
for index, word := range e.Dictionary {
if tok == word.Name {
e.evalWord(index)
handled = true
}
}
// If we didn't handle this as a word, then
// assume it is a number.
if !handled {
i, err := strconv.ParseFloat(tok, 64)
if err != nil {
fmt.Printf("%s: %s\n", tok, err.Error())
return
}
e.Stack.Push(i)
}
}
}
// evalWord evaluates a word, by index from the dictionary
//
// * Functions might contain a pointer to a function implemented in go.
//
// If so we just call that pointer.
//
// * Functions will otherwise have lists of numbers, which point to
// previously defined words.
//
// In addition to the pointers to previously-defined words there are
// also some special values:
//
// "-1" means the next value is a number
//
// "-2" is an unconditional jump, which will change our IP.
//
// "-3" is a conditional-jump, which will change our IP if
// the topmost item on the stack is "0".
//
func (e *Eval) evalWord(index int) {
// Lookup the word
word := e.Dictionary[index]
// Is this implemented in golang? If so just invoke the function
// and we're done.
if word.Function != nil {
word.Function()
return
}
//
// TODO: Improve the way these special cases are handled.
//
// The reason this is handled like this, is to avoid poking the
// indexes directly and risking array-overflow on malformed
// word-lists.
//
// (i.e. When we see "[1, 2, -1]" the last instruction should add
// the following number to the stack - but it is missing. We want
// to avoid messing around with the index to avoid that.)
//
// Adding a number?
addNum := false
// jumping?
jump := false
// jumping if the stack has a false-value?
condJump := false
// We need to allow control-jumps now, so we
// have to store our index manually.
ip := 0
for ip < len(word.Words) {
// the current opcode
opcode := word.Words[ip]
// adding a number?
if addNum {
// add to stack
e.Stack.Push(opcode)
addNum = false
} else if jump {
// If the two top-most entries
// are not equal, then jump
cur := e.Stack.Pop()
max := e.Stack.Pop()
if max > cur {
// put them back
e.Stack.Push(max)
e.Stack.Push(cur)
// change opcode
ip = int(opcode)
// decrement as it'll get bumped at
// the foot of the loop
ip--
}
jump = false
} else if condJump {
// Jump only if 0 is on the top of the stack.
//
// i.e. This is an "if" test.
val := e.Stack.Pop()
if val == 0 {
// change opcode
ip = int(opcode)
// decrement as it'll get bumped at
// the foot of the loop
ip--
}
condJump = false
} else {
// if we see -1 we're adding a number
if opcode == -1 {
addNum = true
} else if opcode == -2 {
// -2 is a jump
jump = true
} else if opcode == -3 {
// -3 is a conditional-jump
condJump = true
} else {
// otherwise we evaluate
// otherwise eval as usual
e.evalWord(int(opcode))
}
}
// next instruction
ip++
}
}
// findWords returns the index in our dictionary of the entry for the
// given-name. Returns -1 if the word cannot be found.
func (e *Eval) findWord(name string) int {
for index, word := range e.Dictionary {
if name == word.Name {
return index
}
}
return -1
}