Pauan

Lisp in Arc

Jun 14th, 2012
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.53 KB | None | 0 0
  1. (= glob (list (obj car   (fn (x)   (car x))
  2.                    cdr   (fn (x)   (cdr x))
  3.                    cons  (fn (x y) (cons x y))
  4.                    eq    (fn (x y) (is x y))
  5.                    atom  (fn (x)   (atom x)))
  6.               nil))
  7.  
  8. (def _apply (x args env)
  9.   (if (isa x 'fn)
  10.         (apply x args)
  11.       (isa x 'lambda)
  12.         (let (env parm body) rep.x
  13.           ;; TODO: does this kind of local environment mutation work
  14.           ;;       correctly with things like closures?
  15.           (map (fn (name x) (= car.env.name x)) parm args)
  16.           (alet x body
  17.             (if cdr.x
  18.                 (do (_eval car.x env)
  19.                     (self cdr.x))
  20.                 (_eval car.x env))))
  21.       (err "apply expects a function as first argument" x)))
  22.  
  23. (def _eval (x (o env glob))
  24.   (if (isa x 'sym)
  25.         (let (env parent) env
  26.           (or env.x (if parent (_eval x parent)
  27.                                (err "undefined variable" x))))
  28.       (isa x 'cons)
  29.         (if (caris x 'label)
  30.               (let (n v) cdr.x
  31.                 (= car.env n v))
  32.             (caris x 'quote)
  33.               cadr.x
  34.             (caris x 'if)
  35.               (let (test then else) cdr.x
  36.                 (if (_eval test env)
  37.                     (_eval then env)
  38.                     (_eval else env)))
  39.             (caris x 'lambda)
  40.               (let (parm . body) cdr.x
  41.                 (annotate 'lambda (list (list (obj) env) parm body)))
  42.             (_apply (_eval car.x env) (map (fn (x) (_eval x env)) cdr.x) env))
  43.       x))
Advertisement
Add Comment
Please, Sign In to add comment