1 FileStream fileIn: 'readline.st'.
2 FileStream fileIn: 'reader.st'.
3 FileStream fileIn: 'printer.st'.
4 FileStream fileIn: 'env.st'.
5 FileStream fileIn: 'func.st'.
6 FileStream fileIn: 'core.st'.
9 MAL class >>
READ: input
[
10 ^Reader readStr: input
13 MAL class >>
evalAst: sexp
env: env
[
14 sexp type
= #symbol ifTrue: [
18 sexp type
= #list ifTrue: [
19 ^self evalList: sexp
env: env
class: MALList
21 sexp type
= #vector ifTrue: [
22 ^self evalList: sexp
env: env
class: MALVector
24 sexp type
= #map ifTrue: [
25 ^self evalList: sexp
env: env
class: MALMap
31 MAL class >>
evalList: sexp
env: env
class: aClass
[
33 items
:= sexp value
collect:
34 [ :item |
self EVAL: item
env: env
].
38 MAL class >>
EVAL: aSexp
env: anEnv
[
39 | sexp env ast a0 a0_ a1 a1_ a2 a3 forms function args |
41 "NOTE: redefinition of method arguments is not allowed"
47 sexp type ~
= #list ifTrue: [
48 ^self evalAst: sexp
env: env
50 sexp value isEmpty
ifTrue: [
57 a0_
:= ast first value
.
58 a0_
= #'def!' ifTrue: [
60 a1_
:= ast second value
.
62 result
:= self EVAL: a2
env: env
.
63 env
set: a1_
value: result
.
67 a0_
= #'let*' ifTrue: [
70 a1_
:= ast second value
.
72 1 to: a1_ size
by: 2 do:
73 [ :i | env_
set: (a1_
at: i
) value
74 value: (self EVAL: (a1_
at: i
+ 1)
83 ast size <
2 ifTrue: [
85 last
:= MALObject Nil.
87 forms
:= ast
copyFrom: 2 to: ast size
- 1.
91 forms
do: [ :form |
self EVAL: form
env: env
].
100 a3
:= ast
at: 4 ifAbsent: [ MALObject Nil ].
101 condition
:= self EVAL: a1
env: env
.
103 (condition type
= #false or:
104 [ condition type
= #nil ]) ifTrue: [
112 a0_
= #'fn*' ifTrue: [
114 a1_
:= ast second value
.
115 binds
:= a1_
collect: [ :item | item value
].
119 (Env new: env
binds: binds
exprs: args
) ].
120 ^Func new: a2
params: binds
env: env
fn: fn
123 forms
:= (self evalAst: sexp
env: env
) value
.
124 function
:= forms first
.
125 args
:= forms allButFirst asArray
.
127 function type
= #fn ifTrue: [ ^function fn
value: args
].
128 function type
= #func ifTrue: [
130 sexp
:= function ast
.
131 env_
:= Env new: function env
binds: function params
140 MAL class >>
PRINT: sexp
[
141 ^Printer prStr: sexp
printReadably: true
144 MAL class >>
rep: input
env: env
[
145 ^self PRINT: (self EVAL: (self READ: input
) env: env
)
149 | input historyFile replEnv argv |
151 historyFile
:= '.mal_history'.
152 ReadLine readHistory: historyFile
.
153 replEnv
:= Env new: nil.
155 argv
:= Smalltalk arguments
.
156 argv notEmpty
ifTrue: [ argv
:= argv allButFirst
].
157 argv
:= OrderedCollection from: (argv
collect: [ :arg |
MALString new: arg
]).
159 Core Ns keysAndValuesDo: [ :op :block | replEnv
set: op
value: block
].
160 replEnv
set: #eval value: (Fn new: [ :args |
MAL EVAL: args first
env: replEnv
]).
161 replEnv
set: #'*ARGV*' value: (MALList new: argv
).
163 MAL rep: '(def! not (fn* (a) (if a false true)))' env: replEnv
.
164 MAL rep: '(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))' env: replEnv
.
166 Smalltalk arguments notEmpty
ifTrue: [
167 MAL rep: '(load-file "',
Smalltalk arguments first,
'")' env: replEnv
169 [ input
:= ReadLine readLine: 'user> '. input isNil
] whileFalse: [
170 input isEmpty
ifFalse: [
171 ReadLine addHistory: input
.
172 ReadLine writeHistory: historyFile
.
173 [ (MAL rep: input
env: replEnv
) displayNl
]
174 on: MALEmptyInput do: [ #return ]
176 [ :err |
('error: ', err messageText
) displayNl
. #return ].