jack-compiler/compiler/compiler.c

566 lines
15 KiB
C
Raw Normal View History

2020-12-20 13:58:10 -05:00
#include <stdlib.h>
#include <string.h>
#include "compiler.h"
2020-12-31 08:00:21 -05:00
LINEBLOCK* compilestatements(COMPILER* c, SCOPE* s, STATEMENT* sts);
2020-12-27 16:52:28 -05:00
LINEBLOCK* compilesubroutcall(SCOPE* s, SUBROUTCALL* call);
LINEBLOCK* compileexpression(SCOPE* s, TERM* e);
2020-12-22 13:45:12 -05:00
2020-12-27 16:52:28 -05:00
int countparameters(PARAMETER* params) {
2020-12-20 13:58:10 -05:00
int i = 0;
while(params != NULL) {
i++;
params = params->next;
}
return i;
}
2020-12-27 16:52:28 -05:00
int countexpressions(EXPRESSIONLIST* explist) {
int i = 0;
while(explist != NULL) {
i++;
explist = explist->next;
}
return i;
}
2020-12-20 13:58:10 -05:00
int countlocalvars(VARDEC* decs) {
int i = 0;
while(decs != NULL) {
2020-12-27 16:52:28 -05:00
STRINGLIST* curr = decs->names;
while(curr != NULL) {
i++;
curr = curr->next;
}
2020-12-20 13:58:10 -05:00
decs = decs->next;
}
return i;
}
char* dotlabel(char* n1, char* n2) {
int sz = (strlen(n1) + strlen(n2) + 2) * sizeof(char);
char* result = (char*)malloc(sz);
2020-12-21 13:05:49 -05:00
sprintf(result, "%s.%s", n1, n2);
2020-12-20 13:58:10 -05:00
return result;
}
2020-12-27 16:52:28 -05:00
char* mkcondlabel(char* name, int count) {
int sz = (strlen(name) + countplaces(count) + 1) * sizeof(char);
char* result = (char*)malloc(sz);
sprintf(result, "%s%i", name, count);
return result;
2020-12-20 13:58:10 -05:00
}
LINE* onetoken(char* str) {
LINE* ln = mkline(1);
addtoken(ln, ezheapstr(str));
2020-12-21 13:05:49 -05:00
ln->next = NULL;
2020-12-20 13:58:10 -05:00
return ln;
}
2020-12-21 13:05:49 -05:00
LINE* mksimpleln(char** tokens, int count) {
2020-12-20 13:58:10 -05:00
LINE* ln = mkline(count);
for(int i = 0; i < count; i++)
addtoken(ln, ezheapstr(tokens[i]));
2020-12-21 13:21:37 -05:00
ln->next = NULL;
2020-12-20 13:58:10 -05:00
return ln;
}
LINE* mathopln(char op) {
if(op == '+')
return onetoken("add");
if(op == '-')
return onetoken("sub");
if(op == '=')
return onetoken("eq");
if(op == '>')
return onetoken("gt");
if(op == '<')
return onetoken("lt");
if(op == '|')
return onetoken("or");
if(op == '&')
return onetoken("and");
if(op == '/') {
char* tokens[] = { "call", "Math.divide", "2" };
2020-12-21 16:11:23 -05:00
return mksimpleln(tokens, strcount(tokens));
2020-12-20 13:58:10 -05:00
}
2020-12-31 14:49:38 -05:00
char* tokens[] = { "call", "Math.multiply", "2" };
return mksimpleln(tokens, strcount(tokens));
2020-12-20 13:58:10 -05:00
}
2020-12-31 14:49:38 -05:00
LINE* pushconstant(int n) {
2020-12-27 16:52:28 -05:00
char* tokens[] = { "push", "constant", itoa(n) };
2020-12-31 14:49:38 -05:00
return mksimpleln(tokens, strcount(tokens));
2020-12-27 16:52:28 -05:00
}
2020-12-20 13:58:10 -05:00
2020-12-27 16:52:28 -05:00
LINEBLOCK* pushunaryopterm(SCOPE* s, TERM* t) {
LINEBLOCK* blk = compileexpression(s, t->expression);
LINE* neg;
if(t->unaryop == '-')
neg = onetoken("neg");
else
neg = onetoken("not");
appendln(blk, neg);
return blk;
}
2020-12-31 14:49:38 -05:00
LINE* opvarraw(SCOPE* s, char* op, VAR* v) {
2020-12-27 16:52:28 -05:00
char* tokens[] = { op, v->memsegment, itoa(v->index) };
2020-12-31 14:49:38 -05:00
return mksimpleln(tokens, strcount(tokens));
}
2020-12-31 14:49:38 -05:00
LINE* opvar(SCOPE* s, char* op, const char* name) {
VAR* v = getvar(s, name);
return opvarraw(s, op, v);
}
2020-12-27 16:52:28 -05:00
2020-12-31 14:49:38 -05:00
LINE* pushvarraw(SCOPE*s, VAR* v) {
return opvarraw(s, "push", v);
2020-12-27 16:52:28 -05:00
}
2020-12-31 14:49:38 -05:00
LINE* pushvar(SCOPE* s, const char* name) {
2020-12-27 16:52:28 -05:00
return opvar(s, "push", name);
}
2020-12-31 14:49:38 -05:00
LINE* popvar(SCOPE* s, const char* name) {
2020-12-27 16:52:28 -05:00
return opvar(s, "pop", name);
}
2020-12-31 14:49:38 -05:00
LINE* pushfalse() {
2020-12-27 16:52:28 -05:00
return pushconstant(0);
}
LINEBLOCK* pushtrue() {
2020-12-31 14:49:38 -05:00
LINEBLOCK* blk = mklnblk(pushfalse());
2020-12-27 16:52:28 -05:00
appendln(blk, onetoken("not"));
return blk;
}
2020-12-31 14:49:38 -05:00
LINE* pushthisadd() {
char* pushthisadd[] = { "push", "pointer", "0" };
return mksimpleln(pushthisadd, strcount(pushthisadd));
}
LINE* popthatadd() {
char* popthatadd[] = { "pop", "pointer", "1" };
return mksimpleln(popthatadd, strcount(popthatadd));
}
LINE* pushthatadd() {
char* pushthatadd[] = { "push", "pointer", "1" };
return mksimpleln(pushthatadd, strcount(pushthatadd));
}
LINE* popthat() {
char* popthat[] = { "pop", "that", "0" };
return mksimpleln(popthat, strcount(popthat));
}
LINE* pushthat() {
char* pushthat[] = { "push", "that", "0" };
return mksimpleln(pushthat, strcount(pushthat));
}
LINE* pushtemp() {
char* pushtemp[] = { "push", "temp", "0" };
return mksimpleln(pushtemp, strcount(pushtemp));
}
LINE* poptemp() {
char* poptemp[] = { "pop", "temp", "0" };
return mksimpleln(poptemp, strcount(poptemp));
2020-12-30 18:47:11 -05:00
}
2020-12-27 16:52:28 -05:00
LINEBLOCK* compilekeywordconst(SCOPE* s, TERM* t) {
if(!strcmp(t->string, "true")) return pushtrue();
2020-12-31 14:49:38 -05:00
if(!strcmp(t->string, "false")) return mklnblk(pushfalse());
if(!strcmp(t->string, "this")) return mklnblk(pushthisadd());
2020-12-27 16:52:28 -05:00
eprintf("Unsupported keyword '%s'\n", t->string);
exit(1);
}
2020-12-31 12:54:14 -05:00
char* toascii(char c) {
char* result = (char*)malloc(sizeof(char) * (countplaces(c) + 1));
sprintf(result, "%i", c);
return result;
}
LINEBLOCK* compilestrconst(char* str) {
if(str[0] == '\0')
return NULL;
char* pushchar[] = { "push", "constant", "" };
pushchar[2] = toascii(str[0]);
LINEBLOCK* blk = mklnblk(mksimpleln(pushchar, strcount(pushchar)));
free(pushchar[2]);
char* appendchar[] = { "call", "String.appendChar", "2" };
appendln(blk, mksimpleln(appendchar, strcount(appendchar)));
int i = 1;
char c;
while(c = str[i], c != '\0') {
pushchar[2] = toascii(c);
appendln(blk, mksimpleln(pushchar, strcount(pushchar)));
free(pushchar[2]);
appendln(blk, mksimpleln(appendchar, strcount(appendchar)));
i++;
}
char* strsize[] = { "push", "constant", itoa(i) };
char* mknew[] = { "call", "String.new", "1" };
appendlnbefore(blk, mksimpleln(mknew, strcount(mknew)));
appendlnbefore(blk, mksimpleln(strsize, strcount(strsize)));
free(strsize[2]);
2020-12-31 14:49:38 -05:00
return blk;
}
LINEBLOCK* compilearrayitem(SCOPE* s, TERM* t) {
LINEBLOCK* blk = compileexpression(s, t->array->exp);
appendln(blk, pushvar(s, t->array->name));
appendln(blk, onetoken("add"));
appendln(blk, popthatadd());
appendln(blk, pushthat());
return blk;
2020-12-31 12:54:14 -05:00
}
2020-12-27 16:52:28 -05:00
LINEBLOCK* compileterm(SCOPE* s, TERM* t) {
2020-12-31 14:49:38 -05:00
if(t->type == intconstant) return mklnblk(pushconstant(t->integer));
2020-12-27 16:52:28 -05:00
if(t->type == unaryopterm) return pushunaryopterm(s, t);
if(t->type == innerexpression) return compileexpression(s, t->expression);
2020-12-31 14:49:38 -05:00
if(t->type == varname) return mklnblk(pushvar(s, t->string));
2020-12-27 16:52:28 -05:00
if(t->type == subroutcall) return compilesubroutcall(s, t->call);
if(t->type == keywordconstant) return compilekeywordconst(s, t);
2020-12-31 12:54:14 -05:00
if(t->type == stringconstant) return compilestrconst(t->string);
2020-12-31 14:49:38 -05:00
return compilearrayitem(s, t);
2020-12-27 16:52:28 -05:00
}
2020-12-20 13:58:10 -05:00
2020-12-27 16:52:28 -05:00
LINEBLOCK* compileexpression(SCOPE* s, TERM* e) {
LINEBLOCK* blk = compileterm(s, e);
TERM* curr = e->next;
if(curr != NULL) {
while(true) {
blk = mergelnblks(blk, compileterm(s, curr));
if(curr->next != NULL) {
appendln(blk, mathopln(curr->op));
curr = curr->next;
}
else break;
}
appendln(blk, mathopln(e->op));
2020-12-20 13:58:10 -05:00
}
2020-12-27 16:52:28 -05:00
return blk;
2020-12-20 13:58:10 -05:00
}
2020-12-27 16:52:28 -05:00
LINEBLOCK* compileexplist(SCOPE* s, EXPRESSIONLIST* explist) {
2020-12-21 13:05:49 -05:00
LINEBLOCK* head = NULL;
2020-12-27 16:52:28 -05:00
while(explist != NULL) {
head = mergelnblks(head, compileexpression(s, explist->expression));
explist = explist->next;
2020-12-20 13:58:10 -05:00
}
return head;
}
LINEBLOCK* compilecallln(SCOPE* s, SUBROUTDEC* d, SUBROUTCALL* call) {
2020-12-21 13:05:49 -05:00
LINE* ln = mkline(3);
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
addtoken(ln, ezheapstr("call"));
2020-12-20 13:58:10 -05:00
addtoken(ln, dotlabel(d->class->name, call->name));
2020-12-20 13:58:10 -05:00
int count = countexpressions(call->parameters);
if(d->subroutclass == method)
count++;
addtoken(ln, itoa(count));
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
return mklnblk(ln);
}
2020-12-20 13:58:10 -05:00
2020-12-27 16:52:28 -05:00
LINEBLOCK* compilesubroutcall(SCOPE* s, SUBROUTCALL* call) {
VAR* v;
SUBROUTDEC* d = getsubroutdecfromcall(s, call, &v);
LINEBLOCK* blk = compilecallln(s, d, call);
2020-12-21 13:05:49 -05:00
if(call->parameters != NULL)
2020-12-27 16:52:28 -05:00
blk = mergelnblks(compileexplist(s, call->parameters), blk);
2020-12-21 13:05:49 -05:00
if(d->subroutclass == method) {
2020-12-30 18:47:11 -05:00
if(call->parentname == NULL)
2020-12-31 14:49:38 -05:00
appendlnbefore(blk, pushthisadd());
2020-12-30 18:47:11 -05:00
else
2020-12-31 14:49:38 -05:00
appendlnbefore(blk, pushvarraw(s, v));
2020-12-30 18:47:11 -05:00
}
// void functions always return 0
// therefore must be thrown away
if(!strcmp(d->type, "void")) {
2020-12-31 14:49:38 -05:00
appendln(blk, poptemp());
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
return blk;
2020-12-20 13:58:10 -05:00
}
2020-12-21 13:05:49 -05:00
LINEBLOCK* compileret(SCOPE* s, TERM* e) {
LINE* ret = onetoken("return");
2020-12-27 16:52:28 -05:00
LINEBLOCK* blk = mklnblk(ret);
2020-12-21 13:05:49 -05:00
2020-12-21 18:35:41 -05:00
// void subroutdecs return 0
2020-12-20 13:58:10 -05:00
if(e == NULL) {
2020-12-21 13:05:49 -05:00
char* tokens[] = { "push", "constant", "0" };
2020-12-27 16:52:28 -05:00
appendlnbefore(blk, mksimpleln(tokens, strcount(tokens)));
2020-12-21 13:05:49 -05:00
} else
2020-12-27 16:52:28 -05:00
blk = mergelnblks(compileexpression(s, e), blk);
2020-12-21 13:05:49 -05:00
2020-12-27 16:52:28 -05:00
return blk;
2020-12-20 13:58:10 -05:00
}
2020-12-31 08:00:21 -05:00
LINEBLOCK* compileif(COMPILER* c, SCOPE* s, IFSTATEMENT* st) {
2020-12-27 16:52:28 -05:00
LINEBLOCK* blk = compileexpression(s, st->base->expression);
2020-12-31 08:00:21 -05:00
pthread_mutex_lock(&(c->ifmutex));
2020-12-27 16:52:28 -05:00
static int ifcount = 0;
int mycount = ifcount;
ifcount++;
2020-12-31 08:00:21 -05:00
pthread_mutex_unlock(&(c->ifmutex));
2020-12-22 13:45:12 -05:00
2020-12-27 16:52:28 -05:00
char* truelabel = mkcondlabel("IF_TRUE", mycount);
char* ifgoto[] = { "if-goto", truelabel };
appendln(blk, mksimpleln(ifgoto, strcount(ifgoto)));
char* falselabel = mkcondlabel("IF_FALSE", mycount);
char* gotofalse[] = { "goto", falselabel };
appendln(blk, mksimpleln(gotofalse, strcount(gotofalse)));
char* truelabelln[] = { "label", truelabel };
appendln(blk, mksimpleln(truelabelln, strcount(truelabelln)));
2020-12-22 13:45:12 -05:00
2020-12-31 08:00:21 -05:00
blk = mergelnblks(blk, compilestatements(c, s, st->base->statements));
2020-12-22 13:45:12 -05:00
2020-12-27 16:52:28 -05:00
char* endlabel;
2020-12-22 13:45:12 -05:00
bool haselse = st->elsestatements != NULL;
if(haselse) {
2020-12-27 16:52:28 -05:00
endlabel = mkcondlabel("IF_END", mycount);
char* endgoto[] = { "goto", endlabel };
appendln(blk, mksimpleln(endgoto, strcount(endgoto)));
2020-12-20 13:58:10 -05:00
}
2020-12-22 13:45:12 -05:00
2020-12-27 16:52:28 -05:00
char* falselabelln[] = { "label", falselabel};
appendln(blk, mksimpleln(falselabelln, strcount(falselabelln)));
2020-12-22 13:45:12 -05:00
if(haselse) {
2020-12-31 08:00:21 -05:00
blk = mergelnblks(blk, compilestatements(c, s, st->elsestatements));
2020-12-27 16:52:28 -05:00
char* endlabelln[] = { "label", endlabel };
appendln(blk, mksimpleln(endlabelln, strcount(endlabelln)));
2020-12-22 13:45:12 -05:00
}
2020-12-27 16:52:28 -05:00
return blk;
2020-12-22 13:45:12 -05:00
}
2020-12-31 08:00:21 -05:00
LINEBLOCK* compilewhile(COMPILER* c, SCOPE* s, CONDSTATEMENT* w) {
2020-12-27 16:52:28 -05:00
LINEBLOCK* blk = compileexpression(s, w->expression);
2020-12-22 13:45:12 -05:00
2020-12-31 08:00:21 -05:00
pthread_mutex_lock(&(c->whilemutex));
2020-12-27 16:52:28 -05:00
static int whilecount = 0;
int mycount = whilecount;
whilecount++;
2020-12-31 08:00:21 -05:00
pthread_mutex_unlock(&(c->whilemutex));
2020-12-22 13:45:12 -05:00
2020-12-27 16:52:28 -05:00
char* explabel = mkcondlabel("WHILE_EXP", mycount);
char* explabelln[] = { "label", explabel };
appendlnbefore(blk, mksimpleln(explabelln, strcount(explabelln)));
2020-12-22 13:45:12 -05:00
2020-12-27 16:52:28 -05:00
appendln(blk, onetoken("not"));
2020-12-22 13:45:12 -05:00
2020-12-27 16:52:28 -05:00
char* endlabel = mkcondlabel("WHILE_END", mycount);
char* ifgoto[] = { "if-goto", endlabel };
appendln(blk, mksimpleln(ifgoto, strcount(ifgoto)));
2020-12-22 13:45:12 -05:00
2020-12-31 08:00:21 -05:00
blk = mergelnblks(blk, compilestatements(c, s, w->statements));
2020-12-22 13:45:12 -05:00
2020-12-27 16:52:28 -05:00
char* gotoln[] = { "goto", explabel };
appendln(blk, mksimpleln(gotoln, strcount(gotoln)));
2020-12-22 13:45:12 -05:00
2020-12-27 16:52:28 -05:00
char* endlabelln[] = { "label", endlabel };
appendln(blk, mksimpleln(endlabelln, strcount(endlabelln)));
return blk;
2020-12-22 13:45:12 -05:00
}
2020-12-27 16:52:28 -05:00
LINEBLOCK* compilelet(SCOPE* s, LETSTATEMENT* l) {
LINEBLOCK* blk = compileexpression(s, l->expression);
2020-12-31 14:49:38 -05:00
if(l->arrayind != NULL) {
appendlnbefore(blk, onetoken("add"));
appendlnbefore(blk, pushvar(s, l->varname));
blk = mergelnblks(compileexpression(s, l->arrayind), blk);
appendln(blk, poptemp());
appendln(blk, popthatadd());
appendln(blk, pushtemp());
appendln(blk, popthat());
}
else
appendln(blk, popvar(s, l->varname));
2020-12-27 16:52:28 -05:00
return blk;
2020-12-22 13:45:12 -05:00
}
2020-12-31 08:00:21 -05:00
LINEBLOCK* compilestatement(COMPILER* c, SCOPE* s, STATEMENT* st) {
2020-12-27 16:52:28 -05:00
s->currdebug = st->debug;
if(st->type == dostatement) return compilesubroutcall(s, st->dostatement);
2020-12-22 13:45:12 -05:00
if(st->type == returnstatement) return compileret(s, st->retstatement);
2020-12-31 08:00:21 -05:00
if(st->type == ifstatement) return compileif(c, s, st->ifstatement);
if(st->type == whilestatement) return compilewhile(c, s, st->whilestatement);
2020-12-27 16:52:28 -05:00
if(st->type == letstatement) return compilelet(s, st->letstatement);
2020-12-22 13:45:12 -05:00
eprintf("UNSUPPORTED type %i\n", st->type);
exit(1);
2020-12-20 13:58:10 -05:00
}
2020-12-31 08:00:21 -05:00
LINEBLOCK* compilestatements(COMPILER* c, SCOPE* s, STATEMENT* sts) {
2020-12-21 13:05:49 -05:00
LINEBLOCK* head = NULL;
while(sts != NULL) {
2020-12-31 08:00:21 -05:00
head = mergelnblks(head, compilestatement(c, s, sts));
2020-12-20 13:58:10 -05:00
sts = sts->next;
}
return head;
}
2020-12-31 08:00:21 -05:00
LINEBLOCK* compilefunbody(COMPILER* c, SCOPE* s, CLASS* cl, SUBROUTBODY* b) {
2020-12-24 14:22:22 -05:00
SCOPE* myscope = mkscope(s);
2020-12-31 08:00:21 -05:00
myscope->currclass = cl;
2020-12-24 14:22:22 -05:00
if(b->vardecs != NULL)
2020-12-27 16:52:28 -05:00
addlocalvars(s, b->vardecs);
2020-12-31 08:00:21 -05:00
LINEBLOCK* head = compilestatements(c, myscope, b->statements);
2020-12-20 13:58:10 -05:00
return head;
}
2020-12-30 18:47:11 -05:00
LINE* mksubdeclabel(CLASS* c, SUBROUTDEC* sd) {
char* labelstrs[] = { "function", dotlabel(c->name, sd->name), itoa(countlocalvars(sd->body->vardecs)) };
LINE* label = mksimpleln(labelstrs, strcount(labelstrs));
free(labelstrs[1]);
free(labelstrs[2]);
2020-12-21 13:05:49 -05:00
label->next = NULL;
2020-12-30 18:47:11 -05:00
return label;
}
2020-12-31 08:00:21 -05:00
LINEBLOCK* compilefundec(COMPILER* c, SCOPE* s, CLASS* cl, SUBROUTDEC* f) {
LINE* label = mksubdeclabel(cl, f);
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
if(f->body->statements != NULL) {
2020-12-31 08:00:21 -05:00
LINEBLOCK* body = compilefunbody(c, s, cl, f->body);
2020-12-21 13:05:49 -05:00
appendlnbefore(body, label);
return body;
}
else
return mklnblk(label);
2020-12-20 13:58:10 -05:00
}
2020-12-30 18:47:11 -05:00
int countstrs(STRINGLIST* ls) {
int count = 0;
while(ls != NULL) {
count++;
ls = ls->next;
}
return count;
}
int getobjsize(CLASS* c) {
CLASSVARDEC* curr = c->vardecs;
int count = 0;
while(curr != NULL) {
if(curr->type == field)
count += countstrs(curr->base->names);
curr = curr->next;
}
return count;
}
2020-12-31 08:00:21 -05:00
LINEBLOCK* compileconstructor(COMPILER* c, SCOPE* s, CLASS* cl, SUBROUTDEC* con) {
LINE* label = mksubdeclabel(cl, con);
2020-12-30 18:47:11 -05:00
LINEBLOCK* blk = mklnblk(label);
char* size[] = { "push", "constant", itoa(getobjsize(cl)) };
2020-12-30 18:47:11 -05:00
char* memalloc[] = { "call", "Memory.alloc", "1" };
char* poppointer[] = { "pop", "pointer", "0" };
appendln(blk, mksimpleln(size, strcount(size)));
appendln(blk, mksimpleln(memalloc, strcount(memalloc)));
appendln(blk, mksimpleln(poppointer, strcount(poppointer)));
free(size[2]);
2020-12-30 18:47:11 -05:00
if(con->body != NULL)
2020-12-31 08:00:21 -05:00
return mergelnblks(blk, compilefunbody(c, s, cl, con->body));
2020-12-30 18:47:11 -05:00
else
return blk;
}
2020-12-31 08:00:21 -05:00
LINEBLOCK* compilemethod(COMPILER* c, SCOPE* s, CLASS* cl, SUBROUTDEC* m) {
LINE* label = mksubdeclabel(cl, m);
2020-12-30 18:47:11 -05:00
LINEBLOCK* blk = mklnblk(label);
char* pusharg0[] = { "push", "argument", "0" };
2020-12-30 18:47:11 -05:00
char* poppointer[] = { "pop", "pointer", "0" };
appendln(blk, mksimpleln(pusharg0, strcount(pusharg0)));
appendln(blk, mksimpleln(poppointer, strcount(poppointer)));
if(m->body != NULL)
2020-12-31 08:00:21 -05:00
return mergelnblks(blk, compilefunbody(c, s, cl, m->body));
2020-12-30 18:47:11 -05:00
else
return blk;
}
2020-12-31 08:00:21 -05:00
LINEBLOCK* compilesubroutdec(COMPILER* c, SCOPE* s, CLASS* cl, SUBROUTDEC* sd) {
2020-12-20 13:58:10 -05:00
// 'this' and arguments are pushed by caller
// Must have a 'return' at the end
// Label names must have class name too (see mapping)
// types: method, function, constructor
// must switch all of these
2020-12-27 16:52:28 -05:00
SCOPE* myscope = mkscope(s);
if(sd->parameters != NULL)
addparameters(myscope, sd->parameters);
2020-12-20 13:58:10 -05:00
if(sd->subroutclass == function)
2020-12-31 08:00:21 -05:00
return compilefundec(c, myscope, cl, sd);
2020-12-30 18:47:11 -05:00
if(sd->subroutclass == constructor)
2020-12-31 08:00:21 -05:00
return compileconstructor(c, myscope, cl, sd);
return compilemethod(c, myscope, cl, sd);
2020-12-20 13:58:10 -05:00
}
2020-12-21 13:05:49 -05:00
LINEBLOCK* compileclass(COMPILER* c, CLASS* class) {
2020-12-20 13:58:10 -05:00
SCOPE* topscope = mkscope(c->globalscope);
2020-12-24 14:22:22 -05:00
if(class->vardecs != NULL)
2020-12-31 11:39:07 -05:00
addclassvardecs(c, topscope, class->vardecs);
2020-12-24 14:22:22 -05:00
if(class->subroutdecs != NULL)
2020-12-27 16:52:28 -05:00
topscope->subroutines = class->subroutdecs;
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
LINEBLOCK* output = NULL;
2020-12-21 18:35:41 -05:00
SUBROUTDEC* curr = class->subroutdecs;
2020-12-21 13:05:49 -05:00
while(curr != NULL) {
2020-12-31 08:00:21 -05:00
output = mergelnblks(output, compilesubroutdec(c, topscope, class, curr));
2020-12-21 13:05:49 -05:00
curr = curr->next;
2020-12-20 13:58:10 -05:00
}
2020-12-21 13:05:49 -05:00
return output;
2020-12-20 13:58:10 -05:00
}
COMPILER* mkcompiler(CLASS* classes) {
COMPILER* c = (COMPILER*)malloc(sizeof(COMPILER));
c->globalscope = mkscope(NULL);
2020-12-27 16:52:28 -05:00
c->globalscope->classes = classes;
2020-12-24 14:22:22 -05:00
c->classes = classes;
2020-12-31 08:00:21 -05:00
pthread_mutex_init(&(c->ifmutex), NULL);
pthread_mutex_init(&(c->whilemutex), NULL);
2020-12-31 11:39:07 -05:00
pthread_mutex_init(&(c->staticmutex), NULL);
2020-12-20 13:58:10 -05:00
return c;
}
2020-12-31 08:00:21 -05:00
void freecompiler(COMPILER* c) {
pthread_mutex_destroy(&(c->ifmutex));
pthread_mutex_destroy(&(c->whilemutex));
2020-12-31 11:39:07 -05:00
pthread_mutex_destroy(&(c->staticmutex));
2020-12-31 08:00:21 -05:00
// to be continued
free(c);
}