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;
}
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_begin_of_nested_function_definition(self, top_frame, stop_index)) {
nested_functions_count++;
}
if(is_end_of_nested_function_definition(self, top_frame, stop_index)) {
count_nested_functions--;
if (is_end_of_nested_function_definition(self, top_frame, stop_index)) {
nested_functions_count--;
}
} while (current->type != TOKENTYPE_ARROW || count_nested_functions != 0);
} 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;
do {
current = Interpreter_ExpectToken(self, top_frame, stop_index);
top_frame->instruction_pointer++;
do {
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,7 +241,7 @@ 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
if (1 == 1
&& type == type_mapping[i]
&& StringView_Equal(type_name, StringView_FromString(name_mapping[i]))
) {
@ -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

View file

@ -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