chapter 1: ex2

This commit is contained in:
Michael Thomson 2024-09-23 08:31:37 -04:00
parent ef3c5dbe0d
commit 2840bb6779
No known key found for this signature in database
GPG Key ID: 8EFECCD347C72F7D
12 changed files with 112 additions and 31 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
.direnv
.ccls-cache
a.out
*.o

BIN
c1/a.out

Binary file not shown.

View File

@ -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"
}
]

View File

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

20
c1/main.h Normal file
View File

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

BIN
c1/main.o

Binary file not shown.

View File

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

Binary file not shown.

BIN
c1/slp.o

Binary file not shown.

View File

@ -1,5 +1,12 @@
#include <stdlib.h>
#include <strings.h>
#include "util.h"
string String(char *s)
{
string p = checked_malloc(strlen(s)+1);
strcpy(p,s);
return p;
}
void *checked_malloc(int len)
{

BIN
c1/util.o

Binary file not shown.

1
compile_commands.json Normal file
View File

@ -0,0 +1 @@
[]