implements inner function definition in function body

This commit is contained in:
Alexander Acker 2024-10-08 14:12:31 +02:00
parent 2fefd8c04f
commit 78bb3321d8
2 changed files with 44 additions and 38 deletions

View file

@ -148,7 +148,7 @@ int Interpreter_ParseFunction(Interpreter* self, CallFrame* top_frame, size_t st
return EXIT_FAILURE; return EXIT_FAILURE;
} }
int count_nested_functions = 0; int nested_functions_count = 0;
FlupFunctionAlternative** fdef = &f->alternatives; FlupFunctionAlternative** fdef = &f->alternatives;
while (alternative_start->type == TOKENTYPE_PIPE) { while (alternative_start->type == TOKENTYPE_PIPE) {
@ -163,26 +163,33 @@ int Interpreter_ParseFunction(Interpreter* self, CallFrame* top_frame, size_t st
if (current == NULL) { if (current == NULL) {
return EXIT_FAILURE; 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)) {
if(is_begin_of_nested_function_definition(self, top_frame, stop_index)) { nested_functions_count++;
count_nested_functions++;
}
if(is_end_of_nested_function_definition(self, top_frame, stop_index)) {
count_nested_functions--;
} }
} 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; size_t condition_end = top_frame->instruction_pointer - 2;
// condition body parsing
nested_functions_count = 0;
size_t body_start = top_frame->instruction_pointer; size_t body_start = top_frame->instruction_pointer;
current = Interpreter_ExpectToken(self, top_frame, stop_index);
do { do {
current = Interpreter_ExpectToken(self, top_frame, stop_index);
top_frame->instruction_pointer++;
if (current == NULL) { if (current == NULL) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// TODO: Count inner colon and semicolon to allow for nested functions if (current->type == TOKENTYPE_COLON) {
} while (current->type != TOKENTYPE_PIPE && current->type != TOKENTYPE_SEMICOLON); nested_functions_count++;
top_frame->instruction_pointer--; }
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; size_t body_end = top_frame->instruction_pointer;
*fdef = CallFrame_Reserve(top_frame, sizeof(**pdef), alignof(**pdef)); *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"}; const char* name_mapping[] = {"int", "double", "bool", "boolean"};
for (size_t i = 0; i < sizeof(name_mapping) / sizeof(name_mapping[0]); i++) { for (size_t i = 0; i < sizeof(name_mapping) / sizeof(name_mapping[0]); i++) {
if (1==1 if (1 == 1
&& type == type_mapping[i] && type == type_mapping[i]
&& StringView_Equal(type_name, StringView_FromString(name_mapping[i])) && StringView_Equal(type_name, StringView_FromString(name_mapping[i]))
) { ) {
return true; return true;
} }
} }
@ -403,7 +410,7 @@ int BuiltinFunction_Plus(CallFrame* top_frame)
case VALUETYPE_BOOLEAN: case VALUETYPE_BOOLEAN:
result.get.boolean = v2.get.boolean + v1.get.boolean; result.get.boolean = v2.get.boolean + v1.get.boolean;
break; break;
} }
top_frame->instruction_pointer++; top_frame->instruction_pointer++;
return CallFrame_StackPush(top_frame, &result); return CallFrame_StackPush(top_frame, &result);
@ -436,7 +443,7 @@ int BuiltinFunction_Minus(CallFrame* top_frame)
case VALUETYPE_BOOLEAN: case VALUETYPE_BOOLEAN:
result.get.boolean = v2.get.boolean - v1.get.boolean; result.get.boolean = v2.get.boolean - v1.get.boolean;
break; break;
} }
top_frame->instruction_pointer++; top_frame->instruction_pointer++;
return CallFrame_StackPush(top_frame, &result); return CallFrame_StackPush(top_frame, &result);
@ -660,9 +667,9 @@ int Interpreter_ExecuteNext(Interpreter* self, size_t stop_token)
case TOKENTYPE_NONE: case TOKENTYPE_NONE:
case TOKENTYPE_ERROR: case TOKENTYPE_ERROR:
return EXIT_FAILURE; return EXIT_FAILURE;
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
bool Value_IsTruthy(Value* v) bool Value_IsTruthy(Value* v)
@ -728,7 +735,7 @@ int Interpreter_RunFrame(Interpreter* self)
if (Value_IsTruthy(&alternative_success)) { if (Value_IsTruthy(&alternative_success)) {
// into body // into body
top_frame->instruction_pointer = top_frame->alternative->body_token_start; top_frame->instruction_pointer = top_frame->alternative->body_token_start;
} else { } else {
if (top_frame->alternative->next == NULL) { if (top_frame->alternative->next == NULL) {
// TODO: Error message // TODO: Error message
return EXIT_FAILURE; return EXIT_FAILURE;
@ -767,10 +774,10 @@ int Interpreter_RunFrame(Interpreter* self)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
char function_name[top_frame->self_function->name.length + 1]; // char function_name[top_frame->self_function->name.length + 1];
memset(function_name, 0, sizeof(function_name)); // memset(function_name, 0, sizeof(function_name));
StringView_Paste(function_name, top_frame->self_function->name); // StringView_Paste(function_name, top_frame->self_function->name);
printf("%s\n", function_name); // printf("%s\n", function_name);
if (! _are_types_compatible(return_value.type, top_frame->self_function->return_type)) { if (! _are_types_compatible(return_value.type, top_frame->self_function->return_type)) {
// TODO: Error message // TODO: Error message
@ -795,7 +802,7 @@ int Interpreter_RunFrame(Interpreter* self)
int Interpreter_Run(Interpreter* self) int Interpreter_Run(Interpreter* self)
{ {
size_t frame_count = 1; size_t frame_count = 1;
do { do {
if (Interpreter_RunFrame(self)) { if (Interpreter_RunFrame(self)) {
// TODO: Return the error code // TODO: Return the error code
return EXIT_FAILURE; return EXIT_FAILURE;

View file

@ -1,9 +1,11 @@
add: int a int b -> int
| 1 -> a b +
;
ad: int a int b -> int test: int a -> int
| 1 -> a b + | a 1 == -> inner: int b -> int
| b 0 == -> 5 fuenf
| 1 1 == -> 7 sieben
;
a inner
| 1 1 == -> 3 drei
; ;
fuenf: int a -> int fuenf: int a -> int
@ -14,12 +16,9 @@ drei: int a -> int
| 1 -> a | 1 -> a
; ;
test: int a -> int
| fb: int b -> int sieben: int a -> int
| 1 b == -> 0 0 add | 1 -> a
| 1 1 == -> 1 0 ad
;
a fb -> 1 drei
| 1 1 == -> 3 fb
; ;
0 test 0 test