make clean

This commit is contained in:
Michael Thomson 2024-12-10 10:42:46 -05:00
parent 2840bb6779
commit 1abc5b8e52
4 changed files with 112 additions and 77 deletions

View File

@ -1,12 +1,42 @@
[ [
{ {
"arguments": [ "arguments": [
"/nix/store/k8mrxviw965lv59hqbln3297jwfffm2w-clang-wrapper-16.0.6/bin/clang", "/usr/bin/gcc",
"-g", "-g",
"-c", "-c",
"main.c" "main.c"
], ],
"directory": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1", "directory": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1",
"file": "/Users/mthomson/dev/personal/modern-compiler-implementation-in-c/c1/main.c" "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"
} }
] ]

BIN
c1/main

Binary file not shown.

147
c1/main.c
View File

@ -1,6 +1,7 @@
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <strings.h> #include <string.h>
#include <stddef.h>
#include "prog1.h" #include "prog1.h"
#include "main.h" #include "main.h"
@ -21,100 +22,104 @@ int maxargs(A_stm statement)
} }
case A_printStm: { case A_printStm: {
int sum = 1; int sum = 1;
A_expList expList = statement->u.print.exps; A_expList expList = statement->u.print.exps;
while (expList->kind == A_pairExpList) { while (expList->kind == A_pairExpList) {
sum++; sum++;
expList = expList->u.pair.tail; expList = expList->u.pair.tail;
} }
return sum; return sum;
} }
} }
return 0; return 0;
} }
Table_ interpStm(A_stm stm, Table_ t) { Table_ interpStm(A_stm stm, Table_ t)
if (stm->kind == A_compoundStm) { {
t = interpStm(stm->u.compound.stm1, t); if (stm->kind == A_compoundStm) {
t = interpStm(stm->u.compound.stm2, t); t = interpStm(stm->u.compound.stm1, t);
} else if (stm->kind == A_assignStm) { t = interpStm(stm->u.compound.stm2, t);
IntAndTable_ it = interpExp(stm->u.assign.exp, t); } else if (stm->kind == A_assignStm) {
t = Table(stm->u.assign.id, it->i, it->t); IntAndTable_ it = interpExp(stm->u.assign.exp, t);
} else { t = Table(stm->u.assign.id, it->i, it->t);
A_expList expList = stm->u.print.exps; } else {
while (expList->kind != A_lastExpList) { A_expList expList = stm->u.print.exps;
t = interpExp(expList->u.pair.head, t)->t; while (expList->kind != A_lastExpList) {
expList = expList->u.pair.tail; t = interpExp(expList->u.pair.head, t)->t;
} expList = expList->u.pair.tail;
t = interpExp(expList->u.last, t)->t; }
} t = interpExp(expList->u.last, t)->t;
}
return t; return t;
} }
IntAndTable_ interpExp(A_exp e, Table_ t) { IntAndTable_ interpExp(A_exp e, Table_ t)
if (e->kind == A_idExp) { {
return IntAndTable(lookup(t, e->u.id), t); if (e->kind == A_idExp) {
} else if (e->kind == A_numExp) { return IntAndTable(lookup(t, e->u.id), t);
return IntAndTable(e->u.num, t); } else if (e->kind == A_numExp) {
} else if (e->kind == A_opExp) { return IntAndTable(e->u.num, t);
IntAndTable_ left = interpExp(e->u.op.left, t); } else if (e->kind == A_opExp) {
IntAndTable_ right = interpExp(e->u.op.right, t); IntAndTable_ left = interpExp(e->u.op.left, t);
A_binop op = e->u.op.oper; IntAndTable_ right = interpExp(e->u.op.right, t);
A_binop op = e->u.op.oper;
switch (op) { switch (op) {
case A_plus: case A_plus:
return IntAndTable(left->i + right->i, t); return IntAndTable(left->i + right->i, t);
case A_minus: case A_minus:
return IntAndTable(left->i - right->i, t); return IntAndTable(left->i - right->i, t);
case A_times: case A_times:
return IntAndTable(left->i * right->i, t); return IntAndTable(left->i * right->i, t);
case A_div: case A_div:
return IntAndTable(left->i / right->i, t); return IntAndTable(left->i / right->i, t);
} }
} else { } else {
t = interpStm(e->u.eseq.stm, t); t = interpStm(e->u.eseq.stm, t);
return interpExp(e->u.eseq.exp, t); return interpExp(e->u.eseq.exp, t);
} }
} }
IntAndTable_ IntAndTable(int i, Table_ t) { IntAndTable_ IntAndTable(int i, Table_ t)
IntAndTable_ it = checked_malloc(sizeof(*it)); {
it->i = i; IntAndTable_ it = checked_malloc(sizeof(*it));
it->t = t; it->i = i;
return it; it->t = t;
return it;
} }
Table_ Table(string id, int value, struct table *tail) { Table_ Table(string id, int value, struct table *tail)
Table_ t = checked_malloc(sizeof(*t)); {
t->id = id; Table_ t = checked_malloc(sizeof(*t));
t->value = value; t->id = id;
t->tail = tail; t->value = value;
return t; t->tail = tail;
return t;
} }
int lookup(Table_ t, string key) { int lookup(Table_ t, string key)
if (t == NULL) { {
// HACK: should indicate a null value if (t == NULL) {
return 0; // HACK: should indicate a null value
} return 0;
}
if (strcmp(t->id, key) == 0) { if (strcmp(t->id, key) == 0) {
return t->value; return t->value;
} }
return lookup(t->tail, key); return lookup(t->tail, key);
} }
int main() int main()
{ {
A_stm program = prog(); A_stm program = prog();
int result = maxargs(program); 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("a: %d\n", lookup(t, "a")); printf("b: %d\n", lookup(t, "b"));
printf("b: %d\n", lookup(t, "b"));
} }

View File

@ -1,17 +1,17 @@
a.out: main.o prog1.o slp.o util.o 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 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 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 slp.o: slp.c slp.h util.h
cc -g -c slp.c gcc -g -c slp.c
util.o: util.c util.h util.o: util.c util.h
cc -g -c util.c gcc -g -c util.c
clean: clean:
rm -f a.out util.o prog1.o slp.o main.o rm -f a.out util.o prog1.o slp.o main.o