This commit is contained in:
attilavs2 2025-03-29 22:43:18 +01:00
parent 6997aee232
commit 2236d1c85f
9 changed files with 57 additions and 38 deletions

View file

@ -1,3 +0,0 @@
build/interpreter.o: file format elf64-x86-64

6
examples/scopes.flsp Normal file
View file

@ -0,0 +1,6 @@
(fn (a)
(fn (b)
(write 1)
)
(write 2)
)

BIN
perf.data

Binary file not shown.

View file

@ -23,7 +23,7 @@
#pragma once
#define SYMBOL_MAP_S (1024*16)
#define SYMBOL_MAP_S (1024*4)
enum StatementTypes {
ST_None = 0, // Not processed yet

View file

@ -28,7 +28,7 @@
// Debug logs
// 0 : None
// 1 : All misc debug logs
#define LOG 0
#define LOG 1
// TODO : Automate this
#define INTERPR

View file

@ -31,7 +31,10 @@ int heap_hashmap(HashMap *map, i32 size){
map->curr_len = size;
map->buffer = malloc(sizeof(MapItem)*size);
map->bit_free = malloc(sizeof(u32)*size/32);
#if LOG
printf("Hashmap alloc : %ldKiB\n",
(sizeof(MapItem)*size + sizeof(u32)*size/32)/1024);
#endif
if(!map->buffer || !map->bit_free)
return 1;

View file

@ -41,6 +41,10 @@ Tag atag = {0,0,T_any};
int make_stack(){
stack = malloc(sizeof(ASTStack)+sizeof(Statement)*2048);
#if LOG
printf("AST alloc : %ld KiB\n",
(sizeof(ASTStack)+sizeof(Statement)*2048)/1024);
#endif
if(!stack)
return 1;
if(heap_hashmap(&stack->symbol_map, SYMBOL_MAP_S))
@ -49,6 +53,8 @@ int make_stack(){
stack->fn_n = 0;
stack->stack_size = 2048;
symbol_map_add_bi(&stack->symbol_map);
stack->scope_id = 1;
stack->curr_scope = 1;
return 0;
}
@ -118,6 +124,19 @@ Statement *make_strconst(char *str){
return stat;
}
// 3 digit "Base64" scope id + { (invalid identifier char) + name
void mangle_name(char *dst, i32 scope, char *name){
// Scope 0 is global so we just return the name
if(!scope)
return;
for(int i = 0; i < 3; i++){
dst[i] = (scope & 63) + ' ';
scope >>= 6;
}
dst[3] = '{';
strncpy(&dst[4], name, 32);
}
Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){
i32 len = strcspn(name, " \t\n)");
#if LOG
@ -232,50 +251,44 @@ Statement *make_operation(i32 type, Tag vtype, char *name, i32 nparam, ...){
}
void push_scope(){
printf("push_scope %d\n", stack->scope_id);
if(stack->curr_scope >= 256)
yyerror("Scopes are too deep");
stack->scope_stack[stack->curr_scope++] = stack->scope_id++;
}
void pop_scope(){
if(stack->curr_scope <= 0)
yyerror("Weird scope error");
stack->curr_scope--;
printf("pop_scope %d\n", stack->scope_stack[stack->curr_scope]);
if(stack->curr_scope < 0)
yyerror("Weird scope error");
}
char _mangle_tmp[36];
// 3 digit "Base64" scope id + { (invalid identifier char) + name
char *mangle_name(i32 scope, char *name){
// Scope 0 is global so we just return the name
if(!scope)
return name;
for(int i = 0; i < 3; i++){
_mangle_tmp[i] = (scope & 63) + ' ';
scope >>= 6;
}
_mangle_tmp[3] = '{';
strncpy(&_mangle_tmp[4], name, 32);
return _mangle_tmp;
}
FnArgs _fnargs;
FnArgs *make_fnargs(char *name, Tag type){
_fnargs.n_args = 1;
_fnargs.names[0] = name;
_fnargs.types[0] = type;
return &_fnargs;
printf("fnargs : %s\n", name);
FnArgs *args = malloc(sizeof(FnArgs));
if(!args)
yyerror("alloc error");
args->n_args = 1;
args->names[0] = name;
args->types[0] = type;
return args;
}
FnArgs *add_fnargs(char *name, Tag type){
_fnargs.names[_fnargs.n_args] = name;
_fnargs.types[_fnargs.n_args++] = type;
return &_fnargs;
FnArgs *add_fnargs(FnArgs *args, char *name, Tag type){
printf("fnargs : %s\n", name);
if(args->n_args >= 9)
yyerror("Too many args to func");
args->names[args->n_args] = name;
args->types[args->n_args++] = type;
return args;
}
Statement *make_function(FnArgs *fn_args, Statement *statements){
printf("make_function : %s\n", fn_args->names[0]);
Statement *stat = get_statement();
free(fn_args);
return stat;
}
// TODO

View file

@ -57,7 +57,7 @@ void push_scope();
void pop_scope();
FnArgs *make_fnargs(char *name, Tag type);
FnArgs *add_fnargs(char *name, Tag type);
FnArgs *add_fnargs(FnArgs *args, char *name, Tag type);
Statement *make_function(FnArgs *fn_args, Statement *statements);

View file

@ -40,7 +40,7 @@
FnArgs *args;
}
%nonassoc <str> G_IDENT
%token <str> G_IDENT
%token <str> TYPE_U
%token VEC
%token VAR
@ -84,9 +84,9 @@ fn_args:
| G_IDENT type
{$$ = make_fnargs($1, $2);}
| fn_args G_IDENT
{$$ = add_fnargs($2, atag);}
{$$ = add_fnargs($1, $2, atag);}
| fn_args G_IDENT type
{$$ = add_fnargs($2, $3);}
{$$ = add_fnargs($1, $2, $3);}
;
expr: