2017-07-10 00:57:14 +03:00
|
|
|
String extend [
|
|
|
|
String >> loadRelative [
|
|
|
|
| scriptPath scriptDirectory |
|
2017-07-20 18:37:08 +03:00
|
|
|
scriptPath := thisContext currentFileName.
|
2017-07-10 00:57:14 +03:00
|
|
|
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.
|
|
|
|
'env.st' loadRelative.
|
2017-07-03 10:29:21 +03:00
|
|
|
|
|
|
|
Object subclass: MAL [
|
|
|
|
MAL class >> READ: input [
|
|
|
|
^Reader readStr: input
|
|
|
|
]
|
|
|
|
|
2022-01-10 02:15:40 +03:00
|
|
|
MAL class >> evalList: list env: env [
|
|
|
|
^list collect:
|
|
|
|
[ :item | self EVAL: item env: env ].
|
|
|
|
]
|
|
|
|
|
|
|
|
MAL class >> EVAL: sexp env: env [
|
|
|
|
| ast a0_ a1 a1_ a2 forms function args |
|
|
|
|
|
|
|
|
a2 := env get: #'DEBUG-EVAL'.
|
|
|
|
(a2 isNil or: [ a2 type = #false or: [ a2 type = #nil ] ] )
|
|
|
|
ifFalse: [
|
|
|
|
('EVAL: ' , (Printer prStr: sexp printReadably: true))
|
|
|
|
displayNl.
|
2017-07-03 10:29:21 +03:00
|
|
|
].
|
|
|
|
|
2022-01-10 02:15:40 +03:00
|
|
|
sexp type = #symbol ifTrue: [
|
|
|
|
| key value |
|
|
|
|
key := sexp value.
|
|
|
|
value := env get: key.
|
|
|
|
value isNil ifTrue: [
|
|
|
|
^MALUnknownSymbol new signal: key
|
|
|
|
].
|
|
|
|
^value
|
2017-07-03 10:29:21 +03:00
|
|
|
].
|
|
|
|
sexp type = #vector ifTrue: [
|
2022-01-10 02:15:40 +03:00
|
|
|
^MALVector new: (self evalList: sexp value env: env)
|
2017-07-03 10:29:21 +03:00
|
|
|
].
|
|
|
|
sexp type = #map ifTrue: [
|
2022-01-10 02:15:40 +03:00
|
|
|
^MALMap new: (self evalList: sexp value env: env)
|
2017-07-03 10:29:21 +03:00
|
|
|
].
|
|
|
|
sexp type ~= #list ifTrue: [
|
2022-01-10 02:15:40 +03:00
|
|
|
^sexp
|
2017-07-03 10:29:21 +03:00
|
|
|
].
|
|
|
|
sexp value isEmpty ifTrue: [
|
|
|
|
^sexp
|
|
|
|
].
|
|
|
|
|
|
|
|
ast := sexp value.
|
|
|
|
a0_ := ast first value.
|
|
|
|
a0_ = #'def!' ifTrue: [
|
|
|
|
| result |
|
|
|
|
a1_ := ast second value.
|
|
|
|
a2 := ast third.
|
|
|
|
result := self EVAL: a2 env: env.
|
|
|
|
env set: a1_ value: result.
|
|
|
|
^result
|
|
|
|
].
|
|
|
|
|
|
|
|
a0_ = #'let*' ifTrue: [
|
|
|
|
| env_ |
|
|
|
|
env_ := Env new: env.
|
|
|
|
a1_ := ast second value.
|
|
|
|
a2 := ast third.
|
|
|
|
1 to: a1_ size by: 2 do:
|
|
|
|
[ :i | env_ set: (a1_ at: i) value
|
|
|
|
value: (self EVAL: (a1_ at: i + 1) env: env_) ].
|
|
|
|
^self EVAL: a2 env: env_
|
|
|
|
].
|
|
|
|
|
2022-01-10 02:15:40 +03:00
|
|
|
forms := self evalList: sexp value env: env.
|
2017-07-03 10:29:21 +03:00
|
|
|
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 := Env new: nil.
|
|
|
|
|
|
|
|
replEnv set: #+ value: [ :a :b | MALNumber new: a value + b value ].
|
|
|
|
replEnv set: #- value: [ :a :b | MALNumber new: a value - b value ].
|
|
|
|
replEnv set: #* value: [ :a :b | MALNumber new: a value * b value ].
|
|
|
|
replEnv set: #/ 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.
|