]> git.lizzy.rs Git - nothing.git/blob - src/script/scope.c
Merge pull request #320 from tsoding/318
[nothing.git] / src / script / scope.c
1 #include "./scope.h"
2
3 struct Expr get_scope_value(struct Expr scope, struct Expr name)
4 {
5     if (cons_p(scope)) {
6         struct Expr value = assoc(name, scope.cons->car);
7         return nil_p(value) ? get_scope_value(scope.cons->cdr, name) : value;
8     }
9
10     return scope;
11 }
12
13 struct Expr set_scope_value(Gc *gc, struct Expr scope, struct Expr name, struct Expr value)
14 {
15     if (cons_p(scope)) {
16         if (!nil_p(assoc(name, scope.cons->car)) || nil_p(scope.cons->cdr)) {
17             return CONS(gc,
18                         CONS(gc, CONS(gc, name, value), scope.cons->car),
19                         scope.cons->cdr);
20         } else {
21             return CONS(gc,
22                         scope.cons->car,
23                         set_scope_value(gc, scope.cons->cdr, name, value));
24         }
25     } else {
26         return CONS(gc,
27                     CONS(gc, CONS(gc, name, value), NIL(gc)),
28                     scope);
29     }
30 }