Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (= glob (list (obj car (fn (x) (car x))
- cdr (fn (x) (cdr x))
- cons (fn (x y) (cons x y))
- eq (fn (x y) (is x y))
- atom (fn (x) (atom x)))
- nil))
- (def _apply (x args env)
- (if (isa x 'fn)
- (apply x args)
- (isa x 'lambda)
- (let (env parm body) rep.x
- ;; TODO: does this kind of local environment mutation work
- ;; correctly with things like closures?
- (map (fn (name x) (= car.env.name x)) parm args)
- (alet x body
- (if cdr.x
- (do (_eval car.x env)
- (self cdr.x))
- (_eval car.x env))))
- (err "apply expects a function as first argument" x)))
- (def _eval (x (o env glob))
- (if (isa x 'sym)
- (let (env parent) env
- (or env.x (if parent (_eval x parent)
- (err "undefined variable" x))))
- (isa x 'cons)
- (if (caris x 'label)
- (let (n v) cdr.x
- (= car.env n v))
- (caris x 'quote)
- cadr.x
- (caris x 'if)
- (let (test then else) cdr.x
- (if (_eval test env)
- (_eval then env)
- (_eval else env)))
- (caris x 'lambda)
- (let (parm . body) cdr.x
- (annotate 'lambda (list (list (obj) env) parm body)))
- (_apply (_eval car.x env) (map (fn (x) (_eval x env)) cdr.x) env))
- x))
Advertisement
Add Comment
Please, Sign In to add comment