Finish compiler

This commit is contained in:
Augusto Gunsch 2020-12-31 17:38:41 -03:00
parent b9a553b107
commit b7fe5b8f45
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
5 changed files with 10 additions and 10 deletions

View File

@ -283,8 +283,8 @@ void addlocalvars(SCOPE* s, VARDEC* localvars) {
} }
} }
void addparameters(SCOPE* s, PARAMETER* params) { void addparameters(SCOPE* s, bool isformethod, PARAMETER* params) {
int i = 0; int i = isformethod ? 1 : 0;
while(params != NULL) { while(params != NULL) {
addparameter(s, params, &i); addparameter(s, params, &i);
params = params->next; params = params->next;

View File

@ -40,7 +40,7 @@ struct compiler;
// Group adding // Group adding
void addclassvardecs(struct compiler* c, SCOPE* s, CLASSVARDEC* classvardecs); void addclassvardecs(struct compiler* c, SCOPE* s, CLASSVARDEC* classvardecs);
void addlocalvars(SCOPE* s, VARDEC* localvars); void addlocalvars(SCOPE* s, VARDEC* localvars);
void addparameters(SCOPE* s, PARAMETER* params); void addparameters(SCOPE* s, bool isformethod, PARAMETER* params);
// Scope handling // Scope handling
SCOPE* mkscope(SCOPE* prev); SCOPE* mkscope(SCOPE* prev);

View File

@ -176,8 +176,7 @@ LINEBLOCK* compilekeywordconst(SCOPE* s, TERM* t) {
if(!strcmp(t->string, "true")) return pushtrue(); if(!strcmp(t->string, "true")) return pushtrue();
if(!strcmp(t->string, "false")) return mklnblk(pushfalse()); if(!strcmp(t->string, "false")) return mklnblk(pushfalse());
if(!strcmp(t->string, "this")) return mklnblk(pushthisadd()); if(!strcmp(t->string, "this")) return mklnblk(pushthisadd());
eprintf("Unsupported keyword '%s'\n", t->string); return mklnblk(pushconstant(0));
exit(1);
} }
char* toascii(char c) { char* toascii(char c) {
@ -521,7 +520,7 @@ LINEBLOCK* compilesubroutdec(COMPILER* c, SCOPE* s, CLASS* cl, SUBROUTDEC* sd) {
// must switch all of these // must switch all of these
SCOPE* myscope = mkscope(s); SCOPE* myscope = mkscope(s);
if(sd->parameters != NULL) if(sd->parameters != NULL)
addparameters(myscope, sd->parameters); addparameters(myscope, sd->subroutclass == method, sd->parameters);
if(sd->subroutclass == function) if(sd->subroutclass == function)
return compilefundec(c, myscope, cl, sd); return compilefundec(c, myscope, cl, sd);
if(sd->subroutclass == constructor) if(sd->subroutclass == constructor)

View File

@ -28,6 +28,7 @@ void populatemath() {
CLASS* mathclass = addclass("Math"); CLASS* mathclass = addclass("Math");
adddec(mathclass, function, "int", "multiply"); adddec(mathclass, function, "int", "multiply");
adddec(mathclass, function, "int", "divide"); adddec(mathclass, function, "int", "divide");
adddec(mathclass, function, "int", "abs");
adddec(mathclass, function, "int", "min"); adddec(mathclass, function, "int", "min");
adddec(mathclass, function, "int", "max"); adddec(mathclass, function, "int", "max");
adddec(mathclass, function, "int", "sqrt"); adddec(mathclass, function, "int", "sqrt");
@ -61,7 +62,7 @@ void populateoutput() {
adddec(outclass, function, "void", "printChar"); adddec(outclass, function, "void", "printChar");
adddec(outclass, function, "void", "printString"); adddec(outclass, function, "void", "printString");
adddec(outclass, function, "void", "printInt"); adddec(outclass, function, "void", "printInt");
adddec(outclass, function, "void", "printLn"); adddec(outclass, function, "void", "println");
adddec(outclass, function, "void", "backSpace"); adddec(outclass, function, "void", "backSpace");
} }

View File

@ -201,10 +201,10 @@ TOKEN* tokenize(char* file) {
FILE* input = fopen(file, "r"); FILE* input = fopen(file, "r");
unsigned char c; unsigned char c;
while(c = fgetc(input), !feof(input)) { while(!feof(input)) {
if(c == '\n') { c = fgetc(input);
if(c == '\n')
lnscount++; lnscount++;
}
else if(c == '/' && handlecomment(input, &lnscount)) else if(c == '/' && handlecomment(input, &lnscount))
continue; continue;
else if(c == '"') { else if(c == '"') {