Compare commits
5 commits
adedde0253
...
2236d1c85f
Author | SHA1 | Date | |
---|---|---|---|
|
2236d1c85f | ||
|
6997aee232 | ||
|
cb44634016 | ||
|
e05a3e9ff5 | ||
|
767f03e765 |
9 changed files with 77 additions and 38 deletions
6
examples/scopes.flsp
Normal file
6
examples/scopes.flsp
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
(fn (a)
|
||||||
|
(fn (b)
|
||||||
|
(write 1)
|
||||||
|
)
|
||||||
|
(write 2)
|
||||||
|
)
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define SYMBOL_MAP_S (1024*16)
|
#define SYMBOL_MAP_S (1024*4)
|
||||||
|
|
||||||
enum StatementTypes {
|
enum StatementTypes {
|
||||||
ST_None = 0, // Not processed yet
|
ST_None = 0, // Not processed yet
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
// 0 : None (still features runtime debug)
|
// 0 : None (still features runtime debug)
|
||||||
// 1 : Some checks
|
// 1 : Some checks
|
||||||
// 2 : All checks
|
// 2 : All checks
|
||||||
#define DEBUG 2
|
#define DEBUG 0
|
||||||
|
|
||||||
// Debug logs
|
// Debug logs
|
||||||
// 0 : None
|
// 0 : None
|
||||||
|
|
|
@ -440,13 +440,27 @@ Value add(Value a, Value b){
|
||||||
break;
|
break;
|
||||||
case T_str:
|
case T_str:
|
||||||
if(a.vstr.len + b.vstr.len > 4){
|
if(a.vstr.len + b.vstr.len > 4){
|
||||||
runtime_err("not implem");
|
u32 tmp = flisp_alloc(a.vstr.len + b.vstr.len);
|
||||||
|
if(!tmp){
|
||||||
|
a.tag = ntag;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
a.vstr.pos = tmp;
|
||||||
|
char *nstr = get_addr(tmp);
|
||||||
|
if(a.vstr.len > 4)
|
||||||
|
memcpy(nstr, get_addr(a.vstr.pos), a.vstr.len);
|
||||||
|
else
|
||||||
|
memcpy(nstr, a.vstr.str, a.vstr.len);
|
||||||
|
if(b.vstr.len > 4)
|
||||||
|
memcpy(nstr+a.vstr.len, get_addr(b.vstr.pos), b.vstr.len);
|
||||||
|
else
|
||||||
|
memcpy(nstr+a.vstr.len, b.vstr.str, b.vstr.len);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for(int i = 0; i < b.vstr.len; i++)
|
for(int i = 0; i < b.vstr.len; i++)
|
||||||
a.vstr.str[a.vstr.len + i] = b.vstr.str[i];
|
a.vstr.str[a.vstr.len + i] = b.vstr.str[i];
|
||||||
a.vstr.len += b.vstr.len;
|
|
||||||
}
|
}
|
||||||
|
a.vstr.len += b.vstr.len;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
runtime_err("Add : Invalid types");
|
runtime_err("Add : Invalid types");
|
||||||
|
|
|
@ -49,5 +49,6 @@ Value cast(Tag tag, Value b);
|
||||||
// Makes a copy of the string
|
// Makes a copy of the string
|
||||||
// If len < 0, will copy up to a '\0'
|
// If len < 0, will copy up to a '\0'
|
||||||
Value str_to_val(char *str, i32 len);
|
Value str_to_val(char *str, i32 len);
|
||||||
|
void clean_strval(Value str);
|
||||||
|
|
||||||
void symbol_map_add_bi(HashMap *map);
|
void symbol_map_add_bi(HashMap *map);
|
||||||
|
|
|
@ -31,7 +31,10 @@ int heap_hashmap(HashMap *map, i32 size){
|
||||||
map->curr_len = size;
|
map->curr_len = size;
|
||||||
map->buffer = malloc(sizeof(MapItem)*size);
|
map->buffer = malloc(sizeof(MapItem)*size);
|
||||||
map->bit_free = malloc(sizeof(u32)*size/32);
|
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)
|
if(!map->buffer || !map->bit_free)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,10 @@ Tag atag = {0,0,T_any};
|
||||||
|
|
||||||
int make_stack(){
|
int make_stack(){
|
||||||
stack = malloc(sizeof(ASTStack)+sizeof(Statement)*2048);
|
stack = malloc(sizeof(ASTStack)+sizeof(Statement)*2048);
|
||||||
|
#if LOG
|
||||||
|
printf("AST alloc : %ld KiB\n",
|
||||||
|
(sizeof(ASTStack)+sizeof(Statement)*2048)/1024);
|
||||||
|
#endif
|
||||||
if(!stack)
|
if(!stack)
|
||||||
return 1;
|
return 1;
|
||||||
if(heap_hashmap(&stack->symbol_map, SYMBOL_MAP_S))
|
if(heap_hashmap(&stack->symbol_map, SYMBOL_MAP_S))
|
||||||
|
@ -49,12 +53,14 @@ int make_stack(){
|
||||||
stack->fn_n = 0;
|
stack->fn_n = 0;
|
||||||
stack->stack_size = 2048;
|
stack->stack_size = 2048;
|
||||||
symbol_map_add_bi(&stack->symbol_map);
|
symbol_map_add_bi(&stack->symbol_map);
|
||||||
|
stack->scope_id = 1;
|
||||||
|
stack->curr_scope = 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_stack(){
|
void free_stack(){
|
||||||
for(int i = 0; i < stack->curr_statement){
|
for(int i = 0; i < stack->curr_statement; i++){
|
||||||
Statement *stat = &stack->statements[i];
|
Statement *stat = &stack->statements[i];
|
||||||
if(stat->child_n)
|
if(stat->child_n)
|
||||||
free(stat->children);
|
free(stat->children);
|
||||||
|
@ -118,6 +124,19 @@ Statement *make_strconst(char *str){
|
||||||
return stat;
|
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){
|
Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){
|
||||||
i32 len = strcspn(name, " \t\n)");
|
i32 len = strcspn(name, " \t\n)");
|
||||||
#if LOG
|
#if LOG
|
||||||
|
@ -232,50 +251,44 @@ Statement *make_operation(i32 type, Tag vtype, char *name, i32 nparam, ...){
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_scope(){
|
void push_scope(){
|
||||||
|
printf("push_scope %d\n", stack->scope_id);
|
||||||
if(stack->curr_scope >= 256)
|
if(stack->curr_scope >= 256)
|
||||||
yyerror("Scopes are too deep");
|
yyerror("Scopes are too deep");
|
||||||
stack->scope_stack[stack->curr_scope++] = stack->scope_id++;
|
stack->scope_stack[stack->curr_scope++] = stack->scope_id++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pop_scope(){
|
void pop_scope(){
|
||||||
if(stack->curr_scope <= 0)
|
|
||||||
yyerror("Weird scope error");
|
|
||||||
stack->curr_scope--;
|
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 *make_fnargs(char *name, Tag type){
|
||||||
_fnargs.n_args = 1;
|
printf("fnargs : %s\n", name);
|
||||||
_fnargs.names[0] = name;
|
FnArgs *args = malloc(sizeof(FnArgs));
|
||||||
_fnargs.types[0] = type;
|
if(!args)
|
||||||
return &_fnargs;
|
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 *add_fnargs(FnArgs *args, char *name, Tag type){
|
||||||
_fnargs.names[_fnargs.n_args] = name;
|
printf("fnargs : %s\n", name);
|
||||||
_fnargs.types[_fnargs.n_args++] = type;
|
if(args->n_args >= 9)
|
||||||
return &_fnargs;
|
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){
|
Statement *make_function(FnArgs *fn_args, Statement *statements){
|
||||||
|
printf("make_function : %s\n", fn_args->names[0]);
|
||||||
Statement *stat = get_statement();
|
Statement *stat = get_statement();
|
||||||
|
free(fn_args);
|
||||||
|
return stat;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
|
|
@ -57,6 +57,8 @@ void push_scope();
|
||||||
void pop_scope();
|
void pop_scope();
|
||||||
|
|
||||||
FnArgs *make_fnargs(char *name, Tag type);
|
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);
|
||||||
|
|
||||||
void set_entry_point(Statement *statement);
|
void set_entry_point(Statement *statement);
|
||||||
|
|
|
@ -40,7 +40,7 @@
|
||||||
FnArgs *args;
|
FnArgs *args;
|
||||||
}
|
}
|
||||||
|
|
||||||
%nonassoc <str> G_IDENT
|
%token <str> G_IDENT
|
||||||
%token <str> TYPE_U
|
%token <str> TYPE_U
|
||||||
%token VEC
|
%token VEC
|
||||||
%token VAR
|
%token VAR
|
||||||
|
@ -84,9 +84,9 @@ fn_args:
|
||||||
| G_IDENT type
|
| G_IDENT type
|
||||||
{$$ = make_fnargs($1, $2);}
|
{$$ = make_fnargs($1, $2);}
|
||||||
| fn_args G_IDENT
|
| fn_args G_IDENT
|
||||||
{$$ = add_fnargs($2, atag);}
|
{$$ = add_fnargs($1, $2, atag);}
|
||||||
| fn_args G_IDENT type
|
| fn_args G_IDENT type
|
||||||
{$$ = add_fnargs($2, $3);}
|
{$$ = add_fnargs($1, $2, $3);}
|
||||||
;
|
;
|
||||||
|
|
||||||
expr:
|
expr:
|
||||||
|
|
Loading…
Add table
Reference in a new issue