implements inner function definition in function condition
This commit is contained in:
parent
b7f11d1fc2
commit
2fefd8c04f
3 changed files with 58 additions and 9 deletions
|
@ -73,6 +73,16 @@ Token* Interpreter_ExpectToken(Interpreter* self, CallFrame* top_frame, size_t s
|
|||
return Interpreter_GetToken(self, top_frame->instruction_pointer);
|
||||
}
|
||||
|
||||
bool is_begin_of_nested_function_definition(Interpreter* self, CallFrame* top_frame, size_t stop_index) {
|
||||
Token* possible_colon = Interpreter_ExpectToken(self, top_frame, stop_index);
|
||||
return possible_colon != NULL && possible_colon->type == TOKENTYPE_COLON;
|
||||
}
|
||||
|
||||
bool is_end_of_nested_function_definition(Interpreter* self, CallFrame* top_frame, size_t stop_index) {
|
||||
Token* possible_semicolon = Interpreter_ExpectToken(self, top_frame, stop_index);
|
||||
return possible_semicolon != NULL && possible_semicolon->type == TOKENTYPE_SEMICOLON;
|
||||
}
|
||||
|
||||
int Interpreter_ParseFunction(Interpreter* self, CallFrame* top_frame, size_t stop_index)
|
||||
{
|
||||
FlupFunction* f = CallFrame_Reserve(top_frame, sizeof(FlupFunction), alignof(FlupFunction));
|
||||
|
@ -138,11 +148,14 @@ int Interpreter_ParseFunction(Interpreter* self, CallFrame* top_frame, size_t st
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int count_nested_functions = 0;
|
||||
|
||||
FlupFunctionAlternative** fdef = &f->alternatives;
|
||||
while (alternative_start->type == TOKENTYPE_PIPE) {
|
||||
top_frame->instruction_pointer++;
|
||||
top_frame->instruction_pointer++; // pipe Token überspringen
|
||||
size_t condition_start = top_frame->instruction_pointer;
|
||||
|
||||
// condition parsing
|
||||
Token* current;
|
||||
do {
|
||||
current = Interpreter_ExpectToken(self, top_frame, stop_index);
|
||||
|
@ -151,7 +164,13 @@ int Interpreter_ParseFunction(Interpreter* self, CallFrame* top_frame, size_t st
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
// TODO: Count inner colon and semicolon to allow for nested functions
|
||||
} while (current->type != TOKENTYPE_ARROW);
|
||||
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--;
|
||||
}
|
||||
} while (current->type != TOKENTYPE_ARROW || count_nested_functions != 0);
|
||||
size_t condition_end = top_frame->instruction_pointer - 2;
|
||||
|
||||
size_t body_start = top_frame->instruction_pointer;
|
||||
|
@ -748,9 +767,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);
|
||||
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
|
||||
|
|
12
src/main.c
12
src/main.c
|
@ -39,7 +39,7 @@ int tokenize_all(StringView source, DynamicArray* a)
|
|||
|
||||
char* load_file_string(StringView path)
|
||||
{
|
||||
FILE* stream = fopen(path.source, "r");
|
||||
FILE* stream = fopen(path.source, "rb");
|
||||
if (stream == NULL) {
|
||||
fprintf(stderr, "Fatal Error: Failed to open file at %*s\n", (int) path.length, path.source);
|
||||
return NULL;
|
||||
|
@ -56,7 +56,10 @@ char* load_file_string(StringView path)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
rewind(stream);
|
||||
if (fseek(stream, 0, SEEK_SET)) {
|
||||
perror("fseek");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* buffer = malloc(length + 1);
|
||||
if (buffer == NULL) {
|
||||
|
@ -64,9 +67,10 @@ char* load_file_string(StringView path)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
size_t objects_read = fread(buffer, length, 1, stream);
|
||||
if (objects_read != 1) {
|
||||
size_t objects_read = fread(buffer, 1, length, stream);
|
||||
if (objects_read != length) {
|
||||
fprintf(stderr, "Fatal Error: Failed read %li bytes from script file, got only %li\n", length, objects_read);
|
||||
;
|
||||
free(buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
|
25
test-inputs/innere_function_definition.flup
Normal file
25
test-inputs/innere_function_definition.flup
Normal file
|
@ -0,0 +1,25 @@
|
|||
add: int a int b -> int
|
||||
| 1 -> a b +
|
||||
;
|
||||
|
||||
ad: int a int b -> int
|
||||
| 1 -> a b +
|
||||
;
|
||||
|
||||
fuenf: int a -> int
|
||||
| 1 -> a
|
||||
;
|
||||
|
||||
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
|
||||
;
|
||||
0 test
|
Loading…
Reference in a new issue