implements inner function definition in function body
This commit is contained in:
parent
2fefd8c04f
commit
78bb3321d8
2 changed files with 44 additions and 38 deletions
|
@ -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)) {
|
if (is_end_of_nested_function_definition(self, top_frame, stop_index)) {
|
||||||
count_nested_functions--;
|
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;
|
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;
|
||||||
do {
|
|
||||||
current = Interpreter_ExpectToken(self, top_frame, stop_index);
|
current = Interpreter_ExpectToken(self, top_frame, stop_index);
|
||||||
top_frame->instruction_pointer++;
|
do {
|
||||||
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,7 +241,7 @@ 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]))
|
||||||
) {
|
) {
|
||||||
|
@ -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
|
||||||
|
|
|
@ -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
|
Loading…
Reference in a new issue