fix for set! inside lambda.

This commit is contained in:
Jan Nieuwenhuizen 2016-07-10 13:45:54 +02:00
parent f373e5ae76
commit 1da953b6ab
3 changed files with 25 additions and 34 deletions

38
mes.c
View file

@ -191,9 +191,6 @@ quasiquote (scm *x)
{ {
return cons (&scm_symbol_quasiquote, x); return cons (&scm_symbol_quasiquote, x);
} }
scm *eval_quasiquote (scm *, scm *);
#endif #endif
//Library functions //Library functions
@ -210,8 +207,6 @@ scm *cadar (scm *x) {return car (cdr (car (x)));}
scm *cddar (scm *x) {return cdr (cdr (car (x)));} scm *cddar (scm *x) {return cdr (cdr (car (x)));}
scm *cdddr (scm *x) {return cdr (cdr (cdr (x)));} scm *cdddr (scm *x) {return cdr (cdr (cdr (x)));}
scm* make_atom (char const *);
scm * scm *
pairlis (scm *x, scm *y, scm *a) pairlis (scm *x, scm *y, scm *a)
{ {
@ -244,21 +239,12 @@ assoc (scm *x, scm *a)
return assoc (x, cdr (a)); return assoc (x, cdr (a));
} }
scm *apply (scm*, scm*, scm*);
scm *eval_ (scm*, scm*);
scm *apply_ (scm*, scm*, scm*);
scm * scm *
eval_quote (scm *fn, scm *x) eval_quote (scm *fn, scm *x)
{ {
return apply (fn, x, &scm_nil); return apply (fn, x, &scm_nil);
} }
scm *builtin_p (scm*);
scm *call (scm *, scm*);
scm *display (scm*);
scm *newline ();
scm * scm *
apply_ (scm *fn, scm *x, scm *a) apply_ (scm *fn, scm *x, scm *a)
{ {
@ -277,22 +263,22 @@ apply_ (scm *fn, scm *x, scm *a)
return call (fn, x); return call (fn, x);
return apply (eval (fn, a), x, a); return apply (eval (fn, a), x, a);
} }
else if (car (fn) == &scm_lambda) { else if (car (fn) == &scm_lambda)
scm *body = cddr (fn); return begin_env (cddr (fn), pairlis (cadr (fn), x, a));
scm *ca = cadr (fn);
scm *ax = pairlis (cadr (fn), x, a);
scm *result = eval (car (body), ax);
if (cdr (body) == &scm_nil)
return result;
return apply (cons (car (fn), cons (cadr (fn), cdddr (fn))), x, ax);
}
else if (car (fn) == &scm_label) else if (car (fn) == &scm_label)
return apply (caddr (fn), x, cons (cons (cadr (fn), caddr (fn)), a)); return apply (caddr (fn), x, cons (cons (cadr (fn), caddr (fn)), a));
return &scm_unspecified; return &scm_unspecified;
} }
scm *evcon (scm*, scm*); scm *
scm *evlis (scm*, scm*); begin_env (scm *body, scm *a)
{
if (body == &scm_nil) return &scm_unspecified;
scm *result = eval (car (body), a);
if (cdr (body) == &scm_nil)
return result;
return begin_env (cdr (body), a);
}
scm * scm *
eval_ (scm *e, scm *a) eval_ (scm *e, scm *a)
@ -638,8 +624,6 @@ readblock (int c)
return readblock (getchar ()); return readblock (getchar ());
} }
scm *readlis (scm *a);
scm * scm *
readword (int c, char* w, scm *a) readword (int c, char* w, scm *a)
{ {

View file

@ -98,13 +98,7 @@
(call fn x)) (call fn x))
(#t (apply (eval fn a) x a)))) (#t (apply (eval fn a) x a))))
((eq? (car fn) 'lambda) ((eq? (car fn) 'lambda)
(cond ((null? (cdr (cddr fn))) (begin-env (cddr fn) (pairlis (cadr fn) x a)))
(eval (caddr fn) (pairlis (cadr fn) x a)))
(#t
(eval (caddr fn) (pairlis (cadr fn) x a))
(apply (cons (car fn) (cons (cadr fn) (cdddr fn)))
x
(pairlis (cadr fn) x a)))))
((eq? (car fn) 'label) (apply (caddr fn) x (cons (cons (cadr fn) ((eq? (car fn) 'label) (apply (caddr fn) x (cons (cons (cadr fn)
(caddr fn)) a))))) (caddr fn)) a)))))

View file

@ -115,6 +115,19 @@
(display a) (display a)
(newline) (newline)
(display
((lambda (x)
(display 'x:)
(display x)
(newline)
(display 'setting-x=2)
(newline)
(set! x 2)
(display 'x:)
(display x)
(newline))
1))
(display (+ 11 12)) (display (+ 11 12))
(newline) (newline)
(display (* 3 3)) (display (* 3 3))