1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 01:57:09 +03:00
mal/pil/step2_eval.l
Vasilij Schneidermann 872ae9c4a6 Fix rep
2016-10-14 10:16:44 +02:00

60 lines
1.7 KiB
Plaintext

(de load-relative (Path)
(load (pack (car (file)) Path)) )
(load-relative "readline.l")
(load-relative "types.l")
(load-relative "reader.l")
(load-relative "printer.l")
(de READ (String)
(read-str String) )
(def '*ReplEnv
'((+ . ((A B) (MAL-number (+ (MAL-value A) (MAL-value B)))))
(- . ((A B) (MAL-number (- (MAL-value A) (MAL-value B)))))
(* . ((A B) (MAL-number (* (MAL-value A) (MAL-value B)))))
(/ . ((A B) (MAL-number (/ (MAL-value A) (MAL-value B))))) ) )
(de EVAL (Ast Env)
(if (= (MAL-type Ast) 'list)
(if (not (MAL-value Ast))
Ast
(let Value (MAL-value (eval-ast Ast Env))
(apply (car Value) (cdr Value)) ) )
(eval-ast Ast Env) ) )
(de eval-ast (Ast Env)
(let Value (MAL-value Ast)
(case (MAL-type Ast)
(symbol
(if (assoc Value Env)
(cdr @)
(throw 'err (MAL-error (pack "'" Value "' not found"))) ) )
(list (MAL-list (mapcar '((Form) (EVAL Form Env)) Value)))
(vector (MAL-vector (mapcar '((Form) (EVAL Form Env)) Value)))
(map (MAL-map (mapcar '((Form) (EVAL Form Env)) Value)))
(T Ast) ) ) )
(de PRINT (Ast)
(pr-str Ast T) )
(de rep (String)
(PRINT (EVAL (READ String) *ReplEnv)) )
(load-history ".mal_history")
(use Eof
(until Eof
(let Input (readline "user> ")
(if (=0 Input)
(setq Eof T)
(let Output (catch 'err (rep Input))
(if (isa '+MALError Output)
(let Message (MAL-value Output)
(unless (= Message "end of token stream")
(prinl "[error] " Message) ) )
(prinl Output) ) ) ) ) ) )
(prinl)
(bye)