From d1a089caed8d043e70b73e7a5f44756b83937e8c Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sat, 16 Jul 2016 22:43:13 +0200 Subject: [PATCH] mes.c: move define, define-macro into begin_env, remove loop. fixes inner defines. --- loop2.mes | 2 +- mes.c | 82 +++++++++++++++++++++++++++---------------------------- 2 files changed, 41 insertions(+), 43 deletions(-) diff --git a/loop2.mes b/loop2.mes index a61e4076..b73fd801 100644 --- a/loop2.mes +++ b/loop2.mes @@ -50,4 +50,4 @@ (loop2 (set-cdr! (assq (cadr e) a) (eval (caddr e) a)) (readenv a) a)) (#t (loop2 (eval e a) (readenv a) a)))) -EOF +'EOF diff --git a/mes.c b/mes.c index 50b5cd25..bcef80a0 100644 --- a/mes.c +++ b/mes.c @@ -35,12 +35,10 @@ #define DEBUG 0 +#define BOOT 1 #define MACROS 1 #define QUASIQUOTE 1 - -#ifndef QUOTE_SUGAR #define QUOTE_SUGAR 1 -#endif enum type {CHAR, NUMBER, PAIR, STRING, SYMBOL, VALUES, VECTOR, FUNCTION0, FUNCTION1, FUNCTION2, FUNCTION3, FUNCTIONn}; @@ -293,16 +291,6 @@ apply_env_ (scm *fn, scm *x, scm *a) return &scm_unspecified; } -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 * eval_ (scm *e, scm *a) { @@ -1200,42 +1188,43 @@ define_macro (scm *x, scm *a) } scm * -loop (scm *r, scm *e, scm *a) +begin_env (scm *body, scm *a) { -#if 0//DEBUG - printf ("\nc:loop e="); + if (body == &scm_nil) return &scm_unspecified; + scm *e = car (body); +#if DEBUG + printf ("\nc:begin_env e="); display (e); puts (""); #endif - if (e == &scm_nil) - return r; - else if (eq_p (e, &scm_symbol_EOF) == &scm_t) - return apply_env (cdr (assq (&scm_symbol_loop2, a)), - cons (&scm_unspecified, cons (&scm_t, cons (a, &scm_nil))), a); - else if (eq_p (e, &scm_symbol_EOF2) == &scm_t) - return r; - else if (atom_p (e) == &scm_t) - return loop (eval (e, a), readenv (a), a); - else if (eq_p (car (e), &scm_symbol_define) == &scm_t) - return loop (&scm_unspecified, - readenv (a), - cons (define (e, a), a)); - else if (eq_p (car (e), &scm_symbol_define_macro) == &scm_t) - return loop (&scm_unspecified, - readenv (a), - cons (define_macro (e, a), a)); - else if (eq_p (car (e), &scm_symbol_set_x) == &scm_t) - return loop (set_env_x (cadr (e), eval (caddr (e), a), a), readenv (a), a); - return loop (eval (e, a), readenv (a), a); + if (e->type == PAIR) { + if (eq_p (car (e), &scm_symbol_define) == &scm_t) + return begin_env (cdr (body), cons (define (e, a), a)); + else if (eq_p (car (e), &scm_symbol_define_macro) == &scm_t) + return begin_env (cdr (body), cons (define_macro (e, a), a)); + else if (eq_p (car (e), &scm_symbol_set_x) == &scm_t) { + set_env_x (cadr (e), eval (caddr (e), a), a); + return begin_env (cdr (e), a); + } +#if BOOT + else if (eq_p (e, &scm_symbol_EOF) == &scm_t) + return apply_env (cdr (assq (&scm_symbol_loop2, a)), + cons (&scm_unspecified, cons (&scm_t, cons (a, &scm_nil))), a); + else if (eq_p (e, &scm_symbol_EOF2) == &scm_t) + return make_symbol ("exit boot"); +#endif + } + scm *result = eval (e, a); + if (cdr (body) == &scm_nil) + return result; + return begin_env (cdr (body), a); } -int -main (int argc, char *argv[]) +scm * +read_file (scm *e, scm *a) { - scm *a = mes_environment (); - display (loop (&scm_unspecified, readenv (a), a)); - newline (); - return 0; + if (e == &scm_nil) return e; + return cons (e, read_file (readenv (a), a)); } scm * @@ -1275,3 +1264,12 @@ eval (scm *e, scm *a) evalling_p = false; return r; } + +int +main (int argc, char *argv[]) +{ + scm *a = mes_environment (); + display (begin_env (read_file (readenv (a), a), a)); + newline (); + return 0; +}