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": [
"/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"
}
]

BIN
c1/main

Binary file not shown.

147
c1/main.c
View File

@ -1,6 +1,7 @@
#include <math.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stddef.h>
#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"));
}

View File

@ -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