-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic.go
More file actions
415 lines (379 loc) · 11.2 KB
/
Copy patharithmetic.go
File metadata and controls
415 lines (379 loc) · 11.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package main
import (
"fmt"
"os"
"io/ioutil"
"regexp"
"strconv"
)
var Tokens = make([]Token, 0)
var envs = make([]map[string]interface{}, 1)
type Token struct {
tokentype string
value string
linenum int
}
type Definition struct {
name string
value interface{}
}
type Func_Def struct {
name string
params []string
body interface{}
}
type Func_Call struct {
name string
args []interface{}
}
type Variable struct {
name string
}
type Number struct {
value float64
}
type BinaryExpression struct {
typetag string
lhand interface{}
rhand interface{}
}
func eval(node interface{}, currenv map[string]interface{}) interface{} {
switch node := node.(type) {
case *Definition:
val := eval(node.value, currenv)
currenv[node.name] = val
return val
case *Variable:
val := lookup(node.name)
return val.(*Number)
case *Number:
return node
case *BinaryExpression:
op := node.typetag
lhs := eval(node.lhand, currenv).(*Number)
rhs := eval(node.rhand, currenv).(*Number)
switch op {
case "addition":
return &Number{ lhs.value + rhs.value }
case "subtraction":
return &Number{ lhs.value - rhs.value }
case "multiplication":
return &Number{ lhs.value * rhs.value }
case "division":
if rhs.value != 0 {
return &Number{ lhs.value / rhs.value }
} else {
panic("cannot divide by zero.")
}
default:
panic("unrecognized operation.")
}
case *Func_Def:
currenv[node.name] = node
//could change this to make eval return an interface & check return types
return &Number{ 0 }
case *Func_Call:
funcdef := lookup(node.name).(*Func_Def)
newenv := make(map[string]interface{})
if len(funcdef.params) != len(node.args) {
panic("wrong nubmer of arguments for function "+node.name)
}
for i, elem := range node.args {
tmp := eval(elem, currenv).(*Number)
newenv[funcdef.params[i]] = tmp
}
tmpenvs := make([]map[string]interface{}, 1)
tmpenvs[0] = newenv
for _, elem := range envs {
tmpenvs = append(tmpenvs, elem)
}
oldenvs := envs
envs = tmpenvs
retval := eval(funcdef.body, envs[0]).(*Number)
envs = oldenvs
return retval
default:
panic("unrecognized expression.")
}
}
func lookup(name string) interface{} {
var value interface{}
if len(envs) == 1 {
value, was_there := envs[0][name]
if was_there {
return value
} else {
panic("variable not defined: "+name)
}
} else if len(envs) > 1 {
for _, elem := range envs {
value, was_there := elem[name]
if was_there {
return value
}
}
} else {
panic("variable not defined: "+name)
}
return value
}
// todo
// more complex env environment (slice of maps)
// eval needs to take in the env
// then a lookup function searches through the environments in the correct order and returns the first thing
// in the function call clause, need to create a new environment mapping the args to the params and pass that into eval with the function body
// then pop the new env off the the environments slice
// in the function call clause, can just pass in the result of append to eval so it doesn't modify the current env
func program() []interface{} {
v := make([]interface{}, 0)
for len(Tokens) != 0 {
if Tokens[0].tokentype == "EOF" {
break
}
if the_statement := statement(); the_statement != nil {
v = append(v, the_statement)
}
}
return v
}
func statement() interface{} {
statementline := Tokens[0].linenum
var the_statement interface{}
if Tokens[0].tokentype == "let" {
the_statement = definition()
} else if Tokens[0].tokentype == "semicolon" {
Tokens = Tokens[1:]
return nil
} else if Tokens[0].tokentype == "func" {
the_statement = function_def()
} else {
the_statement = expression()
}
if Tokens[0].tokentype != "semicolon" {
panic("expected semicolon at the end of a statement. line number: "+strconv.Itoa(statementline))
} else {
Tokens = Tokens[1:]
return the_statement
}
}
func definition() *Definition {
Tokens = Tokens[1:]
if Tokens[0].tokentype != "identifier" {
panic("no variable name after let. line number: "+strconv.Itoa(Tokens[0].linenum))
}
vari := variable()
if Tokens[0].tokentype != "equal" {
panic("no equal sign after variable definition. line number: "+strconv.Itoa(Tokens[0].linenum))
}
Tokens = Tokens[1:]
expr := expression()
return &Definition{ vari.name, expr }
}
func function_def() *Func_Def {
Tokens = Tokens[1:]
if Tokens[0].tokentype != "identifier" {
panic("no variable name after let. line number: "+strconv.Itoa(Tokens[0].linenum))
}
funcname := variable().name
if Tokens[0].tokentype != "lparen" {
panic("no parenthesis after function name")
}
Tokens = Tokens[1:]
the_params := params()
if Tokens[0].tokentype != "rparen" {
panic("couldn't parse param list, needed right parens")
}
Tokens = Tokens[1:]
if Tokens[0].tokentype != "equal" {
panic("no equal sign after function param list")
}
Tokens = Tokens[1:]
body := expression()
return &Func_Def{ funcname, the_params, body }
}
func params() []string {
vars := make([]string, 0)
if Tokens[0].tokentype == "rparen" {
return vars
}
if Tokens[0].tokentype == "identifier" {
vars = append(vars, variable().name)
} else {
panic("expecting parameter name")
}
for Tokens[0].tokentype == "comma" {
Tokens = Tokens[1:]
if Tokens[0].tokentype == "identifier" {
vars = append(vars, variable().name)
} else {
panic("expecting parameter name")
}
}
return vars
}
func expression() interface{} {
the_expr := term()
if op := Tokens[0].tokentype; op != "addition" && op != "subtraction" {
return the_expr
}
for Tokens[0].tokentype == "addition" || Tokens[0].tokentype == "subtraction" {
op := Tokens[0].tokentype;
Tokens = Tokens[1:]
the_expr = &BinaryExpression{ op, the_expr, term() }
}
return the_expr
}
func term() interface{} {
the_term := factor()
if op := Tokens[0].tokentype; op != "multiplication" && op != "division" {
return the_term
}
for Tokens[0].tokentype == "multiplication" || Tokens[0].tokentype == "division" {
op := Tokens[0].tokentype;
Tokens = Tokens[1:]
the_term = &BinaryExpression{ op, the_term, factor() }
}
return the_term
}
func factor() interface{} {
if Tokens[0].tokentype == "lparen" {
openingparenline := Tokens[0].linenum
Tokens = Tokens[1:]
expr := expression()
if Tokens[0].tokentype == "rparen" {
Tokens = Tokens[1:]
} else {
panic("unmatched parentheses. line number: "+strconv.Itoa(openingparenline))
}
return expr
} else if Tokens[0].tokentype == "identifier" {
if Tokens[1].tokentype == "lparen" {
return function_call()
} else {
return variable()
}
} else if Tokens[0].tokentype == "number" {
return number()
} else {
panic("couldn't parse factor. line number: "+strconv.Itoa(Tokens[0].linenum))
}
}
func function_call() *Func_Call {
funcname := variable().name
Tokens = Tokens[1:]
funcargs := args()
if Tokens[0].tokentype != "rparen" {
panic("couldn't parse argument list, needed right parens")
}
Tokens = Tokens[1:]
return &Func_Call{ funcname, funcargs }
}
func args() []interface{} {
the_args := make([]interface{}, 0)
if Tokens[0].tokentype == "rparen" {
return the_args
}
the_args = append(the_args, expression())
for Tokens[0].tokentype == "comma" {
Tokens = Tokens[1:]
the_args = append(the_args, expression())
}
return the_args
}
func number() *Number {
num1, _ := strconv.ParseFloat(Tokens[0].value, 64)
num := &Number{ num1 }
Tokens = Tokens[1:]
return num
}
func variable() *Variable {
vari := &Variable{ Tokens[0].value }
Tokens = Tokens[1:]
return vari
}
func readFile(filename string) string {
f, err := ioutil.ReadFile(filename)
if err!= nil {
panic(err)
}
return string(f)
}
func munch(src string) string {
numberPattern, _ := regexp.Compile(`\A\d*\.?\d+`)
additionPattern, _ := regexp.Compile(`\A\+`)
subtractionPattern, _ := regexp.Compile(`\A\-`)
multiplicationPattern, _ := regexp.Compile(`\A\*`)
divisionPattern, _ := regexp.Compile(`\A\/`)
lparenPattern, _ := regexp.Compile(`\A\(`)
rparenPattern, _ := regexp.Compile(`\A\)`)
whitespacePattern, _ := regexp.Compile(`\A\s`)
identifierPattern, _ := regexp.Compile(`\A[a-zA-Z]+`)
letPattern, _ := regexp.Compile(`\Alet`)
equalPattern, _ := regexp.Compile(`\A\=`)
semicolonPattern, _ := regexp.Compile(`\A;`)
funcPattern, _ := regexp.Compile(`\Afunc`)
commaPattern, _ := regexp.Compile(`\A,`)
linenum := 1
for len(src) != 0 {
if c := numberPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "number", src, linenum)
} else if c := additionPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "addition", src, linenum)
} else if c := subtractionPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "subtraction", src, linenum)
} else if c := multiplicationPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "multiplication", src, linenum)
} else if c := divisionPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "division", src, linenum)
} else if c := lparenPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "lparen", src, linenum)
} else if c := rparenPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "rparen", src, linenum)
} else if c := whitespacePattern.Find([]byte(src)); c != nil {
if c[0] == 10 { linenum++ }
src = src[len(c):]
} else if c := letPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "let", src, linenum)
} else if c := funcPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "func", src, linenum)
} else if c := identifierPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "identifier", src, linenum)
} else if c := equalPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "equal", src, linenum)
} else if c := semicolonPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "semicolon", src, linenum)
} else if c := commaPattern.Find([]byte(src)); c != nil {
src = munchToken(c, "comma", src, linenum)
} else {
//exit
panic("did not recognize token: "+src[0:1]+ " on line number "+strconv.Itoa(linenum))
}
}
src = munchToken(nil, "EOF", src, linenum)
return src;
}
func munchToken(c []byte, ttype string, source string, line int) string {
t := Token{ ttype, string(c), line }
source = source[len(c):]
Tokens = append(Tokens, t)
return source
}
func printTokens() {
for key, value := range Tokens {
fmt.Printf("Token %i: %s\n", key, value)
}
}
func main() {
contents := readFile(os.Args[1])
munch(contents)
// printTokens()
prog := program()
// fmt.Printf("%#v\n", prog)
var result interface{}
// env := make([]map[string]interface{}, 1)
envs[0] = make(map[string]interface{})
for _, expr := range prog {
result = eval(expr, envs[0])
}
fmt.Println(result)
}