From 78bb3321d8608cfd0d586b8fe119ed77ad625498 Mon Sep 17 00:00:00 2001 From: Alexander Acker Date: Tue, 8 Oct 2024 14:12:31 +0200 Subject: [PATCH] implements inner function definition in function body --- src/interpreter.c | 59 ++++++++++++--------- test-inputs/innere_function_definition.flup | 23 ++++---- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/interpreter.c b/src/interpreter.c index 72ad6b3..b677eb8 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -148,7 +148,7 @@ int Interpreter_ParseFunction(Interpreter* self, CallFrame* top_frame, size_t st return EXIT_FAILURE; } - int count_nested_functions = 0; + int nested_functions_count = 0; FlupFunctionAlternative** fdef = &f->alternatives; while (alternative_start->type == TOKENTYPE_PIPE) { @@ -163,26 +163,33 @@ int Interpreter_ParseFunction(Interpreter* self, CallFrame* top_frame, size_t st if (current == NULL) { return EXIT_FAILURE; } - // TODO: Count inner colon and semicolon to allow for nested functions - if(is_begin_of_nested_function_definition(self, top_frame, stop_index)) { - count_nested_functions++; - } - if(is_end_of_nested_function_definition(self, top_frame, stop_index)) { - count_nested_functions--; + if (is_begin_of_nested_function_definition(self, top_frame, stop_index)) { + nested_functions_count++; } - } while (current->type != TOKENTYPE_ARROW || count_nested_functions != 0); + if (is_end_of_nested_function_definition(self, top_frame, stop_index)) { + nested_functions_count--; + } + } while (current->type != TOKENTYPE_ARROW || nested_functions_count != 0); size_t condition_end = top_frame->instruction_pointer - 2; + // condition body parsing + nested_functions_count = 0; size_t body_start = top_frame->instruction_pointer; + current = Interpreter_ExpectToken(self, top_frame, stop_index); do { - current = Interpreter_ExpectToken(self, top_frame, stop_index); - top_frame->instruction_pointer++; if (current == NULL) { return EXIT_FAILURE; } - // TODO: Count inner colon and semicolon to allow for nested functions - } while (current->type != TOKENTYPE_PIPE && current->type != TOKENTYPE_SEMICOLON); - top_frame->instruction_pointer--; + if (current->type == TOKENTYPE_COLON) { + nested_functions_count++; + } + if (current->type == TOKENTYPE_SEMICOLON && nested_functions_count > 0) { + nested_functions_count--; + } + top_frame->instruction_pointer++; + current = Interpreter_ExpectToken(self, top_frame, stop_index); + } while ((current->type != TOKENTYPE_PIPE && current->type != TOKENTYPE_SEMICOLON) || + nested_functions_count > 0); size_t body_end = top_frame->instruction_pointer; *fdef = CallFrame_Reserve(top_frame, sizeof(**pdef), alignof(**pdef)); @@ -234,10 +241,10 @@ bool _are_types_compatible(enum ValueType type, StringView type_name) const char* name_mapping[] = {"int", "double", "bool", "boolean"}; for (size_t i = 0; i < sizeof(name_mapping) / sizeof(name_mapping[0]); i++) { - if (1==1 - && type == type_mapping[i] + if (1 == 1 + && type == type_mapping[i] && StringView_Equal(type_name, StringView_FromString(name_mapping[i])) - ) { + ) { return true; } } @@ -403,7 +410,7 @@ int BuiltinFunction_Plus(CallFrame* top_frame) case VALUETYPE_BOOLEAN: result.get.boolean = v2.get.boolean + v1.get.boolean; break; - } + } top_frame->instruction_pointer++; return CallFrame_StackPush(top_frame, &result); @@ -436,7 +443,7 @@ int BuiltinFunction_Minus(CallFrame* top_frame) case VALUETYPE_BOOLEAN: result.get.boolean = v2.get.boolean - v1.get.boolean; break; - } + } top_frame->instruction_pointer++; return CallFrame_StackPush(top_frame, &result); @@ -660,9 +667,9 @@ int Interpreter_ExecuteNext(Interpreter* self, size_t stop_token) case TOKENTYPE_NONE: case TOKENTYPE_ERROR: return EXIT_FAILURE; - } + } - return EXIT_SUCCESS; + return EXIT_SUCCESS; } bool Value_IsTruthy(Value* v) @@ -728,7 +735,7 @@ int Interpreter_RunFrame(Interpreter* self) if (Value_IsTruthy(&alternative_success)) { // into body top_frame->instruction_pointer = top_frame->alternative->body_token_start; - } else { + } else { if (top_frame->alternative->next == NULL) { // TODO: Error message return EXIT_FAILURE; @@ -767,10 +774,10 @@ int Interpreter_RunFrame(Interpreter* self) return EXIT_FAILURE; } - char function_name[top_frame->self_function->name.length + 1]; - memset(function_name, 0, sizeof(function_name)); - StringView_Paste(function_name, top_frame->self_function->name); - printf("%s\n", function_name); + // char function_name[top_frame->self_function->name.length + 1]; + // memset(function_name, 0, sizeof(function_name)); + // StringView_Paste(function_name, top_frame->self_function->name); + // printf("%s\n", function_name); if (! _are_types_compatible(return_value.type, top_frame->self_function->return_type)) { // TODO: Error message @@ -795,7 +802,7 @@ int Interpreter_RunFrame(Interpreter* self) int Interpreter_Run(Interpreter* self) { size_t frame_count = 1; - do { + do { if (Interpreter_RunFrame(self)) { // TODO: Return the error code return EXIT_FAILURE; diff --git a/test-inputs/innere_function_definition.flup b/test-inputs/innere_function_definition.flup index a6f835b..0a56733 100644 --- a/test-inputs/innere_function_definition.flup +++ b/test-inputs/innere_function_definition.flup @@ -1,9 +1,11 @@ -add: int a int b -> int - | 1 -> a b + - ; -ad: int a int b -> int - | 1 -> a b + +test: int a -> int + | a 1 == -> inner: int b -> int + | b 0 == -> 5 fuenf + | 1 1 == -> 7 sieben + ; + a inner + | 1 1 == -> 3 drei ; fuenf: int a -> int @@ -14,12 +16,9 @@ drei: int a -> int | 1 -> a ; -test: int a -> int - | fb: int b -> int - | 1 b == -> 0 0 add - | 1 1 == -> 1 0 ad - ; - a fb -> 1 drei - | 1 1 == -> 3 fb + +sieben: int a -> int + | 1 -> a ; + 0 test \ No newline at end of file