Browse Source

Simplify variable lookup and implicit variable declarations.

master
Ian Piumarta 11 months ago
parent
commit
4622db9d34
1 changed files with 11 additions and 18 deletions
  1. +11
    -18
      minproto.leg

+ 11
- 18
minproto.leg View File

@ -1,6 +1,6 @@
# minproto.leg -- minimal prototype langauge for semantic experiments # minproto.leg -- minimal prototype langauge for semantic experiments
# #
# last edited: 2024-05-29 11:05:04 by piumarta on zora
# last edited: 2024-05-29 14:20:52 by piumarta on zora
%{ %{
; ;
@ -784,24 +784,21 @@ oop Object_get(oop obj, oop key)
oop *_refvar(oop obj, oop key) oop *_refvar(oop obj, oop key)
{ {
while (is(Object, obj)) {
while (is(Object, obj)) { // look for a binding of key in the local scopes
ssize_t ind = Object_find(obj, key); ssize_t ind = Object_find(obj, key);
if (ind >= 0) return &_get(obj, Object,properties)[ind].val; if (ind >= 0) return &_get(obj, Object,properties)[ind].val;
obj = _getDelegate(obj); obj = _getDelegate(obj);
} }
int numspaces = _get(namespaces, Object,isize); int numspaces = _get(namespaces, Object,isize);
if (numspaces) {
if (numspaces) { // look for a binding of key in the namespace stack
oop *nss = _get(namespaces, Object,indexed); oop *nss = _get(namespaces, Object,indexed);
for (int i = numspaces; i--;) { for (int i = numspaces; i--;) {
oop ns = nss[i]; oop ns = nss[i];
while (is(Object, ns)) {
ssize_t ind = Object_find(ns, key);
if (ind >= 0) return &_get(ns, Object,properties)[ind].val;
ns = _getDelegate(ns);
}
ssize_t ind = Object_find(ns, key);
if (ind >= 0) return &_get(ns, Object,properties)[ind].val;
} }
} }
oop *ref = &_get(key, Symbol,value); // asserts is(Symbol,key)
oop *ref = &_get(key, Symbol,value); // use the global binding
return ref; return ref;
} }
@ -822,18 +819,14 @@ oop Object_put(oop obj, oop key, oop val);
oop setvar(oop obj, oop key, oop val) oop setvar(oop obj, oop key, oop val)
{ {
oop env = obj; oop env = obj;
while (is(Object, env)) {
while (is(Object, env)) { // look for a binding of key in the local scopes
ssize_t ind = Object_find(env, key); ssize_t ind = Object_find(env, key);
if (ind >= 0) return _get(env, Object,properties)[ind].val = val;
if (ind >= 0) return _get(env, Object,properties)[ind].val = val; // set it
env = _getDelegate(env); env = _getDelegate(env);
} }
int numspaces = _get(namespaces, Object,isize);
if (numspaces) {
oop *nss = _get(namespaces, Object,indexed);
return Object_put(nss[numspaces - 1], key, val);
}
if (nil != obj) return Object_put(obj, key, val);
return _get(key, Symbol,value) = val; // asserts is(Symbol,key)
return (nil == obj) // are we at the top level?
? _get(key, Symbol,value) = val // set a global variable
: Object_put(obj, key, val); // create a new local variable
} }
oop Object_put(oop obj, oop key, oop val) oop Object_put(oop obj, oop key, oop val)

Loading…
Cancel
Save