jack-compiler/compiler/compiler-scopes.c

282 lines
7.3 KiB
C
Raw Normal View History

2020-12-21 13:05:49 -05:00
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "compiler-scopes.h"
#include "os.h"
2020-12-21 13:05:49 -05:00
2020-12-27 16:52:28 -05:00
typedef enum { local, staticseg, arg, fieldseg } MEMSEGMENT;
char* memsegnames[] = { "local", "static", "argument", "this" };
2020-12-21 13:05:49 -05:00
// Error messages
2020-12-27 16:52:28 -05:00
void doubledeclaration(const char* name, DEBUGINFO* d1, DEBUGINFO* d2);
void ensurenoduplicate(SCOPE* s, char* name);
2020-12-21 13:05:49 -05:00
2020-12-27 16:52:28 -05:00
// Getters
VAR* getvarinvars(VAR* vars, const char* name);
CLASS* getclass(SCOPE* s, const char* name);
SUBROUTDEC* getsubroutdecfromlist(SUBROUTDEC* start, char* name);
SUBROUTDEC* getmethod(SCOPE* s, VAR* parent, SUBROUTCALL* call);
SUBROUTDEC* getfunction(SCOPE* s, SUBROUTCALL* call);
SUBROUTDEC* getsubroutdecwithparent(SCOPE* s, SUBROUTCALL* call, VAR** varret);
2020-12-27 16:52:28 -05:00
SUBROUTDEC* getsubroutdecwithoutparent(SCOPE* s, SUBROUTCALL* call);
SUBROUTDEC* getsubroutdec(SCOPE* s, const char* name);
2020-12-21 13:05:49 -05:00
// Scope adding
2020-12-27 16:52:28 -05:00
VAR* mkvar(char* type, char* name, bool primitive, DEBUGINFO* debug, MEMSEGMENT seg);
void addvar(SCOPE* s, VAR** dest, VAR* v);
void addlocalvar(SCOPE* s, VARDEC* v);
2020-12-21 13:05:49 -05:00
void addclassvardec(SCOPE* s, CLASSVARDEC* v);
2020-12-27 16:52:28 -05:00
void addparameter(SCOPE* s, PARAMETER* p);
2020-12-21 13:05:49 -05:00
// Error messages
2020-12-27 16:52:28 -05:00
void doubledeclaration(const char* name, DEBUGINFO* d1, DEBUGINFO* d2) {
2020-12-21 16:11:23 -05:00
eprintf("Double declaration of '%s' at '%s', line %i; previously defined at '%s', line %i\n",
2020-12-27 16:52:28 -05:00
name, d1->file, d1->definedat, d2->file, d2->definedat);
2020-12-21 13:05:49 -05:00
exit(1);
}
2020-12-21 19:50:55 -05:00
void notdeclared(const char* name, DEBUGINFO* debug) {
2020-12-21 16:11:23 -05:00
eprintf("'%s' not declared; file '%s', line %i\n", name, debug->file, debug->definedat);
2020-12-21 13:05:49 -05:00
exit(1);
}
void invalidparent(SUBROUTCALL* call) {
2020-12-21 16:11:23 -05:00
eprintf("Invalid subroutine parent '%s'; file '%s', line %i\n", call->parentname, call->debug->file, call->debug->definedat);
2020-12-21 13:05:49 -05:00
exit(1);
}
2020-12-27 16:52:28 -05:00
void ensurenoduplicate(SCOPE* s, char* name) {
VAR* v = getvar(s, name);
if(v != NULL)
doubledeclaration(name, s->currdebug, v->debug);
CLASS* c = getclass(s, name);
if(c != NULL)
doubledeclaration(name, s->currdebug, c->debug);
SUBROUTDEC* sr = getsubroutdec(s, name);
if(sr != NULL)
doubledeclaration(name, s->currdebug, sr->debug);
2020-12-21 13:05:49 -05:00
}
// Scope handling
SCOPE* mkscope(SCOPE* prev) {
SCOPE* s = (SCOPE*)malloc(sizeof(SCOPE));
s->previous = prev;
2020-12-27 16:52:28 -05:00
s->localvars = NULL;
s->fields = NULL;
s->staticvars = NULL;
s->parameters = NULL;
s->classes = NULL;
s->subroutines = NULL;
2020-12-21 13:05:49 -05:00
return s;
}
2020-12-27 16:52:28 -05:00
// Getters
VAR* getvarinvars(VAR* vars, const char* name) {
while(vars != NULL) {
if(!strcmp(vars->name, name))
return vars;
vars = vars->next;
}
return NULL;
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
VAR* getvar(SCOPE* s, const char* name) {
VAR* var = getvarinvars(s->localvars, name);
if(var != NULL)
return var;
var = getvarinvars(s->parameters, name);
if(var != NULL)
return var;
var = getvarinvars(s->fields, name);
if(var != NULL)
return var;
var = getvarinvars(s->staticvars, name);
if(var != NULL)
return var;
if(s->previous != NULL)
return getvar(s->previous, name);
return NULL;
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
CLASS* getclass(SCOPE* s, const char* name) {
CLASS* curr = s->classes;
2020-12-21 13:05:49 -05:00
while(curr != NULL) {
if(!strcmp(curr->name, name))
return curr;
curr = curr->next;
}
2020-12-27 16:52:28 -05:00
if(s->previous != NULL)
return getclass(s->previous, name);
2020-12-21 13:05:49 -05:00
return NULL;
}
2020-12-27 16:52:28 -05:00
SUBROUTDEC* getsubroutdecfromlist(SUBROUTDEC* start, char* name) {
while(start != NULL) {
if(!strcmp(start->name, name))
return start;
start = start->next;
}
return NULL;
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
SUBROUTDEC* getmethod(SCOPE* s, VAR* parent, SUBROUTCALL* call) {
CLASS* c = getclass(s, parent->type);
SUBROUTDEC* d = getsubroutdecfromlist(c->subroutdecs, call->name);
if(d == NULL)
return NULL;
2020-12-30 18:47:11 -05:00
if(d->subroutclass != method) {
eprintf("Calling a function/constructor as if it were a method; file '%s', line %i\n", call->debug->file, call->debug->definedat);
2020-12-27 16:52:28 -05:00
exit(1);
}
return d;
}
2020-12-21 13:05:49 -05:00
2020-12-27 16:52:28 -05:00
SUBROUTDEC* getfunction(SCOPE* s, SUBROUTCALL* call) {
CLASS* c = getclass(s, call->parentname);
if(c == NULL)
2020-12-21 13:05:49 -05:00
notdeclared(call->parentname, call->debug);
2020-12-27 16:52:28 -05:00
SUBROUTDEC* d = getsubroutdecfromlist(c->subroutdecs, call->name);
if(d == NULL)
return NULL;
2020-12-27 16:52:28 -05:00
if(d->subroutclass == method) {
eprintf("Calling a method as if it were a function; file '%s', line %i\n", call->debug->file, call->debug->definedat);
exit(1);
}
return d;
}
2020-12-21 13:05:49 -05:00
SUBROUTDEC* getsubroutdecwithparent(SCOPE* s, SUBROUTCALL* call, VAR** varret) {
2020-12-27 16:52:28 -05:00
VAR* parent = getvar(s, call->parentname);
if(parent != NULL) {
if(parent->primitive) {
eprintf("Primitive type does not have subroutines; file '%s', line %i\n", call->debug->file, call->debug->definedat);
exit(1);
}
*varret = parent;
2020-12-27 16:52:28 -05:00
return getmethod(s, parent, call);
}
else
2020-12-27 16:52:28 -05:00
return getfunction(s, call);
}
SUBROUTDEC* getsubroutdecwithoutparent(SCOPE* s, SUBROUTCALL* call) {
SUBROUTDEC* d = getsubroutdecfromlist(s->currclass->subroutdecs, call->name);
return d;
2020-12-21 13:05:49 -05:00
}
SUBROUTDEC* getsubroutdecfromcall(SCOPE* s, SUBROUTCALL* call, VAR** varret) {
SUBROUTDEC* d;
*varret = NULL;
if(call->parentname != NULL) {
d = getossubroutdec(call);
if(d == NULL)
d = getsubroutdecwithparent(s, call, varret);
}
else {
d = getsubroutdecwithoutparent(s, call);
}
if(d == NULL)
notdeclared(call->name, call->debug);
return d;
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
SUBROUTDEC* getsubroutdec(SCOPE* s, const char* name) {
SUBROUTDEC* curr = s->subroutines;
2020-12-24 14:22:22 -05:00
while(curr != NULL) {
2020-12-27 16:52:28 -05:00
if(!strcmp(curr->name, name))
return curr;
2020-12-24 14:22:22 -05:00
curr = curr->next;
2020-12-21 13:05:49 -05:00
}
if(s->previous != NULL)
2020-12-27 16:52:28 -05:00
return getsubroutdec(s->previous, name);
2020-12-21 13:05:49 -05:00
return NULL;
}
2020-12-27 16:52:28 -05:00
// Scope adding
VAR* mkvar(char* type, char* name, bool primitive, DEBUGINFO* debug, MEMSEGMENT seg) {
VAR* v = (VAR*)malloc(sizeof(VAR));
v->name = name;
v->type = type;
v->debug = debug;
v->memsegment = memsegnames[seg];
v->primitive = primitive;
return v;
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
void addvar(SCOPE* s, VAR** dest, VAR* v) {
ensurenoduplicate(s, v->name);
if(!v->primitive) {
CLASS* type = getclass(s, v->type);
if(type == NULL)
notdeclared(v->type, v->debug);
}
if(*dest == NULL)
v->index = 0;
else
v->index = 1+(*dest)->index;
v->next = *dest;
*dest = v;
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
void addlocalvar(SCOPE* s, VARDEC* v) {
STRINGLIST* currname = v->names;
while(currname != NULL) {
addvar(s, &(s->localvars), mkvar(v->type, currname->content, v->primitive, v->debug, local));
currname = currname->next;
}
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
void addstaticvar(SCOPE* s, CLASSVARDEC* v) {
STRINGLIST* currname = v->base->names;
while(currname != NULL) {
addvar(s, &(s->staticvars), mkvar(v->base->type, currname->content, v->base->primitive, v->base->debug, staticseg));
currname = currname->next;
}
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
void addfield(SCOPE* s, CLASSVARDEC* v) {
STRINGLIST* currname = v->base->names;
while(currname != NULL) {
addvar(s, &(s->fields), mkvar(v->base->type, currname->content, v->base->primitive, v->base->debug, fieldseg));
currname = currname->next;
}
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
void addclassvardec(SCOPE* s, CLASSVARDEC* v) {
if(v->type == stat)
2020-12-27 16:52:28 -05:00
addstaticvar(s, v);
else
addfield(s, v);
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
void addparameter(SCOPE* s, PARAMETER* p) {
addvar(s, &(s->parameters), mkvar(p->type, p->name, p->primitive, p->debug, arg));
2020-12-24 14:22:22 -05:00
}
2020-12-27 16:52:28 -05:00
// Group adding
void addclassvardecs(SCOPE* s, CLASSVARDEC* classvardecs) {
while(classvardecs != NULL) {
addclassvardec(s, classvardecs);
classvardecs = classvardecs->next;
}
2020-12-21 13:05:49 -05:00
}
2020-12-27 16:52:28 -05:00
void addlocalvars(SCOPE* s, VARDEC* localvars) {
while(localvars != NULL) {
addlocalvar(s, localvars);
localvars = localvars->next;
}
2020-12-24 14:22:22 -05:00
}
2020-12-27 16:52:28 -05:00
void addparameters(SCOPE* s, PARAMETER* params) {
while(params != NULL) {
addparameter(s, params);
params = params->next;
}
2020-12-21 13:05:49 -05:00
}