|
18 | 18 | (defun eval-val-1 (v s) (if (eq v NOT-FOUND) (eval-gval s) v))
|
19 | 19 | (defun eval-val (s) (eval-val-1 (table-get val-table s) s))
|
20 | 20 |
|
21 |
| -(defun eval-if (o s) (if (eq o if) (run-if s) (eval-defun o s))) |
| 21 | +(defun eval-if (o s) (if (eq o if) (run-if s) (eval-defmacro o s))) |
| 22 | +(defun eval-defmacro (o s) (if (eq o defmacro) (run-defmacro s) (eval-defun o s))) |
22 | 23 | (defun eval-defun (o s) (if (eq o defun) (run-defun s) (eval-lambda o s)))
|
23 | 24 | (defun eval-lambda (o s) (if (eq o lambda) (run-lambda s) (eval-quote o s)))
|
24 | 25 | (defun eval-quote (o s) (if (eq o quote) (run-quote s) (eval-define o s)))
|
25 |
| -(defun eval-define (o s) (if (eq o define) (run-define s) (eval-func (eval o) s))) |
| 26 | +(defun eval-define (o s) (if (eq o define) (run-define s) (eval-macro (eval o) s))) |
| 27 | +(defun is-macro (o) (if (atom o) nil (eq (car o) macro))) |
| 28 | +(defun eval-macro (o s) (if (is-macro o) (run-macro (cdr o) s) (eval-func o s))) |
26 | 29 | (defun is-func (o) (if (atom o) nil (eq (car o) lambda)))
|
27 | 30 | (defun eval-func (o s) (if (is-func o) (run-func (cdr o) s) (eval-add o s)))
|
28 | 31 | (defun eval-add (o s) (if (eq o +) (run-add s) (eval-sub o s)))
|
|
40 | 43 | (defun undefined-func (o) (print (cons o (quote (undefined func)))))
|
41 | 44 |
|
42 | 45 | (defun run-if (s) (if (eval (car s)) (eval (cadr s)) (eval (car (cddr s)))))
|
| 46 | +(defun run-defmacro (s) (gval-table-add (car s) (cons macro (cdr s)))) |
43 | 47 | (defun run-defun (s) (gval-table-add (car s) (run-lambda (cdr s))))
|
44 | 48 | (defun run-lambda (s) (cons lambda s))
|
45 | 49 | (defun run-quote (s) (car s))
|
|
50 | 54 |
|
51 | 55 | (defun run-func-2 (a b c) b)
|
52 | 56 | (defun run-func-1 (v l s) (run-func-2 (set-vals (car l) s) (eval (cadr l)) (define val-table v)))
|
53 |
| -;(defun run-func-1 (v l s) (run-func-2 (set-vals (car l) s) (eval (cadr l)) (define val-table v))) |
54 | 57 | (defun run-func (l s) (run-func-1 val-table l s))
|
55 | 58 |
|
| 59 | +(defun create-val-table-m (a p) (if a (cons (car a) (cons (car p) (create-val-table-m (cdr a) (cdr p)))) ())) |
| 60 | +(defun set-vals-m (a p) (define val-table (create-val-table-m a p))) |
| 61 | + |
| 62 | +(defun run-macro-2 (a b c) b) |
| 63 | +(defun run-macro-1 (v l s) (run-macro-2 (set-vals-m (car l) s) (eval (cadr l)) (define val-table v))) |
| 64 | + |
| 65 | +(defun run-macro (l s) (eval (run-macro-1 val-table l s))) |
| 66 | + |
56 | 67 | (defun run-add (s) (+ (eval (car s)) (eval (cadr s))))
|
57 | 68 | (defun run-sub (s) (- (eval (car s)) (eval (cadr s))))
|
58 | 69 | (defun run-mul (s) (* (eval (car s)) (eval (cadr s))))
|
|
122 | 133 | ;(eval (quote (defun func2 (b) (+ (func) b))))
|
123 | 134 | ;(eval (quote (defun func2 (b) (+ b (func 99)))))
|
124 | 135 | ;(eval (quote (func2 42)))
|
| 136 | + |
| 137 | +(eval (quote (defmacro let (l e) (cons (cons lambda (cons (cons (car l) nil) (cons e nil))) (cons (car (cdr l)) nil))))) |
| 138 | +(eval (quote (let (x 42) x))) |
0 commit comments