Merge branch 'master' of github.com:kanaka/mal
[jackhill/mal.git] / elisp / step1_read_print.el
CommitLineData
0195b0ce
VS
1(defun load-relative (file)
2 (let* ((current-file (or load-file-name buffer-file-name))
3 (current-file-directory (file-name-directory current-file)))
4 (load (expand-file-name file current-file-directory) nil t)))
5
6(load-relative "types.el")
7(load-relative "reader.el")
8(load-relative "printer.el")
9
10(defun READ (input)
11 (read-str input))
12
13(defun EVAL (input)
14 input)
15
16(defun PRINT (input)
17 (pr-str input t))
18
19(defun rep (input)
20 (PRINT (EVAL (READ input))))
21
22(defun readln (prompt)
23 ;; C-d throws an error
24 (ignore-errors (read-from-minibuffer prompt)))
25
26(defun println (format-string &rest args)
27 (if (not args)
28 (princ format-string)
29 (princ (apply 'format format-string args)))
30 (terpri))
31
32(defun main ()
33 (let (eof)
34 (while (not eof)
35 (let ((input (readln "user> ")))
36 (if input
37 (condition-case err
38 (println (rep input))
39 (end-of-token-stream
40 ;; empty input, carry on
41 )
42 (unterminated-sequence
43 (let* ((type (cadr err))
44 (end
45 (cond
46 ((eq type 'string) ?\")
47 ((eq type 'list) ?\))
48 ((eq type 'vector) ?\])
49 ((eq type 'map) ?}))))
50 (princ (format "Expected '%c', got EOF\n" end))))
51 (error ; catch-all
52 (println (error-message-string err))
53 (backtrace)))
54 (setq eof t)
55 ;; print final newline
56 (terpri))))))
57
58(main)