Compare commits
No commits in common. "12dbe8b21daf907b558482821965642d0e1c7c2b" and "0949b183d78986b503770c586cc5f06445bdacd3" have entirely different histories.
12dbe8b21d
...
0949b183d7
4
Makefile
4
Makefile
|
@ -1,7 +1,7 @@
|
|||
FILES = *.c */*.c
|
||||
FILES = *.c */*.c
|
||||
LIBRARIES = -lpthread
|
||||
INCLUDES = -I. -I./parser/ -I./compiler/ -I./vm/ -I./tokenizer/ -I./misc/ -I./assembler/
|
||||
CFLAGS = -std=c99 -Wall -O3
|
||||
CFLAGS = -std=c99 -Wall -o3
|
||||
OUTFILE = jackc
|
||||
|
||||
main: ${FILES}
|
||||
|
|
|
@ -15,10 +15,10 @@ char* toascii(char c);
|
|||
// Dealing with singular terms
|
||||
LINEBLOCK* compilestrconst(char* str);
|
||||
LINEBLOCK* compilekeywordconst(SCOPE* s, TERM* t);
|
||||
LINEBLOCK* compilearrayitem(SCOPE* s, DEBUGINFO* d, TERM* t);
|
||||
LINEBLOCK* compilearrayitem(SCOPE* s, TERM* t);
|
||||
LINEBLOCK* compilecallln(SCOPE* s, SUBROUTDEC* d, SUBROUTCALL* call);
|
||||
LINEBLOCK* pushunaryopterm(SCOPE* s, DEBUGINFO* d, TERM* t);
|
||||
LINEBLOCK* compileterm(SCOPE* s, DEBUGINFO* d, TERM* t);
|
||||
LINEBLOCK* pushunaryopterm(SCOPE* s, TERM* t);
|
||||
LINEBLOCK* compileterm(SCOPE* s, TERM* t);
|
||||
|
||||
/* END FORWARD DECLARATIONS */
|
||||
|
||||
|
@ -128,9 +128,9 @@ LINEBLOCK* compilekeywordconst(SCOPE* s, TERM* t) {
|
|||
return mklnblk(pushconstant(0));
|
||||
}
|
||||
|
||||
LINEBLOCK* compilearrayitem(SCOPE* s, DEBUGINFO* d, TERM* t) {
|
||||
LINEBLOCK* blk = compileexpression(s, d, t->array->exp);
|
||||
appendln(blk, pushvar(s, d, t->array->name));
|
||||
LINEBLOCK* compilearrayitem(SCOPE* s, TERM* t) {
|
||||
LINEBLOCK* blk = compileexpression(s, t->array->exp);
|
||||
appendln(blk, pushvar(s, t->array->name));
|
||||
|
||||
appendln(blk, onetoken("add"));
|
||||
|
||||
|
@ -161,7 +161,7 @@ LINEBLOCK* compilesubroutcall(SCOPE* s, SUBROUTCALL* call) {
|
|||
LINEBLOCK* blk = compilecallln(s, d, call);
|
||||
|
||||
if(call->parameters != NULL)
|
||||
blk = mergelnblks(compileexplist(s, call->debug, call->parameters), blk);
|
||||
blk = mergelnblks(compileexplist(s, call->parameters), blk);
|
||||
|
||||
if(d->subroutclass == method) {
|
||||
if(call->parentname == NULL)
|
||||
|
@ -179,8 +179,8 @@ LINEBLOCK* compilesubroutcall(SCOPE* s, SUBROUTCALL* call) {
|
|||
return blk;
|
||||
}
|
||||
|
||||
LINEBLOCK* pushunaryopterm(SCOPE* s, DEBUGINFO* d, TERM* t) {
|
||||
LINEBLOCK* blk = compileexpression(s, d, t->expression);
|
||||
LINEBLOCK* pushunaryopterm(SCOPE* s, TERM* t) {
|
||||
LINEBLOCK* blk = compileexpression(s, t->expression);
|
||||
LINE* neg;
|
||||
if(t->unaryop == '-')
|
||||
neg = onetoken("neg");
|
||||
|
@ -190,39 +190,36 @@ LINEBLOCK* pushunaryopterm(SCOPE* s, DEBUGINFO* d, TERM* t) {
|
|||
return blk;
|
||||
}
|
||||
|
||||
LINEBLOCK* compileterm(SCOPE* s, DEBUGINFO* d, TERM* t) {
|
||||
if(t->type == varname) return mklnblk(pushvar(s, d, t->string));
|
||||
LINEBLOCK* compileterm(SCOPE* s, TERM* t) {
|
||||
if(t->type == varname) return mklnblk(pushvar(s, t->string));
|
||||
if(t->type == intconstant) return mklnblk(pushconstant(t->integer));
|
||||
if(t->type == stringconstant) return compilestrconst(t->string);
|
||||
if(t->type == keywordconstant) return compilekeywordconst(s, t);
|
||||
if(t->type == arrayitem) return compilearrayitem(s, d, t);
|
||||
if(t->type == arrayitem) return compilearrayitem(s, t);
|
||||
if(t->type == subroutcall) return compilesubroutcall(s, t->call);
|
||||
if(t->type == innerexpression) return compileexpression(s, d, t->expression);
|
||||
return pushunaryopterm(s, d, t);
|
||||
if(t->type == innerexpression) return compileexpression(s, t->expression);
|
||||
return pushunaryopterm(s, t);
|
||||
}
|
||||
|
||||
// Dealing with whole expressions
|
||||
LINEBLOCK* compileexpression(SCOPE* s, DEBUGINFO* d, TERM* e) {
|
||||
LINEBLOCK* compileexpression(SCOPE* s, TERM* e) {
|
||||
LINEBLOCK* blk = NULL;
|
||||
LINEBLOCK* ops = NULL;
|
||||
if(e != NULL) {
|
||||
while(true) {
|
||||
blk = mergelnblks(blk, compileterm(s, d, e));
|
||||
blk = mergelnblks(blk, compileterm(s, e));
|
||||
if(e->next == NULL)
|
||||
break;
|
||||
ops = mergelnblks(ops, mklnblk(mathopln(e->op)));
|
||||
appendln(blk, mathopln(e->op));
|
||||
e = e->next;
|
||||
}
|
||||
}
|
||||
if(ops != NULL)
|
||||
blk = mergelnblks(blk, ops);
|
||||
return blk;
|
||||
}
|
||||
|
||||
LINEBLOCK* compileexplist(SCOPE* s, DEBUGINFO* d, EXPRESSIONLIST* explist) {
|
||||
LINEBLOCK* compileexplist(SCOPE* s, EXPRESSIONLIST* explist) {
|
||||
LINEBLOCK* head = NULL;
|
||||
while(explist != NULL) {
|
||||
head = mergelnblks(head, compileexpression(s, d, explist->expression));
|
||||
head = mergelnblks(head, compileexpression(s, explist->expression));
|
||||
explist = explist->next;
|
||||
}
|
||||
return head;
|
||||
|
|
|
@ -10,6 +10,6 @@
|
|||
LINEBLOCK* compilesubroutcall(SCOPE* s, SUBROUTCALL* call);
|
||||
|
||||
// Dealing with whole expressions
|
||||
LINEBLOCK* compileexpression(SCOPE* s, DEBUGINFO* d, TERM* e);
|
||||
LINEBLOCK* compileexplist(SCOPE* s, DEBUGINFO* d, EXPRESSIONLIST* explist);
|
||||
LINEBLOCK* compileexpression(SCOPE* s, TERM* e);
|
||||
LINEBLOCK* compileexplist(SCOPE* s, EXPRESSIONLIST* explist);
|
||||
#endif
|
||||
|
|
|
@ -10,7 +10,7 @@ char* memsegnames[] = { "local", "static", "argument", "this" };
|
|||
|
||||
// Error messages
|
||||
void doubledeclaration(const char* name, DEBUGINFO* d1, DEBUGINFO* d2);
|
||||
void ensurenoduplicate(SCOPE* s, DEBUGINFO* d, char* name);
|
||||
void ensurenoduplicate(SCOPE* s, char* name);
|
||||
|
||||
// Getters
|
||||
VAR* getvarinvars(VAR* vars, const char* name);
|
||||
|
@ -48,18 +48,18 @@ void invalidparent(SUBROUTCALL* call) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
void ensurenoduplicate(SCOPE* s, DEBUGINFO* d, char* name) {
|
||||
void ensurenoduplicate(SCOPE* s, char* name) {
|
||||
VAR* v = getvar(s, name);
|
||||
if(v != NULL)
|
||||
doubledeclaration(name, d, v->debug);
|
||||
doubledeclaration(name, s->currdebug, v->debug);
|
||||
|
||||
CLASS* c = getclass(s, name);
|
||||
if(c != NULL)
|
||||
doubledeclaration(name, d, c->debug);
|
||||
doubledeclaration(name, s->currdebug, c->debug);
|
||||
|
||||
SUBROUTDEC* sr = getsubroutdec(s, name);
|
||||
if(sr != NULL)
|
||||
doubledeclaration(name, d, sr->debug);
|
||||
doubledeclaration(name, s->currdebug, sr->debug);
|
||||
}
|
||||
|
||||
// Scope handling
|
||||
|
@ -105,13 +105,6 @@ VAR* getvar(SCOPE* s, const char* name) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
VAR* getvarmustexist(SCOPE* s, DEBUGINFO* d, const char* name) {
|
||||
VAR* v = getvar(s, name);
|
||||
if(v == NULL)
|
||||
notdeclared(name, d);
|
||||
return v;
|
||||
}
|
||||
|
||||
CLASS* getclass(SCOPE* s, const char* name) {
|
||||
CLASS* curr = s->classes;
|
||||
while(curr != NULL) {
|
||||
|
@ -215,7 +208,7 @@ VAR* mkvar(char* type, char* name, bool primitive, DEBUGINFO* debug, MEMSEGMENT
|
|||
}
|
||||
|
||||
void addvar(SCOPE* s, VAR** dest, VAR* v) {
|
||||
ensurenoduplicate(s, v->debug, v->name);
|
||||
ensurenoduplicate(s, v->name);
|
||||
|
||||
if(!v->primitive) {
|
||||
CLASS* type = getclass(s, v->type);
|
||||
|
|
|
@ -22,6 +22,7 @@ typedef struct var {
|
|||
|
||||
typedef struct scope {
|
||||
struct compiler* compiler;
|
||||
DEBUGINFO* currdebug;
|
||||
CLASS* currclass;
|
||||
|
||||
CLASS* classes;
|
||||
|
@ -51,7 +52,6 @@ CLASS* getclass(SCOPE* s, const char* name);
|
|||
|
||||
// Generic getters
|
||||
VAR* getvar(SCOPE* s, const char* name);
|
||||
VAR* getvarmustexist(SCOPE* s, DEBUGINFO* d, const char* name);
|
||||
|
||||
// Freeing
|
||||
void freescope(SCOPE* s);
|
||||
|
|
|
@ -12,10 +12,10 @@ LINE* pushtemp();
|
|||
char* mkcondlabel(char* name, int count);
|
||||
|
||||
// Handling individual statements
|
||||
LINEBLOCK* compileret(SCOPE* s, STATEMENT* st);
|
||||
LINEBLOCK* compileif(SCOPE* s, STATEMENT* st);
|
||||
LINEBLOCK* compilewhile(SCOPE* s, STATEMENT* st);
|
||||
LINEBLOCK* compilelet(SCOPE* s, STATEMENT* st);
|
||||
LINEBLOCK* compileret(SCOPE* s, TERM* e);
|
||||
LINEBLOCK* compileif(SCOPE* s, IFSTATEMENT* st);
|
||||
LINEBLOCK* compilewhile(SCOPE* s, CONDSTATEMENT* w);
|
||||
LINEBLOCK* compilelet(SCOPE* s, LETSTATEMENT* l);
|
||||
LINEBLOCK* compilestatement(SCOPE* s, STATEMENT* st);
|
||||
|
||||
/* END FORWARD DECLARATIONS */
|
||||
|
@ -41,8 +41,7 @@ char* mkcondlabel(char* name, int count) {
|
|||
}
|
||||
|
||||
// Handling individual statements
|
||||
LINEBLOCK* compileret(SCOPE* s, STATEMENT* st) {
|
||||
TERM* e = st->retstatement;
|
||||
LINEBLOCK* compileret(SCOPE* s, TERM* e) {
|
||||
LINE* ret = onetoken("return");
|
||||
LINEBLOCK* blk = mklnblk(ret);
|
||||
|
||||
|
@ -51,14 +50,13 @@ LINEBLOCK* compileret(SCOPE* s, STATEMENT* st) {
|
|||
char* tokens[] = { "push", "constant", "0" };
|
||||
appendlnbefore(blk, mkln(tokens));
|
||||
} else
|
||||
blk = mergelnblks(compileexpression(s, st->debug, e), blk);
|
||||
blk = mergelnblks(compileexpression(s, e), blk);
|
||||
|
||||
return blk;
|
||||
}
|
||||
|
||||
LINEBLOCK* compileif(SCOPE* s, STATEMENT* st) {
|
||||
IFSTATEMENT* ifst = st->ifstatement;
|
||||
LINEBLOCK* blk = compileexpression(s, st->debug, ifst->base->expression);
|
||||
LINEBLOCK* compileif(SCOPE* s, IFSTATEMENT* st) {
|
||||
LINEBLOCK* blk = compileexpression(s, st->base->expression);
|
||||
|
||||
pthread_mutex_lock(&(s->compiler->ifmutex));
|
||||
static int ifcount = 0;
|
||||
|
@ -77,10 +75,10 @@ LINEBLOCK* compileif(SCOPE* s, STATEMENT* st) {
|
|||
char* truelabelln[] = { "label", truelabel };
|
||||
appendln(blk, mkln(truelabelln));
|
||||
|
||||
blk = mergelnblks(blk, compilestatements(s, ifst->base->statements));
|
||||
blk = mergelnblks(blk, compilestatements(s, st->base->statements));
|
||||
|
||||
char* endlabel;
|
||||
bool haselse = ifst->elsestatements != NULL;
|
||||
bool haselse = st->elsestatements != NULL;
|
||||
if(haselse) {
|
||||
endlabel = mkcondlabel("IF_END", mycount);
|
||||
char* endgoto[] = { "goto", endlabel };
|
||||
|
@ -91,7 +89,7 @@ LINEBLOCK* compileif(SCOPE* s, STATEMENT* st) {
|
|||
appendln(blk, mkln(falselabelln));
|
||||
|
||||
if(haselse) {
|
||||
blk = mergelnblks(blk, compilestatements(s, ifst->elsestatements));
|
||||
blk = mergelnblks(blk, compilestatements(s, st->elsestatements));
|
||||
char* endlabelln[] = { "label", endlabel };
|
||||
appendln(blk, mkln(endlabelln));
|
||||
free(endlabel);
|
||||
|
@ -103,9 +101,8 @@ LINEBLOCK* compileif(SCOPE* s, STATEMENT* st) {
|
|||
return blk;
|
||||
}
|
||||
|
||||
LINEBLOCK* compilewhile(SCOPE* s, STATEMENT* st) {
|
||||
CONDSTATEMENT* w = st->whilestatement;
|
||||
LINEBLOCK* blk = compileexpression(s, st->debug, w->expression);
|
||||
LINEBLOCK* compilewhile(SCOPE* s, CONDSTATEMENT* w) {
|
||||
LINEBLOCK* blk = compileexpression(s, w->expression);
|
||||
|
||||
pthread_mutex_lock(&(s->compiler->whilemutex));
|
||||
static int whilecount = 0;
|
||||
|
@ -137,14 +134,13 @@ LINEBLOCK* compilewhile(SCOPE* s, STATEMENT* st) {
|
|||
return blk;
|
||||
}
|
||||
|
||||
LINEBLOCK* compilelet(SCOPE* s, STATEMENT* st) {
|
||||
LETSTATEMENT* l = st->letstatement;
|
||||
LINEBLOCK* blk = compileexpression(s, st->debug, l->expression);
|
||||
LINEBLOCK* compilelet(SCOPE* s, LETSTATEMENT* l) {
|
||||
LINEBLOCK* blk = compileexpression(s, l->expression);
|
||||
|
||||
if(l->arrayind != NULL) {
|
||||
appendlnbefore(blk, onetoken("add"));
|
||||
appendlnbefore(blk, pushvar(s, st->debug, l->varname));
|
||||
blk = mergelnblks(compileexpression(s, st->debug, l->arrayind), blk);
|
||||
appendlnbefore(blk, pushvar(s, l->varname));
|
||||
blk = mergelnblks(compileexpression(s, l->arrayind), blk);
|
||||
|
||||
appendln(blk, poptemp());
|
||||
appendln(blk, popthatadd());
|
||||
|
@ -152,16 +148,17 @@ LINEBLOCK* compilelet(SCOPE* s, STATEMENT* st) {
|
|||
appendln(blk, popthat());
|
||||
}
|
||||
else
|
||||
appendln(blk, popvar(s, st->debug, l->varname));
|
||||
appendln(blk, popvar(s, l->varname));
|
||||
return blk;
|
||||
}
|
||||
|
||||
LINEBLOCK* compilestatement(SCOPE* s, STATEMENT* st) {
|
||||
s->currdebug = st->debug;
|
||||
if(st->type == dostatement) return compilesubroutcall(s, st->dostatement);
|
||||
if(st->type == returnstatement) return compileret(s, st);
|
||||
if(st->type == ifstatement) return compileif(s, st);
|
||||
if(st->type == whilestatement) return compilewhile(s, st);
|
||||
return compilelet(s, st);
|
||||
if(st->type == returnstatement) return compileret(s, st->retstatement);
|
||||
if(st->type == ifstatement) return compileif(s, st->ifstatement);
|
||||
if(st->type == whilestatement) return compilewhile(s, st->whilestatement);
|
||||
return compilelet(s, st->letstatement);
|
||||
}
|
||||
|
||||
LINEBLOCK* compilestatements(SCOPE* s, STATEMENT* sts) {
|
||||
|
|
|
@ -92,9 +92,13 @@ LINEBLOCK* compilefunbody(SCOPE* s, CLASS* cl, SUBROUTDEC* d) {
|
|||
LINEBLOCK* compilefundec(SCOPE* s, CLASS* cl, SUBROUTDEC* f) {
|
||||
LINE* label = mksubdeclabel(cl, f);
|
||||
|
||||
LINEBLOCK* body = compilefunbody(s, cl, f);
|
||||
appendlnbefore(body, label);
|
||||
return body;
|
||||
if(f->body->statements != NULL) {
|
||||
LINEBLOCK* body = compilefunbody(s, cl, f);
|
||||
appendlnbefore(body, label);
|
||||
return body;
|
||||
}
|
||||
else
|
||||
return mklnblk(label);
|
||||
}
|
||||
|
||||
LINEBLOCK* compileconstructor(SCOPE* s, CLASS* cl, SUBROUTDEC* con) {
|
||||
|
|
|
@ -8,16 +8,21 @@ LINE* opvarraw(SCOPE* s, char* op, VAR* v) {
|
|||
return ln;
|
||||
}
|
||||
|
||||
LINE* pushvarraw(SCOPE* s, VAR* v) {
|
||||
LINE* opvar(SCOPE* s, char* op, const char* name) {
|
||||
VAR* v = getvar(s, name);
|
||||
return opvarraw(s, op, v);
|
||||
}
|
||||
|
||||
LINE* pushvarraw(SCOPE*s, VAR* v) {
|
||||
return opvarraw(s, "push", v);
|
||||
}
|
||||
|
||||
LINE* pushvar(SCOPE* s, DEBUGINFO* d, const char* name) {
|
||||
return opvarraw(s, "push", getvarmustexist(s, d, name));
|
||||
LINE* pushvar(SCOPE* s, const char* name) {
|
||||
return opvar(s, "push", name);
|
||||
}
|
||||
|
||||
LINE* popvar(SCOPE* s, DEBUGINFO* d, const char* name) {
|
||||
return opvarraw(s, "pop", getvarmustexist(s, d, name));
|
||||
LINE* popvar(SCOPE* s, const char* name) {
|
||||
return opvar(s, "pop", name);
|
||||
}
|
||||
|
||||
LINE* poptemp() {
|
||||
|
|
|
@ -12,9 +12,9 @@
|
|||
LINE* onetoken(char* str);
|
||||
LINE* mksimpleln(char** tokens, int count);
|
||||
|
||||
LINE* pushvarraw(SCOPE* s, VAR* v);
|
||||
LINE* pushvar(SCOPE* s, DEBUGINFO* d, const char* name);
|
||||
LINE* popvar(SCOPE* s, DEBUGINFO* d, const char* name);
|
||||
LINE* pushvarraw(SCOPE*s, VAR* v);
|
||||
LINE* pushvar(SCOPE* s, const char* name);
|
||||
LINE* popvar(SCOPE* s, const char* name);
|
||||
LINE* poptemp();
|
||||
LINE* popthatadd();
|
||||
|
||||
|
|
Loading…
Reference in New Issue