core: One big eval_apply.

* mes.c (eval_apply): New function.
  (eval_apply_t): New type.
  (g_target): New global.
  (vm_evlis, vm_apply_env, vm_eval_env, vm_expand_macro_env,
  vm_begin_env, vm_if_env, vm_call_with_values_env): Remove.  Update callers.
  (macro_expand_env): Rename from expand_macro_env.
* guile/mes.mes: Update callers.
* module/mes/base-0.mes: Likewise.
* module/mes/mes-0.mes: Likewise.
* module/mes/psyntax-1.mes: Likewise.
* module/mes/repl.mes: Likewise.
This commit is contained in:
Jan Nieuwenhuizen 2016-12-22 16:34:28 +01:00
parent cc58cf2e04
commit 471bdb0af4
6 changed files with 62 additions and 54 deletions

View file

@ -123,11 +123,11 @@
(cons %the-unquoters a)) (cons %the-unquoters a))
(define (eval-env e a) (define (eval-env e a)
(eval-expand (expand-macro-env e a) a)) (eval-expand (macro-expand-env e a) a))
(define (expand-macro-env e a) (define (macro-expand-env e a)
(if (pair? e) ((lambda (macro) (if (pair? e) ((lambda (macro)
(if macro (expand-macro-env (apply-env macro (cdr e) a) a) (if macro (macro-expand-env (apply-env macro (cdr e) a) a)
e)) e))
(lookup-macro (car e) a)) (lookup-macro (car e) a))
e)) e))

98
mes.c
View file

@ -105,7 +105,7 @@ scm scm_symbol_unquote = {SYMBOL, "unquote"};
scm scm_symbol_unquote_splicing = {SYMBOL, "unquote-splicing"}; scm scm_symbol_unquote_splicing = {SYMBOL, "unquote-splicing"};
scm scm_symbol_sc_expand = {SYMBOL, "sc-expand"}; scm scm_symbol_sc_expand = {SYMBOL, "sc-expand"};
scm scm_symbol_expand_macro = {SYMBOL, "expand-macro"}; scm scm_symbol_macro_expand = {SYMBOL, "macro-expand"};
scm scm_symbol_sc_expander_alist = {SYMBOL, "*sc-expander-alist*"}; scm scm_symbol_sc_expander_alist = {SYMBOL, "*sc-expander-alist*"};
scm scm_symbol_noexpand = {SYMBOL, "noexpand"}; scm scm_symbol_noexpand = {SYMBOL, "noexpand"};
scm scm_symbol_syntax = {SYMBOL, "syntax"}; scm scm_symbol_syntax = {SYMBOL, "syntax"};
@ -339,15 +339,8 @@ assert_defined (SCM x, SCM e)
return e; return e;
} }
SCM enum eval_apply_t {EVLIS, APPLY, EVAL, MACRO_EXPAND, BEGIN, IF, CALL_WITH_VALUES};
vm_evlis_env () enum eval_apply_t g_target;
{
if (r1 == cell_nil) return cell_nil;
if (TYPE (r1) != PAIR) return eval_env (r1, r0);
r2 = eval_env (car (r1), r0);
r1 = evlis_env (cdr (r1), r0);
return cons (r2, r1);
}
SCM SCM
call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal)) call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
@ -357,12 +350,32 @@ call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
r0 = cl; r0 = cl;
r2 = a; r2 = a;
r3 = aa; r3 = aa;
return vm_begin_env (); g_target = BEGIN;
return eval_apply ();
} }
SCM SCM
vm_apply_env () eval_apply ()
{ {
switch (g_target)
{
case EVLIS: goto evlis;
case APPLY: goto apply;
case EVAL: goto eval;
case MACRO_EXPAND: goto macro_expand;
case BEGIN: goto begin;
case IF: goto label_if;
case CALL_WITH_VALUES: goto call_with_values;
}
evlis:
if (r1 == cell_nil) return cell_nil;
if (TYPE (r1) != PAIR) goto eval; // (r1, r0);
r2 = eval_env (car (r1), r0);
r1 = evlis_env (cdr (r1), r0);
return cons (r2, r1);
apply:
if (TYPE (r1) != PAIR) if (TYPE (r1) != PAIR)
{ {
if (TYPE (r1) == FUNCTION) return call (r1, r2); if (TYPE (r1) == FUNCTION) return call (r1, r2);
@ -412,11 +425,8 @@ vm_apply_env ()
assert (!"cannot apply"); assert (!"cannot apply");
} }
return apply_env (e, r2, r0); return apply_env (e, r2, r0);
}
SCM eval:
vm_eval_env ()
{
switch (TYPE (r1)) switch (TYPE (r1))
{ {
case PAIR: case PAIR:
@ -445,7 +455,7 @@ vm_eval_env ()
} }
#endif #endif
default: { default: {
SCM x = expand_macro_env (r1, r0); SCM x = macro_expand_env (r1, r0);
if (x != r1) if (x != r1)
{ {
if (TYPE (x) == PAIR) if (TYPE (x) == PAIR)
@ -465,11 +475,8 @@ vm_eval_env ()
case SYMBOL: return assert_defined (r1, assq_ref_cache (r1, r0)); case SYMBOL: return assert_defined (r1, assq_ref_cache (r1, r0));
default: return r1; default: return r1;
} }
}
SCM macro_expand:
vm_expand_macro_env ()
{
if (TYPE (CAR (r1)) == STRING && string_to_symbol (CAR (r1)) == cell_symbol_noexpand) if (TYPE (CAR (r1)) == STRING && string_to_symbol (CAR (r1)) == cell_symbol_noexpand)
return cadr (r1); return cadr (r1);
@ -483,17 +490,15 @@ vm_expand_macro_env ()
&& ((expanders = assq_ref_cache (cell_symbol_sc_expander_alist, r0)) != cell_undefined) && ((expanders = assq_ref_cache (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
&& ((macro = assq (CAR (r1), expanders)) != cell_f)) && ((macro = assq (CAR (r1), expanders)) != cell_f))
{ {
SCM sc_expand = assq_ref_cache (cell_symbol_expand_macro, r0); SCM sc_expand = assq_ref_cache (cell_symbol_macro_expand, r0);
if (sc_expand != cell_undefined && sc_expand != cell_f) if (sc_expand != cell_undefined && sc_expand != cell_f)
r1 = apply_env (sc_expand, cons (r1, cell_nil), r0); r1 = apply_env (sc_expand, cons (r1, cell_nil), r0);
} }
return r1; return r1;
}
SCM SCM r;
vm_begin_env () begin:
{ r = cell_unspecified;
SCM r = cell_unspecified;
while (r1 != cell_nil) { while (r1 != cell_nil) {
if (TYPE (r1) == PAIR && TYPE (CAR (r1)) == PAIR) if (TYPE (r1) == PAIR && TYPE (CAR (r1)) == PAIR)
{ {
@ -509,23 +514,19 @@ vm_begin_env ()
r1 = CDR (r1); r1 = CDR (r1);
} }
return r; return r;
}
SCM SCM x;
vm_if_env () label_if:
{ x = eval_env (car (r1), r0);
SCM x = eval_env (car (r1), r0);
if (x != cell_f) if (x != cell_f)
return eval_env (cadr (r1), r0); return eval_env (cadr (r1), r0);
if (cddr (r1) != cell_nil) if (cddr (r1) != cell_nil)
return eval_env (caddr (r1), r0); return eval_env (caddr (r1), r0);
return cell_unspecified; return cell_unspecified;
}
SCM SCM v;
vm_call_with_values_env () call_with_values:
{ v = apply_env (r1, cell_nil, r0);
SCM v = apply_env (r1, cell_nil, r0);
if (TYPE (v) == VALUES) if (TYPE (v) == VALUES)
v = CDR (v); v = CDR (v);
return apply_env (r2, v, r0); return apply_env (r2, v, r0);
@ -593,43 +594,50 @@ vm_call (function0_t f, SCM p1, SCM p2, SCM a)
SCM SCM
evlis_env (SCM m, SCM a) evlis_env (SCM m, SCM a)
{ {
return vm_call (vm_evlis_env, m, cell_undefined, a); g_target = EVLIS;
return vm_call (eval_apply, m, cell_undefined, a);
} }
SCM SCM
apply_env (SCM fn, SCM x, SCM a) apply_env (SCM fn, SCM x, SCM a)
{ {
return vm_call (vm_apply_env, fn, x, a); g_target = APPLY;
return vm_call (eval_apply, fn, x, a);
} }
SCM SCM
eval_env (SCM e, SCM a) eval_env (SCM e, SCM a)
{ {
return vm_call (vm_eval_env, e, cell_undefined, a); g_target = EVAL;
return vm_call (eval_apply, e, cell_undefined, a);
} }
SCM SCM
expand_macro_env (SCM e, SCM a) macro_expand_env (SCM e, SCM a)
{ {
return vm_call (vm_expand_macro_env, e, cell_undefined, a); g_target = MACRO_EXPAND;
return vm_call (eval_apply, e, cell_undefined, a);
} }
SCM SCM
begin_env (SCM e, SCM a) begin_env (SCM e, SCM a)
{ {
return vm_call (vm_begin_env, e, cell_undefined, a); g_target = BEGIN;
return vm_call (eval_apply, e, cell_undefined, a);
} }
SCM SCM
if_env (SCM e, SCM a) if_env (SCM e, SCM a)
{ {
return vm_call (vm_if_env, e, cell_undefined, a); g_target = IF;
return vm_call (eval_apply, e, cell_undefined, a);
} }
SCM SCM
call_with_values_env (SCM producer, SCM consumer, SCM a) call_with_values_env (SCM producer, SCM consumer, SCM a)
{ {
return vm_call (vm_call_with_values_env, producer, consumer, a); g_target = CALL_WITH_VALUES;
return vm_call (eval_apply, producer, consumer, a);
} }
SCM SCM

View file

@ -34,7 +34,7 @@
(define (primitive-eval e) (eval-env e (current-module))) (define (primitive-eval e) (eval-env e (current-module)))
(define eval eval-env) (define eval eval-env)
(define (expand-macro e) (expand-macro-env e (current-module))) (define (macro-expand e) (macro-expand-env e (current-module)))
(define quotient /) (define quotient /)

View file

@ -135,11 +135,11 @@
(cons %the-unquoters a)) (cons %the-unquoters a))
(define (eval-env e a) (define (eval-env e a)
(eval-expand (expand-macro-env e a) a)) (eval-expand (macro-expand-env e a) a))
(define (expand-macro-env e a) (define (macro-expand-env e a)
(if (pair? e) ((lambda (macro) (if (pair? e) ((lambda (macro)
(if macro (expand-macro-env (apply-env macro (cdr e) a) a) (if macro (macro-expand-env (apply-env macro (cdr e) a) a)
e)) e))
(lookup-macro (car e) a)) (lookup-macro (car e) a))
e)) e))

View file

@ -27,4 +27,4 @@
(define datum->syntax datum->syntax-object) (define datum->syntax datum->syntax-object)
(define syntax->datum syntax-object->datum) (define syntax->datum syntax-object->datum)
(set! expand-macro sc-expand) (set! macro-expand sc-expand)

View file

@ -120,7 +120,7 @@ along with Mes. If not, see <http://www.gnu.org/licenses/>.
(display sexp) (display sexp)
(display "]") (display "]")
(newline)) (newline))
(display (expand-macro sexp)) (display (macro-expand sexp))
(newline))) (newline)))
(define (scexpand) (define (scexpand)