Fix scope bugs

This commit is contained in:
Augusto Gunsch 2020-12-21 16:49:37 -03:00
parent cbf7879794
commit c3df97b04b
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
5 changed files with 36 additions and 15 deletions

View File

@ -274,53 +274,65 @@ OBJ* getbynamelist(SCOPE* s, STRINGLIST* names, char** retname) {
// Scope adding // Scope adding
void addclassvardec(SCOPE* s, CLASSVARDEC* v) { void addclassvardec(SCOPE* s, CLASSVARDEC* v) {
ensurenoduplicates(s, v->base->names, v->base->debug); ensurenoduplicates(s, v->base->names, v->base->debug);
v->next = s->classvardecs; CLASSVARDEC* new = copy(v, sizeof(CLASSVARDEC));
s->classvardecs = v; new->next = s->classvardecs;
s->classvardecs = new;
} }
void addvardec(SCOPE* s, VARDEC* v) { void addvardec(SCOPE* s, VARDEC* v) {
ensurenoduplicates(s, v->names, v->debug); ensurenoduplicates(s, v->names, v->debug);
v->next = s->vardecs; VARDEC* new = copy(v, sizeof(VARDEC));
s->vardecs = v; new->next = s->vardecs;
s->vardecs = new;
} }
void addsubdec(SCOPE* s, SUBDEC* sd) { void addsubdec(SCOPE* s, SUBDEC* sd) {
ensurenoduplicate(s, sd->name, sd->debug); ensurenoduplicate(s, sd->name, sd->debug);
sd->next = s->subroutines; SUBDEC* new = copy(sd, sizeof(SUBDEC));
s->subroutines = sd; new->next = s->subroutines;
s->subroutines = new;
} }
void addclass(SCOPE* s, CLASS* c) { void addclass(SCOPE* s, CLASS* c) {
ensurenoduplicate(s, c->name, c->debug); ensurenoduplicate(s, c->name, c->debug);
c->next = s->classes; CLASS* new = copy(c, sizeof(CLASS));
s->classes = c; new->next = s->classes;
s->classes = new;
} }
// Group adding // Group adding
void addclassvardecs(SCOPE* s, CLASSVARDEC* vs) { void addclassvardecs(SCOPE* s, CLASSVARDEC* vs) {
CLASSVARDEC* next;
while(vs != NULL) { while(vs != NULL) {
next = vs->next;
addclassvardec(s, vs); addclassvardec(s, vs);
vs = vs->next; vs = next;
} }
} }
void addvardecs(SCOPE* s, VARDEC* vs) { void addvardecs(SCOPE* s, VARDEC* vs) {
VARDEC* next;
while(vs != NULL) { while(vs != NULL) {
next = vs->next;
addvardec(s, vs); addvardec(s, vs);
vs = vs->next; vs = next;
} }
} }
void addsubdecs(SCOPE* s, SUBDEC* ss) { void addsubdecs(SCOPE* s, SUBDEC* ss) {
SUBDEC* next;
while(ss != NULL) { while(ss != NULL) {
next = ss->next;
addsubdec(s, ss); addsubdec(s, ss);
ss = ss->next; ss = next;
} }
} }
void addclasses(SCOPE* s, CLASS* c) { void addclasses(SCOPE* s, CLASS* c) {
CLASS* next;
while(c != NULL) { while(c != NULL) {
next = c->next;
addclass(s, c); addclass(s, c);
c = c->next; c = next;
} }
} }

View File

@ -127,7 +127,7 @@ LINEBLOCK* compilecallln(CLASS* c, SUBROUTCALL* call) {
// temporary ignore list for OS functions // temporary ignore list for OS functions
char* ignoresubdecs[] = { char* ignoresubdecs[] = {
"printInt", "void" "printInt", "void", "peek", "int"
}; };
int ignorecount = sizeof(ignoresubdecs) / sizeof(char*); int ignorecount = sizeof(ignoresubdecs) / sizeof(char*);

View File

@ -106,6 +106,7 @@ TERM* parsetermnullified(PARSER* p) {
t->type = unaryopterm; t->type = unaryopterm;
next(p); next(p);
t->expression = parseterm(p); t->expression = parseterm(p);
t->expression->next = NULL;
} else if(!strcmp(p->current->token, "(")) { } else if(!strcmp(p->current->token, "(")) {
next(p); next(p);
t->type = innerexpression; t->type = innerexpression;
@ -148,7 +149,7 @@ TERM* parseexpressionnullified(PARSER* p) {
while(isop(p->current)) { while(isop(p->current)) {
current->op = p->current->token[0]; current->op = p->current->token[0];
next(p); next(p);
nextt = parsetermnullified(p); nextt = parseterm(p);
current->next = nextt; current->next = nextt;
current = nextt; current = nextt;
} }
@ -466,6 +467,7 @@ PARAMETER* parseparameters(PARSER* p) {
PARAMETER* head = parseparameter(p); PARAMETER* head = parseparameter(p);
PARAMETER* current = head; PARAMETER* current = head;
while(!strcmp(p->current->token, ",")) { while(!strcmp(p->current->token, ",")) {
next(p);
current->next = parseparameter(p); current->next = parseparameter(p);
current = current->next; current = current->next;
} }
@ -518,7 +520,7 @@ SUBDEC* parsesubroutdec(PARSER* p) {
SUBDEC* parsesubroutdecs(PARSER* p) { SUBDEC* parsesubroutdecs(PARSER* p) {
SUBDEC* head = parsesubroutdec(p); SUBDEC* head = parsesubroutdec(p);
SUBDEC* current= head; SUBDEC* current = head;
SUBDEC* next; SUBDEC* next;
while(next = parsesubroutdec(p), next != NULL) { while(next = parsesubroutdec(p), next != NULL) {
current->next = next; current->next = next;

6
util.c
View File

@ -13,6 +13,12 @@ char* ezheapstr(char* str) {
return heapstr(str, strlen(str)); return heapstr(str, strlen(str));
} }
void* copy(void* v, int sz) {
void* copy = malloc(sz);
memcpy(copy, v, sz);
return copy;
}
int countplaces(int n) { int countplaces(int n) {
int places = 1; int places = 1;
int divisor = 1; int divisor = 1;

1
util.h
View File

@ -14,6 +14,7 @@ char* heapstr(char* str, int len);
char* ezheapstr(char* str); char* ezheapstr(char* str);
int countplaces(int n); int countplaces(int n);
char* itoa(int i); char* itoa(int i);
void* copy(void* v, int sz);
void printstrlist(STRINGLIST* strlist, FILE* stream); void printstrlist(STRINGLIST* strlist, FILE* stream);
void freestrlist(STRINGLIST* strlist); void freestrlist(STRINGLIST* strlist);