From 1abc5b8e5200b21154d2c7fa6780b8c8d1d067d0 Mon Sep 17 00:00:00 2001 From: Michael Thomson Date: Tue, 10 Dec 2024 10:42:46 -0500 Subject: [PATCH] make clean --- c1/compile_commands.json | 32 ++++++++- c1/main | Bin 16824 -> 0 bytes c1/main.c | 147 ++++++++++++++++++++------------------- c1/makefile | 10 +-- 4 files changed, 112 insertions(+), 77 deletions(-) delete mode 100755 c1/main diff --git a/c1/compile_commands.json b/c1/compile_commands.json index 0e1f63e..b85731a 100644 --- a/c1/compile_commands.json +++ b/c1/compile_commands.json @@ -1,12 +1,42 @@ [ { "arguments": [ - "/nix/store/k8mrxviw965lv59hqbln3297jwfffm2w-clang-wrapper-16.0.6/bin/clang", + "/usr/bin/gcc", "-g", "-c", "main.c" ], "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": [ + "/usr/bin/gcc", + "-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": [ + "/usr/bin/gcc", + "-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": [ + "/usr/bin/gcc", + "-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 b/c1/main deleted file mode 100755 index 6ddca431b14a0de8c72fe5a321693eaf9f9f4601..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16824 zcmeI4U5Fc16o7A%UD)DMS8R(~S3^;;;v|#ob~3?*#7jQAd7hrQ6DS?@gM1f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diff --git a/c1/main.c b/c1/main.c index 74a7959..33432fe 100644 --- a/c1/main.c +++ b/c1/main.c @@ -1,6 +1,7 @@ #include #include -#include +#include +#include #include "prog1.h" #include "main.h" @@ -21,100 +22,104 @@ int maxargs(A_stm statement) } case A_printStm: { int sum = 1; - A_expList expList = statement->u.print.exps; - while (expList->kind == A_pairExpList) { - sum++; - expList = expList->u.pair.tail; - } - return sum; + A_expList expList = statement->u.print.exps; + while (expList->kind == A_pairExpList) { + sum++; + expList = expList->u.pair.tail; + } + return sum; } } 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; - } +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; + 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; +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); - } + 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; +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; +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; - } +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; - } + if (strcmp(t->id, key) == 0) { + return t->value; + } - return lookup(t->tail, key); + return lookup(t->tail, key); } int main() { A_stm program = prog(); int result = maxargs(program); - printf("maxargs: %d\n", result); + printf("maxargs: %d\n", result); - Table_ t = interpStm(program, NULL); + Table_ t = interpStm(program, NULL); - - printf("a: %d\n", lookup(t, "a")); - printf("b: %d\n", lookup(t, "b")); + printf("a: %d\n", lookup(t, "a")); + printf("b: %d\n", lookup(t, "b")); } diff --git a/c1/makefile b/c1/makefile index ae04a6d..f1ef6f5 100644 --- a/c1/makefile +++ b/c1/makefile @@ -1,17 +1,17 @@ a.out: main.o prog1.o slp.o util.o - cc -g main.o prog1.o slp.o util.o + gcc -g main.o prog1.o slp.o util.o main.o: main.c slp.h util.h main.h - cc -g -c main.c + gcc -g -c main.c prog1.o: prog1.c slp.h util.h - cc -g -c prog1.c + gcc -g -c prog1.c slp.o: slp.c slp.h util.h - cc -g -c slp.c + gcc -g -c slp.c util.o: util.c util.h - cc -g -c util.c + gcc -g -c util.c clean: rm -f a.out util.o prog1.o slp.o main.o