]> git.lizzy.rs Git - nothing.git/blob - src/script/scope.c
(#312) implement set_scope_value
[nothing.git] / src / script / scope.c
1 #include "./scope.h"
2
3 static struct Expr find_frame_with_name(struct Expr scope, struct Expr name)
4 {
5     (void) name;
6
7     /* TODO: find_frame_with_name is not implemented */
8     return scope;
9 }
10
11 struct Expr empty_scope(void)
12 {
13     return CONS(NIL, NIL);      /* '(()) */
14 }
15
16 struct Expr get_scope_value(struct Expr scope, struct Expr name)
17 {
18     switch (scope.type) {
19     case EXPR_CONS: {
20         struct Expr value = assoc(name, scope.cons->car);
21         return nil_p(value) ? get_scope_value(scope.cons->cdr, name) : value;
22     } break;
23
24     default:
25         return scope;
26     }
27 }
28
29 void set_scope_value(struct Expr scope, struct Expr name, struct Expr value)
30 {
31     struct Expr frame = find_frame_with_name(scope, name);
32     push(CONS(name, value), frame);
33 }
34
35 struct Expr push_scope_frame(struct Expr scope)
36 {
37     return CONS(empty_scope(), scope);
38 }
39
40 struct Expr pop_scope_frame(struct Expr scope)
41 {
42     if (scope.type == EXPR_CONS) {
43         return scope.cons->cdr;
44     } else {
45         return scope;
46     }
47 }