mirror of
https://github.com/kanaka/mal.git
synced 2024-11-10 12:47:45 +03:00
97 lines
2.1 KiB
Plaintext
97 lines
2.1 KiB
Plaintext
|
load "../logo/readline.lg
|
||
|
load "../logo/reader.lg
|
||
|
load "../logo/printer.lg
|
||
|
load "../logo/types.lg
|
||
|
load "../logo/env.lg
|
||
|
|
||
|
to _read :str
|
||
|
output read_str :str
|
||
|
end
|
||
|
|
||
|
to eval_ast :ast :env
|
||
|
output case (obj_type :ast) [
|
||
|
[[symbol] env_get :env :ast]
|
||
|
[[list] obj_new "list map [_eval ? :env] obj_val :ast]
|
||
|
[[vector] obj_new "vector map [_eval ? :env] obj_val :ast]
|
||
|
[[hashmap] obj_new "hashmap map [_eval ? :env] obj_val :ast]
|
||
|
[else :ast]
|
||
|
]
|
||
|
end
|
||
|
|
||
|
to _eval :ast :env
|
||
|
if (obj_type :ast) <> "list [output eval_ast :ast :env]
|
||
|
if emptyp obj_val :ast [output :ast]
|
||
|
localmake "a0 nth :ast 0
|
||
|
case list obj_type :a0 obj_val :a0 [
|
||
|
[[[symbol def!]]
|
||
|
localmake "a1 nth :ast 1
|
||
|
localmake "a2 nth :ast 2
|
||
|
output env_set :env :a1 _eval :a2 :env ]
|
||
|
|
||
|
[[[symbol let*]]
|
||
|
localmake "a1 nth :ast 1
|
||
|
localmake "letenv env_new :env [] []
|
||
|
localmake "i 0
|
||
|
while [:i < _count :a1] [
|
||
|
ignore env_set :letenv nth :a1 :i _eval nth :a1 (:i + 1) :letenv
|
||
|
make "i (:i + 2)
|
||
|
]
|
||
|
output _eval nth :ast 2 :letenv ]
|
||
|
|
||
|
[else
|
||
|
make "el obj_val eval_ast :ast :env
|
||
|
output apply first :el butfirst :el ]
|
||
|
]
|
||
|
end
|
||
|
|
||
|
to _print :exp
|
||
|
output pr_str :exp "true
|
||
|
end
|
||
|
|
||
|
to rep :str
|
||
|
output _print _eval _read :str :repl_env
|
||
|
end
|
||
|
|
||
|
to mal_add :a :b
|
||
|
output obj_new "number ((obj_val :a) + (obj_val :b))
|
||
|
end
|
||
|
|
||
|
to mal_sub :a :b
|
||
|
output obj_new "number ((obj_val :a) - (obj_val :b))
|
||
|
end
|
||
|
|
||
|
to mal_mul :a :b
|
||
|
output obj_new "number ((obj_val :a) * (obj_val :b))
|
||
|
end
|
||
|
|
||
|
to mal_div :a :b
|
||
|
output obj_new "number ((obj_val :a) / (obj_val :b))
|
||
|
end
|
||
|
|
||
|
to repl
|
||
|
localmake "running "true
|
||
|
while [:running] [
|
||
|
localmake "line readline word "user> :space_char
|
||
|
ifelse :line=[] [
|
||
|
print "
|
||
|
make "running "false
|
||
|
] [
|
||
|
if not emptyp :line [
|
||
|
catch "error [print rep :line]
|
||
|
localmake "exception error
|
||
|
if not emptyp :exception [
|
||
|
(print "Error: first butfirst :exception)
|
||
|
]
|
||
|
]
|
||
|
]
|
||
|
]
|
||
|
end
|
||
|
|
||
|
make "repl_env env_new [] [] []
|
||
|
ignore env_set :repl_env obj_new "symbol "+ "mal_add
|
||
|
ignore env_set :repl_env obj_new "symbol "- "mal_sub
|
||
|
ignore env_set :repl_env obj_new "symbol "* "mal_mul
|
||
|
ignore env_set :repl_env obj_new "symbol "/ "mal_div
|
||
|
repl
|
||
|
bye
|