From 6ceb38c53db2a87e77b89d19671db6279db432fd Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Fri, 14 Mar 2025 22:54:43 +0100 Subject: [PATCH] Progress --- TODO.md | 5 --- examples/fib.c | 14 ++++++ examples/fib.flsp | 15 +++++++ examples/fib.py | 13 ++++++ examples/fixed.flsp | 5 +++ src/exec_common.c | 43 ++++++++++++++++--- src/hash.c | 14 +++--- src/parse_utils.c | 5 +++ src/parser.h | 2 + src/parser.y | 22 +++++++--- src/tests.c | 102 ++++++++++++++++++++++++++++++++++++++++++++ src/tests.h | 5 +++ 12 files changed, 223 insertions(+), 22 deletions(-) create mode 100644 examples/fib.c create mode 100644 examples/fib.flsp create mode 100644 examples/fib.py create mode 100644 examples/fixed.flsp create mode 100644 src/tests.c create mode 100644 src/tests.h diff --git a/TODO.md b/TODO.md index 0df24a6..be18c9c 100644 --- a/TODO.md +++ b/TODO.md @@ -1,10 +1,5 @@ -tmp: - - Finish varuse : ~ - -- Load files - Benchmarks - Tests -- Assign on declaration - Rework assign/call to work better - Finish internal allocator - Strings diff --git a/examples/fib.c b/examples/fib.c new file mode 100644 index 0000000..9b661a2 --- /dev/null +++ b/examples/fib.c @@ -0,0 +1,14 @@ +int main(){ + for(int i = 0; i < 10000; i++){ + int a = 0; + int b = 1; + int c; + int n = 0; + while(n < 40){ + c = b; + b += a; + a = c; + n++; + } + } +} diff --git a/examples/fib.flsp b/examples/fib.flsp new file mode 100644 index 0000000..277dfa6 --- /dev/null +++ b/examples/fib.flsp @@ -0,0 +1,15 @@ +(var:int i 0) +(while (< i 10000) + (var:int a 0) + (var:int b 1) + (var:int c) + (var:int n 0) + + (while (< n 40) + (c b) + (b (+ a b)) + (a c) + (n (+ n 1)) + ) + (i (+ i 1)) +) diff --git a/examples/fib.py b/examples/fib.py new file mode 100644 index 0000000..f3bec7f --- /dev/null +++ b/examples/fib.py @@ -0,0 +1,13 @@ +i = 0 +while i < 10000: + a = 0 + b = 1 + c = 0 + n = 0 + + while n < 40: + c = b + b += a + a = c + n += 1 + i+=1 diff --git a/examples/fixed.flsp b/examples/fixed.flsp new file mode 100644 index 0000000..fa10acc --- /dev/null +++ b/examples/fixed.flsp @@ -0,0 +1,5 @@ +(var:fix a (:fix 0.1)) +(var:fix b (:fix 0.2)) + +(write (+ a b)) +(newline) diff --git a/src/exec_common.c b/src/exec_common.c index 985d44c..783f1dc 100644 --- a/src/exec_common.c +++ b/src/exec_common.c @@ -87,7 +87,8 @@ u32 flisp_alloc(size_t size){ for(u32 i = block->pos; i < block->pos+size; i++){ usage[i/32/8] |= 1<<(i%(32*8)); } - memset(get_addr(block->pos),0xB0B69420,size); + // Unvirtualize the memory + memset(get_addr(block->pos),0xB00B5069,size); return block->pos; } pos++; @@ -95,8 +96,22 @@ u32 flisp_alloc(size_t size){ return 0; } -void flisp_free(u32 pos){ +int block_cmpfunc(const void *_a, const void *_b){ + const AllocBlock *a = _a; + const AllocBlock *b = _b; + return b->pos - a->pos; +} +void flisp_free(u32 pos){ + AllocBlock tmp = {.pos = pos}; + AllocBlock *ret = bsearch(&tmp, blocks, block_count, sizeof(AllocBlock), + block_cmpfunc); + if(!ret) + return; + i32 bpos = (size_t)(ret-blocks)/sizeof(AllocBlock); + memset(get_addr(ret->pos), 0, ret->size); + memmove(ret, ret+1, sizeof(AllocBlock) * (block_count-bpos-1)); + block_count--; } void *get_addr(u32 pos){ @@ -114,7 +129,7 @@ void print_ast1(Statement *base, i32 indent){ printf("(none\n"); break; case ST_Call: - printf("(call<%lx>\n", base->func); + printf("(call<%lx>\n", (u64)base->func); break; case ST_Const: write(base->cons); @@ -148,8 +163,9 @@ void print_ast1(Statement *base, i32 indent){ break; case BI_cast: printf("(cast<%d>\n", base->var_type.type); + break; default: - printf("(other\n"); + printf("(other<%d>\n", base->type); break; } for(int i = 0; i < base->child_n; i++) @@ -372,7 +388,24 @@ Value mod(Value a, Value b){ } Value sml(Value a, Value b){ - + Value returnv; + returnv.tag = (Tag){.is_array=0,.type=T_int}; + if(a.tag.is_array || b.tag.is_array) + runtime_err("Sml : Invalid types"); + if(a.tag.type != b.tag.type) + runtime_err("Sml : Types do not match"); + switch(a.tag.type){ + case T_int: + case T_fix: + returnv.v4B.value = a.v4B.value < b.v4B.value; + break; + case T_float: + returnv.v4B.value = a.v4B.vfl < b.v4B.vfl; + break; + default: + runtime_err("Sml : Invalid types"); + } + return returnv; } Value sml_eq(Value a, Value b){ diff --git a/src/hash.c b/src/hash.c index 50d2f7a..c30984f 100755 --- a/src/hash.c +++ b/src/hash.c @@ -143,10 +143,12 @@ int cmp_mapitem_id(const void *_a, const void *_b){ } void hashmap_remove_id(HashMap *map, i32 id){ - MapItem tmp = {.id = id}; - MapItem *ret = bsearch(&tmp, map->buffer, map->curr_len, sizeof(MapItem), - cmp_mapitem_id); - if(!ret) - return; - memset(ret, 0, sizeof(MapItem)); + i32 pos = 0; + int found = 0; + // TODO : Replace with bsearch again + for(int i = 0; i < map->curr_len && !found; i++){ + found = map->buffer[pos++].id == id; + } + if(found) + memset(&map->buffer[--pos], 0, sizeof(MapItem)); } diff --git a/src/parse_utils.c b/src/parse_utils.c index bf614e8..65aba93 100644 --- a/src/parse_utils.c +++ b/src/parse_utils.c @@ -32,6 +32,11 @@ int make_stack(){ return 0; } +void free_stack(){ + free_hashmap(&stack->symbol_map); + free(stack); +} + char *bi_type_names[6] = { "", "int", diff --git a/src/parser.h b/src/parser.h index 2529b6c..5ceedb1 100644 --- a/src/parser.h +++ b/src/parser.h @@ -8,6 +8,8 @@ void yyerror(char *s); int make_stack(); +void free_stack(); + Tag get_type(char *str); Statement *make_iconst(i32 val); diff --git a/src/parser.y b/src/parser.y index cc916be..454059a 100644 --- a/src/parser.y +++ b/src/parser.y @@ -3,6 +3,7 @@ #include #include #include "types.h" + #include "tests.h" #include "code_defs.h" #include "parser.h" #include "interpreter.h" @@ -126,21 +127,30 @@ extern FILE *yyin; int main(int argc, char *argv[]){ alloc_init(); - if(argc < 2){ - printf("fLisp interactive env - v0.1\nFcalva 2025\n"); - printf("Under the terms of the GPL v3.0 license\n"); - yyin = stdin; + i32 fpos = 1; + + if(argc > 1){ + if(!strcmp(argv[1], "--tests")){ + fpos++; + do_tests(); + } } - else { - yyin = fopen(argv[1], "r"); + if(argc > fpos) { + yyin = fopen(argv[fpos], "r"); if(!yyin){ fprintf(stderr,"Failed to open file \"%s\"", argv[1]); exit(1); } } + else { + printf("fLisp interactive env - v0.1\nFcalva 2025\n"); + printf("Under the terms of the GPL v3.0 license\n"); + yyin = stdin; + } make_stack(); yyparse(); + free_stack(); fclose(yyin); diff --git a/src/tests.c b/src/tests.c new file mode 100644 index 0000000..c74ded9 --- /dev/null +++ b/src/tests.c @@ -0,0 +1,102 @@ +#include +#include +#include + +#include "types.h" +#include "parser.h" +#include "code_defs.h" +#include "byte_defs.h" + +typedef int (test_fun_t)(void); + +HashMap t_hmap; + +int t_heap_hashmap(){ + return heap_hashmap(&t_hmap,512); +} + +int t_hashmap_insert_get(){ + int err = 0; + MapItem *ret = hashmap_insert(&t_hmap, "test"); + if(!ret) + return 1; + err += ret->id != 0; + ret->type = 69; + MapItem *ret2 = hashmap_get(&t_hmap, "test"); + if(!ret2) + return 1; + err += ret2->type != 69 || ret2->id != 0; + + return err; +} + +int t_hashmap_remove(){ + int err = 0; + hashmap_remove(&t_hmap, "test"); + MapItem *ret = hashmap_get(&t_hmap, "test"); + err += ret; + MapItem *ret2 = hashmap_insert(&t_hmap, "very_verbose_function_i_guess"); + hashmap_remove_id(&t_hmap, ret2->id); + err += hashmap_get(&t_hmap, "very_verbose_function_i_guess"); + + return err; +} + +int t_free_hashmap(){ + free_hashmap(&t_hmap); + return 0; +} + +int t_get_type(){ + int err = 0; + Tag ttag = get_type("int"); + err += ttag.type != T_int || ttag.is_array; + ttag = get_type("fix"); + err += ttag.type != T_fix || ttag.is_array; + return err; +} + +int t_make_stack(){ + return make_stack(); +} + +int t_free_stack(){ + free_stack(); + return 0; +} + +test_fun_t *tests[] = { + t_heap_hashmap, + t_hashmap_insert_get, + t_hashmap_remove, + t_free_hashmap, + t_get_type, + t_make_stack, + + t_free_stack +}; + +char *names[] = { + "heap_hashmap", + "hashmap_insert_get", + "hashmap_remove", + "free_hashmap", + "get_type", + "make_stack", + + "free_stack" +}; + +void do_tests(){ + i32 tlen = sizeof(tests)/sizeof(void*); + assert(tlen == sizeof(names)/sizeof(void*)); // If forgot to name a test + for(int i = 0; i < tlen; i++){ + if(tests[i]()){ + printf("Test \"%s\" : \x1B[31m[FAILED]\x1B[0m\n", names[i]); + exit(1); + } + else{ + printf("Test \"%s\" : \x1B[32m[PASSED]\x1B[0m\n", names[i]); + } + } +} diff --git a/src/tests.h b/src/tests.h new file mode 100644 index 0000000..21e852d --- /dev/null +++ b/src/tests.h @@ -0,0 +1,5 @@ +#include "config.h" + +#pragma once + +void do_tests();