core: Refactor name resolution.
* src/module.c (module_variable): Do not search local bindings. (module_ref): Remove function. * src/eval-apply.c (lookup_handle): New function. (lookup_value): New function. (set_env_x, expand_variable_, eval_apply): Use 'lookup-handle' and 'lookup-value' in place of 'module_variable' and 'module_ref' respectively. * src/core.c (error): Likewise. * include/mes/mes.h (lookup_handle, lookup_value): Add declarations. * include/mes/builtins.h (module_ref): Remove declaration. * src/builtins.c (mes_builtins): Remove "module-ref". Co-authored-by: Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
This commit is contained in:
parent
895eaa4c5e
commit
870256ed3e
|
@ -104,7 +104,6 @@ struct scm *ash (struct scm *n, struct scm *count);
|
|||
struct scm *make_module_type ();
|
||||
struct scm *module_printer (struct scm *module);
|
||||
struct scm *module_variable (struct scm *module, struct scm *name);
|
||||
struct scm *module_ref (struct scm *module, struct scm *name);
|
||||
struct scm *module_define_x (struct scm *module, struct scm *name, struct scm *value);
|
||||
/* src/posix.c */
|
||||
struct scm *abort_ ();
|
||||
|
|
|
@ -130,6 +130,8 @@ struct scm *cell_ref (struct scm *cell, long index);
|
|||
struct scm *fdisplay_ (struct scm *, int, int);
|
||||
struct scm *init_symbols ();
|
||||
struct scm *init_time (struct scm *a);
|
||||
struct scm *lookup_handle (struct scm *name);
|
||||
struct scm *lookup_value (struct scm *name);
|
||||
struct scm *make_builtin_type ();
|
||||
struct scm *make_bytes (char const *s, size_t length);
|
||||
struct scm *make_cell (long type, struct scm *car, struct scm *cdr);
|
||||
|
|
|
@ -214,7 +214,6 @@ mes_builtins (struct scm *a) /*:((internal)) */
|
|||
a = init_builtin (builtin_type, "make-module-type", 0, &make_module_type, a);
|
||||
a = init_builtin (builtin_type, "module-printer", 1, &module_printer, a);
|
||||
a = init_builtin (builtin_type, "module-variable", 2, &module_variable, a);
|
||||
a = init_builtin (builtin_type, "module-ref", 2, &module_ref, a);
|
||||
a = init_builtin (builtin_type, "module-define!", 3, &module_define_x, a);
|
||||
/* src/posix.c */
|
||||
a = init_builtin (builtin_type, "abort", 0, &abort_, a);
|
||||
|
|
|
@ -149,7 +149,7 @@ struct scm *
|
|||
error (struct scm *key, struct scm *x)
|
||||
{
|
||||
#if !__MESC_MES__ && !__M2_PLANET__
|
||||
struct scm *throw = module_ref (R0, cell_symbol_throw);
|
||||
struct scm *throw = lookup_value (cell_symbol_throw);
|
||||
if (throw != cell_undefined)
|
||||
return apply (throw, cons (key, cons (x, cell_nil)), R0);
|
||||
#endif
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
/* -*-comment-start: "//";comment-end:""-*-
|
||||
* GNU Mes --- Maxwell Equations of Software
|
||||
* Copyright © 2016,2017,2018,2019,2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
||||
* Copyright © Timothy Sample 2022 <samplet@ngyro.com>
|
||||
*
|
||||
* This file is part of GNU Mes.
|
||||
*
|
||||
|
@ -127,7 +128,7 @@ set_env_x (struct scm *x, struct scm *e, struct scm *a)
|
|||
if (x->type == TBINDING)
|
||||
p = x->binding;
|
||||
else
|
||||
p = assert_defined (x, module_variable (a, x));
|
||||
p = assert_defined (x, lookup_handle (x));
|
||||
if (p->type != TPAIR)
|
||||
error (cell_symbol_not_a_pair, cons (p, x));
|
||||
return set_cdr_x (p, e);
|
||||
|
@ -193,6 +194,26 @@ push_cc (struct scm *p1, struct scm *p2, struct scm *a, struct scm *c) /*:((int
|
|||
return cell_unspecified;
|
||||
}
|
||||
|
||||
struct scm *
|
||||
lookup_handle (struct scm *name)
|
||||
{
|
||||
struct scm *handle = assq (name, R0);
|
||||
if (handle == cell_f)
|
||||
{
|
||||
handle = module_variable (M0, name);
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
struct scm *
|
||||
lookup_value (struct scm *name)
|
||||
{
|
||||
struct scm *handle = lookup_handle (name);
|
||||
if (handle != cell_f)
|
||||
return handle->cdr;
|
||||
return cell_undefined;
|
||||
}
|
||||
|
||||
struct scm *
|
||||
add_formals (struct scm *formals, struct scm *x)
|
||||
{
|
||||
|
@ -275,7 +296,7 @@ expand_variable_ (struct scm *x, struct scm *formals, int top_p) /*:((int
|
|||
&& a != cell_symbol_primitive_load
|
||||
&& formal_p (x->car, formals) == 0)
|
||||
{
|
||||
v = module_variable (R0, a);
|
||||
v = lookup_handle (a);
|
||||
if (v != cell_f)
|
||||
x->car = make_binding_ (v);
|
||||
}
|
||||
|
@ -630,7 +651,7 @@ eval:
|
|||
}
|
||||
else
|
||||
{
|
||||
entry = module_variable (R0, name);
|
||||
entry = lookup_handle (name);
|
||||
if (entry == cell_f)
|
||||
module_define_x (M0, name, cell_f);
|
||||
}
|
||||
|
@ -667,7 +688,7 @@ eval:
|
|||
}
|
||||
else if (global_p != 0)
|
||||
{
|
||||
entry = module_variable (R0, name);
|
||||
entry = lookup_handle (name);
|
||||
set_cdr_x (entry, R1);
|
||||
}
|
||||
else
|
||||
|
@ -676,7 +697,7 @@ eval:
|
|||
aa = cons (entry, cell_nil);
|
||||
set_cdr_x (aa, cdr (R0));
|
||||
set_cdr_x (R0, aa);
|
||||
cl = module_variable (R0, cell_closure);
|
||||
cl = lookup_handle (cell_closure);
|
||||
set_cdr_x (cl, aa);
|
||||
}
|
||||
R1 = cell_unspecified;
|
||||
|
@ -703,7 +724,7 @@ eval:
|
|||
goto vm_return;
|
||||
if (R1 == cell_symbol_call_with_current_continuation)
|
||||
goto vm_return;
|
||||
R1 = assert_defined (R1, module_ref (R0, R1));
|
||||
R1 = assert_defined (R1, lookup_value (R1));
|
||||
goto vm_return;
|
||||
}
|
||||
else if (t == TBINDING)
|
||||
|
@ -777,13 +798,13 @@ macro_expand:
|
|||
macro = macro_get_handle (cell_symbol_portable_macro_expand);
|
||||
if (macro != cell_f)
|
||||
{
|
||||
expanders = module_ref (R0, cell_symbol_sc_expander_alist);
|
||||
expanders = lookup_value (cell_symbol_sc_expander_alist);
|
||||
if (expanders != cell_undefined)
|
||||
{
|
||||
macro = assq (R1->car, expanders);
|
||||
if (macro != cell_f)
|
||||
{
|
||||
sc_expand = module_ref (R0, cell_symbol_macro_expand);
|
||||
sc_expand = lookup_value (cell_symbol_macro_expand);
|
||||
R2 = R1;
|
||||
if (sc_expand != cell_undefined && sc_expand != cell_f)
|
||||
{
|
||||
|
|
22
src/module.c
22
src/module.c
|
@ -86,25 +86,9 @@ module_printer (struct scm *module)
|
|||
struct scm *
|
||||
module_variable (struct scm *module, struct scm *name)
|
||||
{
|
||||
/*struct scm *locals = struct_ref_ (module, 3);*/
|
||||
struct scm *locals = module;
|
||||
struct scm *x = assq (name, locals);
|
||||
if (x == cell_f)
|
||||
{
|
||||
module = M0;
|
||||
struct scm *globals = struct_ref_ (module, 5);
|
||||
x = hashq_get_handle (globals, name);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
struct scm *
|
||||
module_ref (struct scm *module, struct scm *name)
|
||||
{
|
||||
struct scm *x = module_variable (module, name);
|
||||
if (x == cell_f)
|
||||
return cell_undefined;
|
||||
return x->cdr;
|
||||
module = M0;
|
||||
struct scm *globals = struct_ref_ (module, 5);
|
||||
return hashq_get_handle (globals, name);
|
||||
}
|
||||
|
||||
struct scm *
|
||||
|
|
Loading…
Reference in New Issue