-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.c
71 lines (59 loc) · 1.64 KB
/
env.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <string.h>
#include "env.h"
#include "error.h"
#include "lexeme.h"
#include "types.h"
Lexeme *insertEnv(Lexeme *env, Lexeme *var, Lexeme *val) {
Lexeme *table = car(env);
setCar(table, cons(CONS, var, car(table)));
setCdr(table, cons(CONS, val, cdr(table)));
return val;
}
Lexeme *lookupEnv(Lexeme *env, Lexeme *var) {
if (var->type == ID_LIST) {
printf("WTF LOOKING UP ID LIST\n");
}
while (env != NULL) {
Lexeme *table = car(env);
Lexeme *vars = car(table);
Lexeme *vals = cdr(table);
while (vars != NULL) {
if (!strcmp(var->sval, car(vars)->sval)) {
return car(vals);
}
vars = cdr(vars);
vals = cdr(vals);
}
env = cdr(env);
}
fatalError("%s is undefined.\n", var->sval);
return NULL;
}
Lexeme *updateEnv(Lexeme *env, Lexeme *var, Lexeme *val) {
while (env != NULL) {
Lexeme *table = car(env);
Lexeme *vars = car(table);
Lexeme *vals = cdr(table);
while (vars != NULL) {
if (!strcmp(var->sval, car(vars)->sval)) {
setCar(vals, val);
return car(vals);
}
vars = cdr(vars);
vals = cdr(vals);
}
env = cdr(env);
}
fatalError("%s is undefined.\n", var->sval);
return NULL;
}
Lexeme *extendEnv(Lexeme *env, Lexeme *vars, Lexeme *vals) {
return cons(ENV, makeTable(vars, vals), env);
}
Lexeme *createEnv() {
return extendEnv(NULL, NULL, NULL);
}
Lexeme *makeTable(Lexeme *vars, Lexeme *vals) {
return cons(TABLE, vars, vals);
}