2016-03-01 17:44:09 +03:00
|
|
|
;; -*- lexical-binding: t; -*-
|
|
|
|
|
2020-07-21 19:01:48 +03:00
|
|
|
(require 'cl-lib)
|
2019-02-25 18:37:24 +03:00
|
|
|
(require 'mal/types)
|
|
|
|
(require 'mal/func)
|
|
|
|
(require 'mal/env)
|
|
|
|
(require 'mal/reader)
|
|
|
|
(require 'mal/printer)
|
|
|
|
(require 'mal/core)
|
2016-03-01 17:44:09 +03:00
|
|
|
|
|
|
|
(defvar repl-env (mal-env))
|
|
|
|
|
|
|
|
(dolist (binding core-ns)
|
|
|
|
(let ((symbol (car binding))
|
|
|
|
(fn (cdr binding)))
|
|
|
|
(mal-env-set repl-env symbol fn)))
|
|
|
|
|
2020-07-21 19:01:48 +03:00
|
|
|
(defun starts-with-p (ast sym)
|
2022-01-10 02:15:40 +03:00
|
|
|
(let ((l (mal-value ast)))
|
|
|
|
(and l
|
|
|
|
(let ((s (car l)))
|
|
|
|
(and (mal-symbol-p s)
|
|
|
|
(eq (mal-value s) sym))))))
|
2020-07-21 19:01:48 +03:00
|
|
|
|
|
|
|
(defun qq-reducer (elt acc)
|
|
|
|
(mal-list (if (and (mal-list-p elt)
|
|
|
|
(starts-with-p elt 'splice-unquote))
|
|
|
|
(list (mal-symbol 'concat) (cadr (mal-value elt)) acc)
|
|
|
|
(list (mal-symbol 'cons) (quasiquote elt) acc))))
|
|
|
|
|
|
|
|
(defun qq-iter (elts)
|
|
|
|
(cl-reduce 'qq-reducer elts :from-end t :initial-value (mal-list nil)))
|
2016-03-01 17:44:09 +03:00
|
|
|
|
|
|
|
(defun quasiquote (ast)
|
2020-07-21 19:01:48 +03:00
|
|
|
(cl-case (mal-type ast)
|
|
|
|
(list (if (starts-with-p ast 'unquote)
|
|
|
|
(cadr (mal-value ast))
|
|
|
|
(qq-iter (mal-value ast))))
|
|
|
|
(vector (mal-list (list (mal-symbol 'vec) (qq-iter (mal-value ast)))))
|
|
|
|
((map symbol) (mal-list (list (mal-symbol 'quote) ast)))
|
|
|
|
(t ast)))
|
2016-03-01 17:44:09 +03:00
|
|
|
|
|
|
|
(defun READ (input)
|
|
|
|
(read-str input))
|
|
|
|
|
|
|
|
(defun EVAL (ast env)
|
|
|
|
(catch 'return
|
|
|
|
(while t
|
|
|
|
|
2022-01-10 02:15:40 +03:00
|
|
|
(let ((dbgeval (mal-env-get env 'DEBUG-EVAL)))
|
|
|
|
(if (and dbgeval
|
|
|
|
(not (member (mal-type dbgeval) '(false nil))))
|
|
|
|
(println "EVAL: %s\n" (PRINT ast))))
|
|
|
|
|
|
|
|
(cl-case (mal-type ast)
|
2016-03-01 17:44:09 +03:00
|
|
|
|
2022-01-10 02:15:40 +03:00
|
|
|
(list
|
2016-03-01 17:44:09 +03:00
|
|
|
(let* ((a (mal-value ast))
|
|
|
|
(a1 (cadr a))
|
|
|
|
(a2 (nth 2 a))
|
|
|
|
(a3 (nth 3 a)))
|
2022-01-10 02:15:40 +03:00
|
|
|
(unless a (throw 'return ast))
|
2020-07-21 19:01:48 +03:00
|
|
|
(cl-case (mal-value (car a))
|
|
|
|
(def!
|
|
|
|
(let ((identifier (mal-value a1))
|
|
|
|
(value (EVAL a2 env)))
|
2016-03-01 17:44:09 +03:00
|
|
|
(throw 'return (mal-env-set env identifier value))))
|
2020-07-21 19:01:48 +03:00
|
|
|
(let*
|
|
|
|
(let ((env* (mal-env env))
|
|
|
|
(bindings (mal-listify a1))
|
|
|
|
(form a2))
|
2016-03-01 17:44:09 +03:00
|
|
|
(while bindings
|
|
|
|
(let ((key (mal-value (pop bindings)))
|
|
|
|
(value (EVAL (pop bindings) env*)))
|
|
|
|
(mal-env-set env* key value)))
|
|
|
|
(setq env env*
|
|
|
|
ast form))) ; TCO
|
2020-07-21 19:01:48 +03:00
|
|
|
(quote
|
2016-03-01 17:44:09 +03:00
|
|
|
(throw 'return a1))
|
2020-07-21 19:01:48 +03:00
|
|
|
(quasiquote
|
2016-03-01 17:44:09 +03:00
|
|
|
(setq ast (quasiquote a1))) ; TCO
|
2020-07-21 19:01:48 +03:00
|
|
|
(defmacro!
|
2016-03-01 17:44:09 +03:00
|
|
|
(let ((identifier (mal-value a1))
|
2021-08-09 00:09:30 +03:00
|
|
|
(value (mal-macro (EVAL a2 env))))
|
2016-03-01 17:44:09 +03:00
|
|
|
(throw 'return (mal-env-set env identifier value))))
|
2020-07-21 19:01:48 +03:00
|
|
|
(try*
|
2016-03-01 17:44:09 +03:00
|
|
|
(condition-case err
|
|
|
|
(throw 'return (EVAL a1 env))
|
|
|
|
(error
|
|
|
|
(if (and a2 (eq (mal-value (car (mal-value a2))) 'catch*))
|
|
|
|
(let* ((a2* (mal-value a2))
|
|
|
|
(identifier (mal-value (cadr a2*)))
|
|
|
|
(form (nth 2 a2*))
|
|
|
|
(err* (if (eq (car err) 'mal-custom)
|
|
|
|
;; throw
|
|
|
|
(cadr err)
|
|
|
|
;; normal error
|
|
|
|
(mal-string (error-message-string err))))
|
|
|
|
(env* (mal-env env (list identifier) (list err*))))
|
|
|
|
(throw 'return (EVAL form env*)))
|
2019-02-25 19:03:22 +03:00
|
|
|
(signal (car err) (cdr err))))))
|
2020-07-21 19:01:48 +03:00
|
|
|
(do
|
2016-03-01 17:44:09 +03:00
|
|
|
(let* ((a0... (cdr a))
|
|
|
|
(butlast (butlast a0...))
|
|
|
|
(last (car (last a0...))))
|
2022-01-10 02:15:40 +03:00
|
|
|
(mapcar (lambda (item) (EVAL item env)) butlast)
|
2016-03-01 17:44:09 +03:00
|
|
|
(setq ast last))) ; TCO
|
2020-07-21 19:01:48 +03:00
|
|
|
(if
|
2016-03-01 17:44:09 +03:00
|
|
|
(let* ((condition (EVAL a1 env))
|
|
|
|
(condition-type (mal-type condition))
|
|
|
|
(then a2)
|
|
|
|
(else a3))
|
|
|
|
(if (and (not (eq condition-type 'false))
|
|
|
|
(not (eq condition-type 'nil)))
|
|
|
|
(setq ast then) ; TCO
|
|
|
|
(if else
|
|
|
|
(setq ast else) ; TCO
|
2020-07-21 19:01:48 +03:00
|
|
|
(throw 'return mal-nil)))))
|
|
|
|
(fn*
|
2016-03-01 17:44:09 +03:00
|
|
|
(let* ((binds (mapcar 'mal-value (mal-value a1)))
|
|
|
|
(body a2)
|
|
|
|
(fn (mal-fn
|
|
|
|
(lambda (&rest args)
|
|
|
|
(let ((env* (mal-env env binds args)))
|
|
|
|
(EVAL body env*))))))
|
|
|
|
(throw 'return (mal-func body binds env fn))))
|
|
|
|
(t
|
|
|
|
;; not a special form
|
2022-01-10 02:15:40 +03:00
|
|
|
(let ((fn (EVAL (car a) env))
|
|
|
|
(args (cdr a)))
|
2016-03-01 17:44:09 +03:00
|
|
|
(if (mal-func-p fn)
|
2022-01-10 02:15:40 +03:00
|
|
|
(if (mal-func-macro-p fn)
|
|
|
|
(setq ast (apply (mal-value (mal-func-fn fn)) args)) ; TCO
|
2016-03-01 17:44:09 +03:00
|
|
|
(let ((env* (mal-env (mal-func-env fn)
|
|
|
|
(mal-func-params fn)
|
2022-01-10 02:15:40 +03:00
|
|
|
(mapcar (lambda (x) (EVAL x env)) args))))
|
2016-03-01 17:44:09 +03:00
|
|
|
(setq env env*
|
2022-01-10 02:15:40 +03:00
|
|
|
ast (mal-func-ast fn)))) ; TCO
|
2016-03-01 17:44:09 +03:00
|
|
|
;; built-in function
|
|
|
|
(let ((fn* (mal-value fn)))
|
2022-01-10 02:15:40 +03:00
|
|
|
(throw 'return (apply fn* (mapcar (lambda (x) (EVAL x env))
|
|
|
|
args))))))))))
|
2020-07-21 19:01:48 +03:00
|
|
|
(symbol
|
2022-01-10 02:15:40 +03:00
|
|
|
(let ((key (mal-value ast)))
|
|
|
|
(throw 'return (or (mal-env-get env key)
|
|
|
|
(error "'%s' not found" key)))))
|
2020-07-21 19:01:48 +03:00
|
|
|
(vector
|
2022-01-10 02:15:40 +03:00
|
|
|
(throw 'return
|
|
|
|
(mal-vector (vconcat (mapcar (lambda (item) (EVAL item env))
|
|
|
|
(mal-value ast))))))
|
2020-07-21 19:01:48 +03:00
|
|
|
(map
|
2022-01-10 02:15:40 +03:00
|
|
|
(let ((map (copy-hash-table (mal-value ast))))
|
2020-07-21 19:01:48 +03:00
|
|
|
(maphash (lambda (key val)
|
|
|
|
(puthash key (EVAL val env) map))
|
2016-03-01 17:44:09 +03:00
|
|
|
map)
|
2022-01-10 02:15:40 +03:00
|
|
|
(throw 'return (mal-map map))))
|
2016-03-01 17:44:09 +03:00
|
|
|
(t
|
|
|
|
;; return as is
|
2022-01-10 02:15:40 +03:00
|
|
|
(throw 'return ast))))))
|
2016-03-01 17:44:09 +03:00
|
|
|
|
|
|
|
(mal-env-set repl-env 'eval (mal-fn (let ((env repl-env)) (lambda (form) (EVAL form env)))))
|
2016-03-02 00:33:32 +03:00
|
|
|
(mal-env-set repl-env '*ARGV* (mal-list (mapcar 'mal-string (cdr argv))))
|
2016-03-01 17:44:09 +03:00
|
|
|
|
|
|
|
(defun PRINT (input)
|
|
|
|
(pr-str input t))
|
|
|
|
|
|
|
|
(defun rep (input)
|
|
|
|
(PRINT (EVAL (READ input) repl-env)))
|
|
|
|
|
|
|
|
(rep "(def! not (fn* (a) (if a false true)))")
|
2019-07-16 00:57:02 +03:00
|
|
|
(rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))")
|
2016-03-01 17:44:09 +03:00
|
|
|
(rep "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))")
|
|
|
|
|
|
|
|
(defun readln (prompt)
|
|
|
|
;; C-d throws an error
|
|
|
|
(ignore-errors (read-from-minibuffer prompt)))
|
|
|
|
|
|
|
|
(defun println (format-string &rest args)
|
|
|
|
(if (not args)
|
|
|
|
(princ format-string)
|
|
|
|
(princ (apply 'format format-string args)))
|
|
|
|
(terpri))
|
|
|
|
|
|
|
|
(defmacro with-error-handling (&rest body)
|
|
|
|
`(condition-case err
|
|
|
|
(progn ,@body)
|
|
|
|
(end-of-token-stream
|
|
|
|
;; empty input, carry on
|
|
|
|
)
|
|
|
|
(unterminated-sequence
|
2020-07-21 19:01:48 +03:00
|
|
|
(princ (format "Expected '%c', got EOF\n"
|
|
|
|
(cl-case (cadr err)
|
|
|
|
(string ?\")
|
|
|
|
(list ?\))
|
|
|
|
(vector ?\])
|
|
|
|
(map ?})))))
|
2016-03-01 17:44:09 +03:00
|
|
|
(error ; catch-all
|
|
|
|
(println (error-message-string err)))))
|
|
|
|
|
|
|
|
(defun main ()
|
|
|
|
(if argv
|
|
|
|
(with-error-handling
|
|
|
|
(rep (format "(load-file \"%s\")" (car argv))))
|
|
|
|
(let (eof)
|
|
|
|
(while (not eof)
|
|
|
|
(let ((input (readln "user> ")))
|
|
|
|
(if input
|
|
|
|
(with-error-handling
|
|
|
|
(println (rep input)))
|
|
|
|
(setq eof t)
|
|
|
|
;; print final newline
|
|
|
|
(terpri)))))))
|
|
|
|
|
|
|
|
(main)
|