forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep2_eval.st
93 lines (79 loc) · 2.55 KB
/
step2_eval.st
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
String extend [
String >> loadRelative [
| scriptPath scriptDirectory |
scriptPath := thisContext currentFileName.
scriptDirectory := FilePath stripFileNameFor: scriptPath.
FileStream fileIn: (FilePath append: self to: scriptDirectory)
]
]
'readline.st' loadRelative.
'util.st' loadRelative.
'types.st' loadRelative.
'reader.st' loadRelative.
'printer.st' loadRelative.
Object subclass: MAL [
MAL class >> READ: input [
^Reader readStr: input
]
MAL class >> evalAst: sexp env: env [
sexp type = #symbol ifTrue: [
^env at: sexp value ifAbsent: [
^MALUnknownSymbol new signal: sexp value
].
].
sexp type = #list ifTrue: [
^self evalList: sexp env: env class: MALList
].
sexp type = #vector ifTrue: [
^self evalList: sexp env: env class: MALVector
].
sexp type = #map ifTrue: [
^self evalList: sexp env: env class: MALMap
].
^sexp
]
MAL class >> evalList: sexp env: env class: aClass [
| items |
items := sexp value collect:
[ :item | self EVAL: item env: env ].
^aClass new: items
]
MAL class >> EVAL: sexp env: env [
| forms function args |
sexp type ~= #list ifTrue: [
^self evalAst: sexp env: env
].
sexp value isEmpty ifTrue: [
^sexp
].
forms := (self evalAst: sexp env: env) value.
function := forms first.
args := forms allButFirst asArray.
^function valueWithArguments: args
]
MAL class >> PRINT: sexp [
^Printer prStr: sexp printReadably: true
]
MAL class >> rep: input env: env [
^self PRINT: (self EVAL: (self READ: input) env: env)
]
]
| input historyFile replEnv |
historyFile := '.mal_history'.
ReadLine readHistory: historyFile.
replEnv := Dictionary from:
{ #+ -> [ :a :b | MALNumber new: a value + b value ].
#- -> [ :a :b | MALNumber new: a value - b value ].
#* -> [ :a :b | MALNumber new: a value * b value ].
#/ -> [ :a :b | MALNumber new: a value // b value ] }.
[ input := ReadLine readLine: 'user> '. input isNil ] whileFalse: [
input isEmpty ifFalse: [
ReadLine addHistory: input.
ReadLine writeHistory: historyFile.
[ (MAL rep: input env: replEnv) displayNl ]
on: MALEmptyInput do: [ #return ]
on: MALError do:
[ :err | ('error: ', err messageText) displayNl. #return ].
]
]
'' displayNl.