This repository was archived by the owner on Aug 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patheval.go
More file actions
164 lines (135 loc) · 3.22 KB
/
Copy patheval.go
File metadata and controls
164 lines (135 loc) · 3.22 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
// part3 - allow defining words in our own environment.
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 []int
}
// 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
}
// 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.add}, // 0
{Name: "-", Function: e.sub}, // 1
{Name: "*", Function: e.mul}, // 2
{Name: "/", Function: e.div}, // 3
{Name: "print", Function: e.print}, // 4
{Name: ".", Function: e.print}, // 5
{Name: "dup", Function: e.dup}, // 6
{Name: ":", Function: e.startDefinition}, // 7
// Note we don't handle ";" here.
}
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 = []int{}
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, idx)
}
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
func (e *Eval) evalWord(index int) {
word := e.Dictionary[index]
if word.Function != nil {
word.Function()
} else {
for _, offset := range word.Words {
e.evalWord(offset)
}
}
}
func (e *Eval) findWord(name string) int {
for index, word := range e.Dictionary {
if name == word.Name {
return index
}
}
return -1
}