From b7fe5b8f45bd9a8247343989ecc6285e346fa075 Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Thu, 31 Dec 2020 17:38:41 -0300 Subject: [PATCH] Finish compiler --- compiler/compiler-scopes.c | 4 ++-- compiler/compiler-scopes.h | 2 +- compiler/compiler.c | 5 ++--- misc/os.c | 3 ++- tokenizer/tokenizer.c | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/compiler/compiler-scopes.c b/compiler/compiler-scopes.c index 8276f50..7d7ed7f 100644 --- a/compiler/compiler-scopes.c +++ b/compiler/compiler-scopes.c @@ -283,8 +283,8 @@ void addlocalvars(SCOPE* s, VARDEC* localvars) { } } -void addparameters(SCOPE* s, PARAMETER* params) { - int i = 0; +void addparameters(SCOPE* s, bool isformethod, PARAMETER* params) { + int i = isformethod ? 1 : 0; while(params != NULL) { addparameter(s, params, &i); params = params->next; diff --git a/compiler/compiler-scopes.h b/compiler/compiler-scopes.h index 2f2a449..2664227 100644 --- a/compiler/compiler-scopes.h +++ b/compiler/compiler-scopes.h @@ -40,7 +40,7 @@ struct compiler; // Group adding void addclassvardecs(struct compiler* c, SCOPE* s, CLASSVARDEC* classvardecs); void addlocalvars(SCOPE* s, VARDEC* localvars); -void addparameters(SCOPE* s, PARAMETER* params); +void addparameters(SCOPE* s, bool isformethod, PARAMETER* params); // Scope handling SCOPE* mkscope(SCOPE* prev); diff --git a/compiler/compiler.c b/compiler/compiler.c index 33f3f24..b8c5554 100644 --- a/compiler/compiler.c +++ b/compiler/compiler.c @@ -176,8 +176,7 @@ LINEBLOCK* compilekeywordconst(SCOPE* s, TERM* t) { if(!strcmp(t->string, "true")) return pushtrue(); if(!strcmp(t->string, "false")) return mklnblk(pushfalse()); if(!strcmp(t->string, "this")) return mklnblk(pushthisadd()); - eprintf("Unsupported keyword '%s'\n", t->string); - exit(1); + return mklnblk(pushconstant(0)); } char* toascii(char c) { @@ -521,7 +520,7 @@ LINEBLOCK* compilesubroutdec(COMPILER* c, SCOPE* s, CLASS* cl, SUBROUTDEC* sd) { // must switch all of these SCOPE* myscope = mkscope(s); if(sd->parameters != NULL) - addparameters(myscope, sd->parameters); + addparameters(myscope, sd->subroutclass == method, sd->parameters); if(sd->subroutclass == function) return compilefundec(c, myscope, cl, sd); if(sd->subroutclass == constructor) diff --git a/misc/os.c b/misc/os.c index 6985273..3329548 100644 --- a/misc/os.c +++ b/misc/os.c @@ -28,6 +28,7 @@ void populatemath() { CLASS* mathclass = addclass("Math"); adddec(mathclass, function, "int", "multiply"); adddec(mathclass, function, "int", "divide"); + adddec(mathclass, function, "int", "abs"); adddec(mathclass, function, "int", "min"); adddec(mathclass, function, "int", "max"); adddec(mathclass, function, "int", "sqrt"); @@ -61,7 +62,7 @@ void populateoutput() { adddec(outclass, function, "void", "printChar"); adddec(outclass, function, "void", "printString"); adddec(outclass, function, "void", "printInt"); - adddec(outclass, function, "void", "printLn"); + adddec(outclass, function, "void", "println"); adddec(outclass, function, "void", "backSpace"); } diff --git a/tokenizer/tokenizer.c b/tokenizer/tokenizer.c index cf14b4e..6831655 100644 --- a/tokenizer/tokenizer.c +++ b/tokenizer/tokenizer.c @@ -201,10 +201,10 @@ TOKEN* tokenize(char* file) { FILE* input = fopen(file, "r"); unsigned char c; - while(c = fgetc(input), !feof(input)) { - if(c == '\n') { + while(!feof(input)) { + c = fgetc(input); + if(c == '\n') lnscount++; - } else if(c == '/' && handlecomment(input, &lnscount)) continue; else if(c == '"') {