From e2c45c0f5087c8639f89b3588064c1e17a72c627 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Mon, 22 Jan 2024 00:48:12 -0500 Subject: keyword branch in func compilation --- compiler.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 2 deletions(-) diff --git a/compiler.c b/compiler.c index edcd2b1..f135d9e 100644 --- a/compiler.c +++ b/compiler.c @@ -455,6 +455,68 @@ Variable _op_coerce(Variable *base, Variable *to_coerce) { // with an array => pointer + 8 bytes (length of array at start) + index * size of data // with a pointer => pointer + index * size of data +char *_op_get_register(int reg, int size) { + Vector out = vect_init(sizeof(char)); + char add = 'r'; + + switch(size) { + case 1: + case 2: + break; + case 4: + add = 'e'; + case 8: + vect_push(&out, &add); + break; + default: + printf("ERROR: invalid register size %d (this is a compiler issue)\n", size); + vect_push(&out, &add); + break; + } + + switch(reg) { + case 1: + case 2: + case 3: + case 4: + add = 'a' + (reg - 1); + break; + case 5: + case 7: + add = 's'; + break; + case 6: + add = 'd'; + break; + case 8: + add = 'b'; + break; + default: + printf("ERROR: Unknown register %d (this is a compiler issue)\n", reg); + break; + } + vect_push(&out, &add); + + //TODO: Ending + + return out.data; +} + +char *_op_get_location(Variable *var, int location) { + Vector out = vect_init(sizeof(char)); + if(location < 0) { + } else if (location == 0) { + } else { + } +} + +void var_swap_register(CompData *out, Variable *swap, int new_reg) { + if(swap->location == new_reg) + return; + + +} + void var_op_reference(CompData *out, Variable *store, Variable *from) {} void var_op_dereference(CompData *out, Variable *store, Variable *from) {} void var_op_index(CompData *out, Variable *store, Variable *from, Variable *index) {} @@ -2326,10 +2388,19 @@ void _p2_handle_method_scope(Module *root, CompData *out, Scope *fs) { } +void _p2_func_scope_init(Module *root, CompData *out, Scope *fs) { + +} + +void _p2_func_scope_end(CompData *out, Scope *fs) { + +} + void p2_compile_function(Module *root, CompData *out, Vector *tokens, size_t *pos) { int end = tnsl_find_closing(tokens, *pos); Token *start = vect_get(tokens, *pos); + // Pre-checks for end of function and function name so scope can be initialized if(end < 0) { printf("ERROR: Could not find closing for function \"%s\" (%d:%d)\n\n", start->data, start->line, start->col); p2_error = true; @@ -2341,6 +2412,9 @@ void p2_compile_function(Module *root, CompData *out, Vector *tokens, size_t *po t = vect_get(tokens, ++(*pos)); if(tok_str_eq(t, "\n")) break; + else if (t->type == TT_DELIMIT && !tok_str_eq(t, ";/") && !tok_str_eq(t, ";;")) { + *pos = tnsl_find_closing(tokens, *pos); + } } if(t == NULL || t->type != TT_DEFWORD) { @@ -2354,8 +2428,12 @@ void p2_compile_function(Module *root, CompData *out, Vector *tokens, size_t *po Function *f = mod_find_func(root, &f_art); art_end(&f_art); + // Scope init Scope fs = scope_init(t->data, root); - _p2_handle_method_scope(); + if(root->name[0] == '@') { + _p2_handle_method_scope(root, out, &fs); + } + _p2_func_scope_init(root, out, &fs); while(*pos < (size_t)end) { t = vect_get(tokens, *pos); @@ -2383,7 +2461,32 @@ void p2_compile_function(Module *root, CompData *out, Vector *tokens, size_t *po } } else if (t->type == TT_KEYWORD) { - + if(tok_str_eq(t, "return")) { + t = vect_get(tokens, *pos + 1); + if (f->outputs.count > 0) { + if (*pos + 1 < end && !tok_str_eq(t, "\n")) + eval(&fs, out, tokens, pos, true); + else { + t = vect_get(tokens, *pos); + printf("ERROR: Attempt to return from a function without a value, but the function requires one (%d:%d)", t->line, t->col); + p2_error = true; + } + } + _p2_func_scope_end(out, &fs); + *pos = end; + return; + } else if (tok_str_eq(t, "asm")) { + t = vect_get(tokens, ++(*pos)); + if(t->type != TT_LITERAL || t->data[0] != '"') { + printf("ERROR: Expected string literal after asm keyword (%d:%d)\n", t->line, t->col); + p2_error = true; + } else { + char *m = tnsl_unquote_str(t->data); + vect_push_string(&out->text, m); + vect_push_string(&out->text, "\n"); + free(m); + } + } } else if (tnsl_is_def(tokens, *pos)) { p2_compile_def(&fs, out, tokens, pos); } else { @@ -2612,6 +2715,7 @@ void p2_file_loop( char *push = tnsl_unquote_str(t->data); if (push != NULL) { vect_push_string(&out->header, push); + vect_push_string(&out->header, "\n"); free(push); } } -- cgit v1.2.3