4 static struct Expr get_scope_value_impl(struct Expr scope, struct Expr name)
7 struct Expr value = assoc(name, scope.cons->car);
8 return nil_p(value) ? get_scope_value_impl(scope.cons->cdr, name) : value;
14 struct Expr get_scope_value(const struct Scope *scope, struct Expr name)
16 return get_scope_value_impl(scope->expr, name);
19 static struct Expr set_scope_value_impl(Gc *gc, struct Expr scope, struct Expr name, struct Expr value)
22 if (!nil_p(assoc(name, scope.cons->car)) || nil_p(scope.cons->cdr)) {
24 CONS(gc, CONS(gc, name, value), scope.cons->car),
29 set_scope_value_impl(gc, scope.cons->cdr, name, value));
33 CONS(gc, CONS(gc, name, value), NIL(gc)),
38 struct Scope create_scope(Gc *gc)
40 struct Scope scope = {
41 .expr = CONS(gc, NIL(gc), NIL(gc))
46 void set_scope_value(Gc *gc, struct Scope *scope, struct Expr name, struct Expr value)
48 scope->expr = set_scope_value_impl(gc, scope->expr, name, value);
51 void push_scope_frame(Gc *gc, struct Scope *scope, struct Expr vars, struct Expr args)
56 struct Expr frame = NIL(gc);
58 while(!nil_p(vars) && !nil_p(args)) {
60 CONS(gc, vars.cons->car, args.cons->car),
62 vars = vars.cons->cdr;
63 args = args.cons->cdr;
66 scope->expr = CONS(gc, frame, scope->expr);
69 void pop_scope_frame(Gc *gc, struct Scope *scope)
74 if (!nil_p(scope->expr)) {
75 scope->expr = scope->expr.cons->cdr;