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_ 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: [
55 a0_
:= ast first value
.
57 a0_
= #'def!' ifTrue: [
59 a1_
:= ast second value
.
61 result
:= self EVAL: a2
env: env
.
62 env
set: a1_
value: result
.
66 a0_
= #'let*' ifTrue: [
69 a1_
:= ast second value
.
71 1 to: a1_ size
by: 2 do:
72 [ :i | env_
set: (a1_
at: i
) value
73 value: (self EVAL: (a1_
at: i
+ 1)
82 ast size <
2 ifTrue: [
84 last
:= MALObject Nil.
86 forms
:= ast
copyFrom: 2 to: ast size
- 1.
90 forms
do: [ :form |
self EVAL: form
env: env
].
99 a3
:= ast
at: 4 ifAbsent: [ MALObject Nil ].
100 condition
:= self EVAL: a1
env: env
.
102 (condition type
= #false or:
103 [ condition type
= #nil ]) ifTrue: [
111 a0_
= #'fn*' ifTrue: [
113 a1_
:= ast second value
.
114 binds
:= a1_
collect: [ :item | item value
].
118 (Env new: env
binds: binds
exprs: args
) ].
119 ^Func new: a2
params: binds
env: env
fn: fn
122 forms
:= (self evalAst: sexp
env: env
) value
.
123 function
:= forms first
.
124 args
:= forms allButFirst asArray
.
126 function class
= BlockClosure ifTrue: [ ^function
value: args
].
127 function class
= Func ifTrue: [
129 sexp
:= function ast
.
130 env_
:= Env new: function env
binds: function params
139 MAL class >>
PRINT: sexp
[
140 ^Printer prStr: sexp
printReadably: true
143 MAL class >>
rep: input
env: env
[
144 ^self PRINT: (self EVAL: (self READ: input
) env: env
)
148 | input historyFile replEnv |
150 historyFile
:= '.mal_history'.
151 ReadLine readHistory: historyFile
.
152 replEnv
:= Env new: nil.
154 Core Ns keysAndValuesDo: [ :op :block | replEnv
set: op
value: block
].
156 MAL rep: '(def! not (fn* (a) (if a false true)))' env: replEnv
.
158 [ input
:= ReadLine readLine: 'user> '. input isNil
] whileFalse: [
159 input isEmpty
ifFalse: [
160 ReadLine addHistory: input
.
161 ReadLine writeHistory: historyFile
.
162 [ (MAL rep: input
env: replEnv
) displayNl
]
163 on: MALEmptyInput do: [ #return ]
165 [ :err |
('error: ', err messageText
) displayNl
. #return ].