2016-10-02 20:41:49 +03:00
|
|
|
(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) )
|
|
|
|
|
2016-10-14 10:40:19 +03:00
|
|
|
(def '*ReplEnv
|
2016-10-02 20:41:49 +03:00
|
|
|
'((+ . ((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)
|
2022-01-10 02:15:40 +03:00
|
|
|
;; (prinl "EVAL: " (pr-str Ast T))
|
2016-10-02 20:41:49 +03:00
|
|
|
(let Value (MAL-value Ast)
|
|
|
|
(case (MAL-type Ast)
|
|
|
|
(symbol
|
|
|
|
(if (assoc Value Env)
|
|
|
|
(cdr @)
|
2016-10-22 13:37:24 +03:00
|
|
|
(throw 'err (MAL-error (MAL-string (pack "'" Value "' not found")))) ) )
|
2022-01-10 02:15:40 +03:00
|
|
|
(list
|
|
|
|
(if Value
|
|
|
|
(let El (mapcar '((Form) (EVAL Form Env)) Value)
|
|
|
|
(apply (car El) (cdr El)))
|
|
|
|
Ast))
|
2016-10-02 20:41:49 +03:00
|
|
|
(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) )
|
|
|
|
|
2016-10-14 11:16:44 +03:00
|
|
|
(de rep (String)
|
|
|
|
(PRINT (EVAL (READ String) *ReplEnv)) )
|
2016-10-02 20:41:49 +03:00
|
|
|
|
|
|
|
(load-history ".mal_history")
|
|
|
|
|
|
|
|
(use Eof
|
|
|
|
(until Eof
|
|
|
|
(let Input (readline "user> ")
|
|
|
|
(if (=0 Input)
|
|
|
|
(setq Eof T)
|
2016-10-14 11:16:44 +03:00
|
|
|
(let Output (catch 'err (rep Input))
|
2016-10-02 20:41:49 +03:00
|
|
|
(if (isa '+MALError Output)
|
|
|
|
(let Message (MAL-value Output)
|
2016-10-22 13:37:24 +03:00
|
|
|
(unless (= (MAL-value Message) "end of token stream")
|
|
|
|
(prinl "[error] " (pr-str Message)) ) )
|
2016-10-02 20:41:49 +03:00
|
|
|
(prinl Output) ) ) ) ) ) )
|
|
|
|
|
|
|
|
(prinl)
|
|
|
|
(bye)
|