diff --git a/.gitignore b/.gitignore index ee080b2..95bab60 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .direnv .ccls-cache +a.out +*.o diff --git a/c1/a.out b/c1/a.out deleted file mode 100755 index c723551..0000000 Binary files a/c1/a.out and /dev/null differ diff --git a/c1/compile_commands.json b/c1/compile_commands.json index d059e99..0e1f63e 100644 --- a/c1/compile_commands.json +++ b/c1/compile_commands.json @@ -8,35 +8,5 @@ ], "directory": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1", "file": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1/main.c" - }, - { - "arguments": [ - "/nix/store/k8mrxviw965lv59hqbln3297jwfffm2w-clang-wrapper-16.0.6/bin/clang", - "-g", - "-c", - "prog1.c" - ], - "directory": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1", - "file": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1/prog1.c" - }, - { - "arguments": [ - "/nix/store/k8mrxviw965lv59hqbln3297jwfffm2w-clang-wrapper-16.0.6/bin/clang", - "-g", - "-c", - "slp.c" - ], - "directory": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1", - "file": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1/slp.c" - }, - { - "arguments": [ - "/nix/store/k8mrxviw965lv59hqbln3297jwfffm2w-clang-wrapper-16.0.6/bin/clang", - "-g", - "-c", - "util.c" - ], - "directory": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1", - "file": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1/util.c" } ] diff --git a/c1/main.c b/c1/main.c index 215946d..74a7959 100644 --- a/c1/main.c +++ b/c1/main.c @@ -1,7 +1,9 @@ #include #include +#include #include "prog1.h" +#include "main.h" int maxargs(A_stm statement) { @@ -31,9 +33,88 @@ int maxargs(A_stm statement) return 0; } +Table_ interpStm(A_stm stm, Table_ t) { + if (stm->kind == A_compoundStm) { + t = interpStm(stm->u.compound.stm1, t); + t = interpStm(stm->u.compound.stm2, t); + } else if (stm->kind == A_assignStm) { + IntAndTable_ it = interpExp(stm->u.assign.exp, t); + t = Table(stm->u.assign.id, it->i, it->t); + } else { + A_expList expList = stm->u.print.exps; + while (expList->kind != A_lastExpList) { + t = interpExp(expList->u.pair.head, t)->t; + expList = expList->u.pair.tail; + } + t = interpExp(expList->u.last, t)->t; + } + + return t; +} + +IntAndTable_ interpExp(A_exp e, Table_ t) { + if (e->kind == A_idExp) { + return IntAndTable(lookup(t, e->u.id), t); + } else if (e->kind == A_numExp) { + return IntAndTable(e->u.num, t); + } else if (e->kind == A_opExp) { + IntAndTable_ left = interpExp(e->u.op.left, t); + IntAndTable_ right = interpExp(e->u.op.right, t); + A_binop op = e->u.op.oper; + + switch (op) { + case A_plus: + return IntAndTable(left->i + right->i, t); + case A_minus: + return IntAndTable(left->i - right->i, t); + case A_times: + return IntAndTable(left->i * right->i, t); + case A_div: + return IntAndTable(left->i / right->i, t); + } + } else { + t = interpStm(e->u.eseq.stm, t); + return interpExp(e->u.eseq.exp, t); + } +} + +IntAndTable_ IntAndTable(int i, Table_ t) { + IntAndTable_ it = checked_malloc(sizeof(*it)); + it->i = i; + it->t = t; + return it; +} + +Table_ Table(string id, int value, struct table *tail) { + Table_ t = checked_malloc(sizeof(*t)); + t->id = id; + t->value = value; + t->tail = tail; + return t; +} + +int lookup(Table_ t, string key) { + if (t == NULL) { + // HACK: should indicate a null value + return 0; + } + + if (strcmp(t->id, key) == 0) { + return t->value; + } + + return lookup(t->tail, key); +} + int main() { A_stm program = prog(); int result = maxargs(program); printf("maxargs: %d\n", result); + + Table_ t = interpStm(program, NULL); + + + printf("a: %d\n", lookup(t, "a")); + printf("b: %d\n", lookup(t, "b")); } diff --git a/c1/main.h b/c1/main.h new file mode 100644 index 0000000..84d7ffd --- /dev/null +++ b/c1/main.h @@ -0,0 +1,20 @@ +#ifndef MAIN_H +#define MAIN_H + +#include "util.h" +#include "slp.h" + +typedef struct table *Table_; +struct table {string id; int value; Table_ tail;}; +Table_ Table(string id, int value, struct table *tail); + +Table_ interpStm(A_stm s, Table_ t); + +int lookup(Table_ t, string key); + +typedef struct intandtable *IntAndTable_; +struct intandtable {int i; Table_ t;}; +IntAndTable_ IntAndTable(int i, Table_ t); +IntAndTable_ interpExp(A_exp e, Table_ t); + +#endif // !MAIN_H diff --git a/c1/main.o b/c1/main.o deleted file mode 100644 index 2a6954a..0000000 Binary files a/c1/main.o and /dev/null differ diff --git a/c1/makefile b/c1/makefile index 9e61244..ae04a6d 100644 --- a/c1/makefile +++ b/c1/makefile @@ -1,7 +1,7 @@ a.out: main.o prog1.o slp.o util.o cc -g main.o prog1.o slp.o util.o -main.o: main.c slp.h util.h +main.o: main.c slp.h util.h main.h cc -g -c main.c prog1.o: prog1.c slp.h util.h diff --git a/c1/prog1.o b/c1/prog1.o deleted file mode 100644 index 3aebcbc..0000000 Binary files a/c1/prog1.o and /dev/null differ diff --git a/c1/slp.o b/c1/slp.o deleted file mode 100644 index f63cf02..0000000 Binary files a/c1/slp.o and /dev/null differ diff --git a/c1/util.c b/c1/util.c index 1f6c0d7..e6445d6 100644 --- a/c1/util.c +++ b/c1/util.c @@ -1,5 +1,12 @@ #include +#include #include "util.h" +string String(char *s) +{ + string p = checked_malloc(strlen(s)+1); + strcpy(p,s); + return p; +} void *checked_malloc(int len) { diff --git a/c1/util.o b/c1/util.o deleted file mode 100644 index 7c5b407..0000000 Binary files a/c1/util.o and /dev/null differ diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1 @@ +[]