From: Michael D. Lowis Date: Thu, 6 May 2021 20:50:16 +0000 (-0400) Subject: functions and function calls fully working now X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=daa7ec5b6e14a8d20b3a736f7bfdb874bda49205;p=proto%2Fobnc.git functions and function calls fully working now --- diff --git a/cerise/backend/c99/codegen.c b/cerise/backend/c99/codegen.c index 93e1807..6c1fd3d 100644 --- a/cerise/backend/c99/codegen.c +++ b/cerise/backend/c99/codegen.c @@ -161,15 +161,19 @@ void codegen_startproc(Parser* p, Symbol* proc) } else { - char* export = (proc->export ? "" : "static"); + char* export = (proc->export ? "" : "static "); printf("\n%s%s %s_%s(", export, - TypeNames[proc->type->form], + (proc->type ? TypeNames[proc->type->form] : "void"), p->name, proc->name); for (Symbol* arg = proc->desc; arg; arg = arg->next) { printf("%s %s", TypeNames[arg->type->form], arg->name); + if (arg->next) + { + printf(", "); + } } printf(") {\n"); } @@ -273,3 +277,12 @@ void codegen_setarg(Parser* p, Item* item, bool firstarg) { load_var(p, item); } + +void codegen_return(Parser* p, Item* item) +{ + if (item) + { + load_var(p, item); + printf(" return _T%d;\n", item->reg); + } +} diff --git a/cerise/backend/test/codegen.c b/cerise/backend/test/codegen.c index 23caad8..ad2db8b 100644 --- a/cerise/backend/test/codegen.c +++ b/cerise/backend/test/codegen.c @@ -115,3 +115,8 @@ void codegen_setarg(Parser* p, Item* item, bool firstarg) { (void)p, (void)item, (void)firstarg; } + +void codegen_return(Parser* p, Item* item) +{ + (void)p, (void)item; +} diff --git a/cerise/backend/x86_64/codegen.c b/cerise/backend/x86_64/codegen.c index 7f1dc3a..dccec0c 100644 --- a/cerise/backend/x86_64/codegen.c +++ b/cerise/backend/x86_64/codegen.c @@ -343,3 +343,8 @@ void codegen_setarg(Parser* p, Item* item, bool firstarg) { (void)p, (void)item, (void)firstarg; } + +void codegen_return(Parser* p, Item* item) +{ + (void)p, (void)item; +} diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index e38d424..de6938f 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -222,6 +222,7 @@ void codegen_endif(Parser* p, long elsifs, Item* item); void codegen_prepcall(Parser* p, Item* item); void codegen_call(Parser* p, Item* item, Item* args); void codegen_setarg(Parser* p, Item* item, bool firstarg); +void codegen_return(Parser* p, Item* item); /* diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index a46e0fe..da646dd 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -4,6 +4,31 @@ #include #include +/* TODO + * Implement logical operators: and, or, not + * Implement arrays and array access + * Implement module scoped identifiers + * Implement strings types + * Implement records and record access + * Implement while, for, do/repeat loops + * Track uninitialized variables/globals + * Implement pointers + * Ownership semantics: Ownership you can count on + * Concurrency: Biased reference counting + * Nested procedure definitions + * Implement record extension + * Cleanup the lexer +*/ + +/* Order of Operations for Implementing Memory Management + + * Implement new operator + * Enforce single owner pointers + * Implement borrowed pointers + * Implement biased refcounting for borrowed pointers + +*/ + //#define TRACE #ifdef TRACE static int Indent = 0; @@ -61,36 +86,6 @@ static void init_item(Item* item, Symbol* sym) *****************************************************************************/ static void expression(Parser* p, Item* item); -//RULE(param_list) -//{ -//// (void)item; -//// int nargs = 0; -//// Item argdef = {0}; -//// (void)argdef; -//// if (!matches(p, ')')) -//// { -//// Item arg = {0}; -//// expression(p, &arg); -//// // if check_types() -//// codegen_setarg(p, &arg, true); -//// // else check_array_type() -//// nargs++; -//// -//// while (!matches(p, ')')) -//// { -//// expect(p, ','); -//// expression(p, &arg); -//// // if check_types() -//// codegen_setarg(p, &arg, false); -//// // else check_array_type() -//// nargs++; -//// } -//// } -// -// Symbol* args = item->sym; -// -//} - RULE(qualident) { char* name = expect_text(p, IDENT); @@ -101,17 +96,6 @@ RULE(qualident) item->imm.s = sym->name; } - -// item->mode = sym->class; -// item->type = sym->type; -// item->imm = sym->imm; - -// if (sym->class == SYM_CONST) -// { -// item->mode = ITEM_CONST; -// item->imm = sym->imm; -// } - // if (accept(p, '.')) // { // expect(p, IDENT); @@ -190,8 +174,6 @@ RULE(factor) designator(p, item); if (accept(p, '(')) { -// codegen_prepcall(p, item); -// param_list(p, item); Symbol* proc = symbol_get(p, SYM_PROC, item->imm.s); Symbol* args = proc->desc; Item arglist = {0}; @@ -206,19 +188,36 @@ RULE(factor) *currarg = argval; currarg = &(argval->next); args = args->next; + if (args) + { + expect(p, ','); + } } + if (args) { error(p, "too few arguments to function '%s'", proc->name); } else if (!matches(p, ')')) { - error(p, "too many arguments to function '%s'", proc->name); + bool comma = accept(p, ','); + if (comma && matches(p, ')')) + { + error(p, "trailing comma in argument list"); + } + else + { + error(p, "too many arguments to function '%s'", proc->name); + } } codegen_call(p, item, arglist.next); expect(p, ')'); } break; + + default: + printf("unknown factor: %d\n", peek(p)->type); + break; } } @@ -518,10 +517,10 @@ RULE(proc_decl) var_decl(p, item); } - while (matches(p, PROCEDURE)) - { - proc_decl(p, item); - } +// while (matches(p, PROCEDURE)) +// { +// proc_decl(p, item); +// } /* parse the body of the procedure */ expect(p, BEGIN); @@ -532,8 +531,13 @@ RULE(proc_decl) if (accept(p, RETURN)) { expression(p, item); + codegen_return(p, item); expect(p, ';'); } + else + { + codegen_return(p, NULL); + } expect(p, END); symbol_closescope(p); diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 115f3c3..fc5d28e 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -20,22 +20,28 @@ var a : Bool b : Int c : Int + d : Real procedure Foo*(e : Int, z : Int) : Int const FOO = 2 type foo = Int var z : Int + +# procedure Bar() : Int +# begin +# end + begin - c = b * b; +# c = b * b; return c; end begin - a = true; - a = A; - b = 24; - b = B; - +# a = true; +# a = A; +# b = 24; +# b = B; +# # # Unary ops # b = +b; # b = -b; @@ -65,7 +71,7 @@ begin # # Complex arithmetic # c = b + b * b + b; # - +# # # If statements # if 1 == 1 then # c = 1; @@ -92,6 +98,7 @@ begin # else # c = 3; # end - - c = Foo(1,2); +# + # Function calls + c = Foo(1,2); end