This commit is contained in:
attilavs2 2025-03-14 22:54:43 +01:00
parent f5d8b75987
commit 6ceb38c53d
12 changed files with 223 additions and 22 deletions

View file

@ -1,10 +1,5 @@
tmp:
- Finish varuse : ~
- Load files
- Benchmarks
- Tests
- Assign on declaration
- Rework assign/call to work better
- Finish internal allocator
- Strings

14
examples/fib.c Normal file
View file

@ -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++;
}
}
}

15
examples/fib.flsp Normal file
View file

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

13
examples/fib.py Normal file
View file

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

5
examples/fixed.flsp Normal file
View file

@ -0,0 +1,5 @@
(var:fix a (:fix 0.1))
(var:fix b (:fix 0.2))
(write (+ a b))
(newline)

View file

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

View file

@ -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));
}

View file

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

View file

@ -8,6 +8,8 @@ void yyerror(char *s);
int make_stack();
void free_stack();
Tag get_type(char *str);
Statement *make_iconst(i32 val);

View file

@ -3,6 +3,7 @@
#include <string.h>
#include <stdlib.h>
#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);

102
src/tests.c Normal file
View file

@ -0,0 +1,102 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#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]);
}
}
}

5
src/tests.h Normal file
View file

@ -0,0 +1,5 @@
#include "config.h"
#pragma once
void do_tests();