Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
tadd committed Jul 17, 2024
1 parent 2c8b74f commit 03a811d
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions lisp.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ static Value map(FuncMapper f, Value l)

static Value environment = Qnil; // alist of ('ident . <value>)

static Value alist_put_or_append(Value l, Value vkey, Value val)
static Value alist_find_or_last(Value l, Value vkey, Value *last)
{
if (!value_is_symbol(vkey))
return Qnil;
Expand All @@ -699,15 +699,32 @@ static Value alist_put_or_append(Value l, Value vkey, Value val)
if (!value_is_symbol(target))
continue;
Symbol sym = value_to_symbol(target);
if (sym == key) {
PAIR(entry)->cdr = val;
return l;
}
if (sym == key)
return entry;
}
if (last)
*last = prev; // may be Qnil
return Qnil;
}

static Value alist_find(Value l, Value vkey)
{
Value entry = alist_find_or_last(l, vkey, NULL);
return cdr(entry);
}

static Value alist_put_or_append(Value l, Value vkey, Value val)
{
Value last;
Value found = alist_find_or_last(l, vkey, &last);
if (found != Qnil) {
PAIR(found)->cdr = val; // put
return l;
}
Value next = cons(cons(vkey, val), Qnil);
if (prev == Qnil)
return next;
PAIR(prev)->cdr = next;
if (last == Qnil)
return next; // create new list
PAIR(last)->cdr = next; // append
return l;
}

Expand All @@ -723,26 +740,6 @@ static Value define_function(const char *name, CFunc cfunc, long arity)
return env_put(name, value_of_func(cfunc, arity));
}

static Value alist_find(Value l, Value vkey)
{
if (!value_is_symbol(vkey))
return Qnil;
Symbol key = value_to_symbol(vkey);
Value p;
for (p = l; p != Qnil; p = cdr(p)) {
Value entry = car(p);
if (!value_is_pair(entry))
continue;
Value target = car(entry);
if (!value_is_symbol(target))
continue;
Symbol sym = value_to_symbol(target);
if (sym == key)
return cdr(entry);
}
return Qnil;
}

static Value lookup(Value name)
{
Value f = alist_find(environment, name);
Expand Down

0 comments on commit 03a811d

Please sign in to comment.