2 ;; "Mu Lei" known as "NalaGinrut" <NalaGinrut@gmail.com>
3 ;; This file is free software: you can redistribute it and/or modify
4 ;; it under the terms of the GNU General Public License as published by
5 ;; the Free Software Foundation, either version 3 of the License, or
6 ;; (at your option) any later version.
8 ;; This file is distributed in the hope that it will be useful,
9 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ;; GNU General Public License for more details.
13 ;; You should have received a copy of the GNU General Public License
14 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
16 (import (readline) (reader) (printer) (ice-9 match) (srfi srfi-43))
27 (define (eval_ast ast env)
28 (define (_eval x) (EVAL x env))
31 (or (assoc-ref env sym)
32 (throw 'mal-error (format #f "'~a' not found" sym))))
33 ((? list? lst) (map _eval lst))
34 ((? vector? vec) (vector-map (lambda (i x) (_eval x)) vec))
36 (hash-for-each (lambda (k v) (hash-set! ht k (_eval v))) ht)
40 (define (EVAL ast env)
44 (let ((el (eval_ast ast env)))
45 (apply (car el) (cdr el))))
46 (else (eval_ast ast env))))
49 (and (not (eof-object? exp))
50 (format #t "~a~%" (pr_str exp #t))))
52 (define (LOOP continue?)
53 (and continue? (REPL)))
57 (let ((line (_readline "user> ")))
59 ((eof-object? line) #f)
60 ((string=? line "") #t)
63 (lambda () (PRINT (EVAL (READ line) *toplevel*)))
65 (format #t "Error: ~a~%" (pr_str (car e) #t)))))))))