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 >>
quasiquote: ast
[
39 | result a a0 a0_ a0_0 a0_1 rest |
41 result
:= {MALSymbol new: #quote. ast
}.
42 ^MALList new: (OrderedCollection from: result
)
48 (a0 type
= #symbol and: [ a0 value
= #unquote ]) ifTrue: [ ^a second
].
55 (a0_0 type
= #symbol and:
56 [ a0_0 value
= #'splice-unquote' ]) ifTrue: [
57 rest
:= MALList new: a allButFirst
.
58 result
:= {MALSymbol new: #concat. a0_1
.
59 self quasiquote: rest
}.
60 ^MALList new: (OrderedCollection from: result
)
64 rest
:= MALList new: a allButFirst
.
65 result
:= {MALSymbol new: #cons. self quasiquote: a0
.
66 self quasiquote: rest
}.
67 ^MALList new: (OrderedCollection from: result
)
70 MAL class >>
EVAL: aSexp
env: anEnv
[
71 | sexp env ast a0 a0_ a1 a1_ a2 a3 forms function args |
73 "NOTE: redefinition of method arguments is not allowed"
79 sexp type ~
= #list ifTrue: [
80 ^self evalAst: sexp
env: env
82 sexp value isEmpty
ifTrue: [
89 a0_
:= ast first value
.
90 a0_
= #'def!' ifTrue: [
92 a1_
:= ast second value
.
94 result
:= self EVAL: a2
env: env
.
95 env
set: a1_
value: result
.
99 a0_
= #'let*' ifTrue: [
101 env_
:= Env new: env
.
102 a1_
:= ast second value
.
104 1 to: a1_ size
by: 2 do:
105 [ :i | env_
set: (a1_
at: i
) value
106 value: (self EVAL: (a1_
at: i
+ 1)
115 ast size <
2 ifTrue: [
117 last
:= MALObject Nil.
119 forms
:= ast
copyFrom: 2 to: ast size
- 1.
123 forms
do: [ :form |
self EVAL: form
env: env
].
132 a3
:= ast
at: 4 ifAbsent: [ MALObject Nil ].
133 condition
:= self EVAL: a1
env: env
.
135 (condition type
= #false or:
136 [ condition type
= #nil ]) ifTrue: [
144 a0_
= #quote ifTrue: [
149 a0_
= #quasiquote ifTrue: [
152 sexp
:= self quasiquote: a1
.
156 a0_
= #'fn*' ifTrue: [
158 a1_
:= ast second value
.
159 binds
:= a1_
collect: [ :item | item value
].
163 (Env new: env
binds: binds
exprs: args
) ].
164 ^Func new: a2
params: binds
env: env
fn: fn
167 forms
:= (self evalAst: sexp
env: env
) value
.
168 function
:= forms first
.
169 args
:= forms allButFirst asArray
.
171 function class
= BlockClosure ifTrue: [ ^function
value: args
].
172 function class
= Func ifTrue: [
174 sexp
:= function ast
.
175 env_
:= Env new: function env
binds: function params
184 MAL class >>
PRINT: sexp
[
185 ^Printer prStr: sexp
printReadably: true
188 MAL class >>
rep: input
env: env
[
189 ^self PRINT: (self EVAL: (self READ: input
) env: env
)
193 | input historyFile replEnv argv |
195 historyFile
:= '.mal_history'.
196 ReadLine readHistory: historyFile
.
197 replEnv
:= Env new: nil.
199 argv
:= Smalltalk arguments
.
200 argv notEmpty
ifTrue: [ argv
:= argv allButFirst
].
201 argv
:= OrderedCollection from: (argv
collect: [ :arg |
MALString new: arg
]).
203 Core Ns keysAndValuesDo: [ :op :block | replEnv
set: op
value: block
].
204 replEnv
set: #eval value: [ :args |
MAL EVAL: args first
env: replEnv
].
205 replEnv
set: #'*ARGV*' value: (MALList new: argv
).
207 MAL rep: '(def! not (fn* (a) (if a false true)))' env: replEnv
.
208 MAL rep: '(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))' env: replEnv
.
210 Smalltalk arguments notEmpty
ifTrue: [
211 MAL rep: '(load-file "',
Smalltalk arguments first,
'")' env: replEnv
213 [ input
:= ReadLine readLine: 'user> '. input isNil
] whileFalse: [
214 input isEmpty
ifFalse: [
215 ReadLine addHistory: input
.
216 ReadLine writeHistory: historyFile
.
217 [ (MAL rep: input
env: replEnv
) displayNl
]
218 on: MALEmptyInput do: [ #return ]
220 [ :err |
('error: ', err messageText
) displayNl
. #return ].