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 >>
isMacroCall: ast
env: env
[
72 ast type
= #list ifTrue: [
73 a0
:= ast value first
.
75 a0 type
= #symbol ifTrue: [
77 (f notNil
and: [ f type
= #func ]) ifTrue: [
85 MAL class >>
macroexpand: aSexp
env: env
[
88 "NOTE: redefinition of method arguments is not allowed"
91 [ self isMacroCall: sexp
env: env
] whileTrue: [
92 | ast a0_ macro rest |
94 a0_
:= ast first value
.
95 macro
:= env
find: a0_
.
96 rest
:= ast allButFirst
.
97 sexp
:= macro fn
value: rest
.
103 MAL class >>
EVAL: aSexp
env: anEnv
[
104 | sexp env ast a0 a0_ a1 a1_ a2 a3 forms function args |
106 "NOTE: redefinition of method arguments is not allowed"
112 sexp type ~
= #list ifTrue: [
113 ^self evalAst: sexp
env: env
115 sexp value isEmpty
ifTrue: [
119 sexp
:= self macroexpand: sexp
env: env
.
120 sexp type ~
= #list ifTrue: [
121 ^self evalAst: sexp
env: env
127 a0_
:= ast first value
.
128 a0_
= #'def!' ifTrue: [
130 a1_
:= ast second value
.
132 result
:= self EVAL: a2
env: env
.
133 env
set: a1_
value: result
.
137 a0_
= #'defmacro!' ifTrue: [
139 a1_
:= ast second value
.
141 result
:= self EVAL: a2
env: env
.
142 result
isMacro: true.
143 env
set: a1_
value: result
.
147 a0_
= #'macroexpand' ifTrue: [
149 ^self macroexpand: a1
env: env
152 a0_
= #'let*' ifTrue: [
154 env_
:= Env new: env
.
155 a1_
:= ast second value
.
157 1 to: a1_ size
by: 2 do:
158 [ :i | env_
set: (a1_
at: i
) value
159 value: (self EVAL: (a1_
at: i
+ 1)
168 ast size <
2 ifTrue: [
170 last
:= MALObject Nil.
172 forms
:= ast
copyFrom: 2 to: ast size
- 1.
176 forms
do: [ :form |
self EVAL: form
env: env
].
185 a3
:= ast
at: 4 ifAbsent: [ MALObject Nil ].
186 condition
:= self EVAL: a1
env: env
.
188 (condition type
= #false or:
189 [ condition type
= #nil ]) ifTrue: [
197 a0_
= #quote ifTrue: [
202 a0_
= #quasiquote ifTrue: [
205 sexp
:= self quasiquote: a1
.
209 a0_
= #'fn*' ifTrue: [
211 a1_
:= ast second value
.
212 binds
:= a1_
collect: [ :item | item value
].
216 (Env new: env
binds: binds
exprs: args
) ].
217 ^Func new: a2
params: binds
env: env
fn: fn
220 forms
:= (self evalAst: sexp
env: env
) value
.
221 function
:= forms first
.
222 args
:= forms allButFirst asArray
.
224 function type
= #fn ifTrue: [ ^function fn
value: args
].
225 function type
= #func ifTrue: [
227 sexp
:= function ast
.
228 env_
:= Env new: function env
binds: function params
237 MAL class >>
PRINT: sexp
[
238 ^Printer prStr: sexp
printReadably: true
241 MAL class >>
rep: input
env: env
[
242 ^self PRINT: (self EVAL: (self READ: input
) env: env
)
246 | input historyFile replEnv argv |
248 historyFile
:= '.mal_history'.
249 ReadLine readHistory: historyFile
.
250 replEnv
:= Env new: nil.
252 argv
:= Smalltalk arguments
.
253 argv notEmpty
ifTrue: [ argv
:= argv allButFirst
].
254 argv
:= OrderedCollection from: (argv
collect: [ :arg |
MALString new: arg
]).
256 Core Ns keysAndValuesDo: [ :op :block | replEnv
set: op
value: block
].
257 replEnv
set: #eval value: (Fn new: [ :args |
MAL EVAL: args first
env: replEnv
]).
258 replEnv
set: #'*ARGV*' value: (MALList new: argv
).
260 MAL rep: '(def! not (fn* (a) (if a false true)))' env: replEnv
.
261 MAL rep: '(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))' env: replEnv
.
263 MAL rep: '(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list ''if (first xs) (if (> (count xs) 1) (nth xs 1) (throw "odd number of forms to cond")) (cons ''cond (rest (rest xs)))))))' env: replEnv
.
264 MAL rep: '(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))' env: replEnv
.
266 Smalltalk arguments notEmpty
ifTrue: [
267 MAL rep: '(load-file "',
Smalltalk arguments first,
'")' env: replEnv
269 [ input
:= ReadLine readLine: 'user> '. input isNil
] whileFalse: [
270 input isEmpty
ifFalse: [
271 ReadLine addHistory: input
.
272 ReadLine writeHistory: historyFile
.
273 [ (MAL rep: input
env: replEnv
) displayNl
]
274 on: MALEmptyInput do: [ #return ]
276 [ :err |
('error: ', err messageText
) displayNl
. #return ].