2020-12-31 18:02:04 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "compiler-statements.h"
|
|
|
|
#include "compiler-structure.h"
|
|
|
|
#include "compiler-util.h"
|
|
|
|
|
|
|
|
/* BEGIN FORWARD DECLARATIONS */
|
|
|
|
|
|
|
|
// Miscelaneous
|
|
|
|
int countlocalvars(VARDEC* decs);
|
|
|
|
int countstrs(STRINGLIST* ls);
|
|
|
|
int getobjsize(CLASS* c);
|
|
|
|
LINE* mksubdeclabel(CLASS* c, SUBROUTDEC* sd);
|
|
|
|
|
|
|
|
// Compiling methods
|
2021-01-03 14:08:54 -05:00
|
|
|
LINEBLOCK* compilefunbody(SCOPE* s, CLASS* cl, SUBROUTBODY* b);
|
|
|
|
LINEBLOCK* compilefundec(SCOPE* s, CLASS* cl, SUBROUTDEC* f);
|
|
|
|
LINEBLOCK* compileconstructor(SCOPE* s, CLASS* cl, SUBROUTDEC* con);
|
|
|
|
LINEBLOCK* compilemethod(SCOPE* s, CLASS* cl, SUBROUTDEC* m);
|
2020-12-31 18:02:04 -05:00
|
|
|
|
|
|
|
/* END FORWARD DECLARATIONS */
|
|
|
|
|
|
|
|
|
|
|
|
// Miscelaneous
|
|
|
|
int countlocalvars(VARDEC* decs) {
|
|
|
|
int i = 0;
|
|
|
|
while(decs != NULL) {
|
|
|
|
STRINGLIST* curr = decs->names;
|
|
|
|
while(curr != NULL) {
|
|
|
|
i++;
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
decs = decs->next;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
LINE* mksubdeclabel(CLASS* c, SUBROUTDEC* sd) {
|
|
|
|
char* labelstrs[] = { "function", dotlabel(c->name, sd->name), itoa(countlocalvars(sd->body->vardecs)) };
|
|
|
|
LINE* label = mkln(labelstrs);
|
|
|
|
free(labelstrs[1]);
|
|
|
|
free(labelstrs[2]);
|
|
|
|
label->next = NULL;
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compiling methods
|
2021-01-03 14:08:54 -05:00
|
|
|
LINEBLOCK* compilefunbody(SCOPE* s, CLASS* cl, SUBROUTBODY* b) {
|
2020-12-31 18:02:04 -05:00
|
|
|
SCOPE* myscope = mkscope(s);
|
|
|
|
myscope->currclass = cl;
|
|
|
|
if(b->vardecs != NULL)
|
2021-01-03 14:08:54 -05:00
|
|
|
addlocalvars(myscope, b->vardecs);
|
|
|
|
LINEBLOCK* head = compilestatements(myscope, b->statements);
|
|
|
|
freescope(myscope);
|
2020-12-31 18:02:04 -05:00
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2021-01-03 14:08:54 -05:00
|
|
|
LINEBLOCK* compilefundec(SCOPE* s, CLASS* cl, SUBROUTDEC* f) {
|
2020-12-31 18:02:04 -05:00
|
|
|
LINE* label = mksubdeclabel(cl, f);
|
|
|
|
|
|
|
|
if(f->body->statements != NULL) {
|
2021-01-03 14:08:54 -05:00
|
|
|
LINEBLOCK* body = compilefunbody(s, cl, f->body);
|
2020-12-31 18:02:04 -05:00
|
|
|
appendlnbefore(body, label);
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return mklnblk(label);
|
|
|
|
}
|
|
|
|
|
2021-01-03 14:08:54 -05:00
|
|
|
LINEBLOCK* compileconstructor(SCOPE* s, CLASS* cl, SUBROUTDEC* con) {
|
2020-12-31 18:02:04 -05:00
|
|
|
LINE* label = mksubdeclabel(cl, con);
|
|
|
|
LINEBLOCK* blk = mklnblk(label);
|
|
|
|
|
|
|
|
char* size[] = { "push", "constant", itoa(getobjsize(cl)) };
|
|
|
|
char* memalloc[] = { "call", "Memory.alloc", "1" };
|
|
|
|
char* poppointer[] = { "pop", "pointer", "0" };
|
|
|
|
appendln(blk, mkln(size));
|
|
|
|
appendln(blk, mkln(memalloc));
|
|
|
|
appendln(blk, mkln(poppointer));
|
|
|
|
free(size[2]);
|
|
|
|
|
|
|
|
if(con->body != NULL)
|
2021-01-03 14:08:54 -05:00
|
|
|
return mergelnblks(blk, compilefunbody(s, cl, con->body));
|
2020-12-31 18:02:04 -05:00
|
|
|
else
|
|
|
|
return blk;
|
|
|
|
}
|
|
|
|
|
2021-01-03 14:08:54 -05:00
|
|
|
LINEBLOCK* compilemethod(SCOPE* s, CLASS* cl, SUBROUTDEC* m) {
|
2020-12-31 18:02:04 -05:00
|
|
|
LINE* label = mksubdeclabel(cl, m);
|
|
|
|
LINEBLOCK* blk = mklnblk(label);
|
|
|
|
|
|
|
|
char* pusharg0[] = { "push", "argument", "0" };
|
|
|
|
char* poppointer[] = { "pop", "pointer", "0" };
|
|
|
|
appendln(blk, mkln(pusharg0));
|
|
|
|
appendln(blk, mkln(poppointer));
|
|
|
|
|
|
|
|
if(m->body != NULL)
|
2021-01-03 14:08:54 -05:00
|
|
|
return mergelnblks(blk, compilefunbody(s, cl, m->body));
|
2020-12-31 18:02:04 -05:00
|
|
|
else
|
|
|
|
return blk;
|
|
|
|
}
|
|
|
|
|
2021-01-03 14:08:54 -05:00
|
|
|
LINEBLOCK* compilesubroutdec(SCOPE* s, CLASS* cl, SUBROUTDEC* sd) {
|
2020-12-31 18:02:04 -05:00
|
|
|
SCOPE* myscope = mkscope(s);
|
2021-01-03 14:08:54 -05:00
|
|
|
LINEBLOCK* blk;
|
2020-12-31 18:02:04 -05:00
|
|
|
if(sd->parameters != NULL)
|
|
|
|
addparameters(myscope, sd->subroutclass == method, sd->parameters);
|
|
|
|
if(sd->subroutclass == function)
|
2021-01-03 14:08:54 -05:00
|
|
|
blk = compilefundec(myscope, cl, sd);
|
|
|
|
else if(sd->subroutclass == constructor)
|
|
|
|
blk = compileconstructor(myscope, cl, sd);
|
|
|
|
else
|
|
|
|
blk = compilemethod(myscope, cl, sd);
|
|
|
|
freescope(myscope);
|
|
|
|
return blk;
|
2020-12-31 18:02:04 -05:00
|
|
|
}
|