Compare commits
No commits in common. "2236d1c85ff7723c57558bd9d6a5367667c52778" and "adedde025367d0b97709388bf104fad9fc2d2ebd" have entirely different histories.
2236d1c85f
...
adedde0253
9 changed files with 38 additions and 77 deletions
|
@ -1,6 +0,0 @@
|
||||||
(fn (a)
|
|
||||||
(fn (b)
|
|
||||||
(write 1)
|
|
||||||
)
|
|
||||||
(write 2)
|
|
||||||
)
|
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define SYMBOL_MAP_S (1024*4)
|
#define SYMBOL_MAP_S (1024*16)
|
||||||
|
|
||||||
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 0
|
#define DEBUG 2
|
||||||
|
|
||||||
// Debug logs
|
// Debug logs
|
||||||
// 0 : None
|
// 0 : None
|
||||||
|
|
|
@ -440,27 +440,13 @@ 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){
|
||||||
u32 tmp = flisp_alloc(a.vstr.len + b.vstr.len);
|
runtime_err("not implem");
|
||||||
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,6 +49,5 @@ 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,10 +31,7 @@ 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,10 +41,6 @@ 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))
|
||||||
|
@ -53,14 +49,12 @@ 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; i++){
|
for(int i = 0; i < stack->curr_statement){
|
||||||
Statement *stat = &stack->statements[i];
|
Statement *stat = &stack->statements[i];
|
||||||
if(stat->child_n)
|
if(stat->child_n)
|
||||||
free(stat->children);
|
free(stat->children);
|
||||||
|
@ -124,19 +118,6 @@ 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
|
||||||
|
@ -251,44 +232,50 @@ 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(){
|
||||||
stack->curr_scope--;
|
if(stack->curr_scope <= 0)
|
||||||
printf("pop_scope %d\n", stack->scope_stack[stack->curr_scope]);
|
|
||||||
if(stack->curr_scope < 0)
|
|
||||||
yyerror("Weird scope error");
|
yyerror("Weird scope error");
|
||||||
|
stack->curr_scope--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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){
|
||||||
printf("fnargs : %s\n", name);
|
_fnargs.n_args = 1;
|
||||||
FnArgs *args = malloc(sizeof(FnArgs));
|
_fnargs.names[0] = name;
|
||||||
if(!args)
|
_fnargs.types[0] = type;
|
||||||
yyerror("alloc error");
|
return &_fnargs;
|
||||||
args->n_args = 1;
|
|
||||||
args->names[0] = name;
|
|
||||||
args->types[0] = type;
|
|
||||||
return args;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FnArgs *add_fnargs(FnArgs *args, char *name, Tag type){
|
FnArgs *add_fnargs(char *name, Tag type){
|
||||||
printf("fnargs : %s\n", name);
|
_fnargs.names[_fnargs.n_args] = name;
|
||||||
if(args->n_args >= 9)
|
_fnargs.types[_fnargs.n_args++] = type;
|
||||||
yyerror("Too many args to func");
|
return &_fnargs;
|
||||||
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,8 +57,6 @@ 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(FnArgs *args, char *name, Tag type);
|
FnArgs *add_fnargs(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
%token <str> G_IDENT
|
%nonassoc <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($1, $2, atag);}
|
{$$ = add_fnargs($2, atag);}
|
||||||
| fn_args G_IDENT type
|
| fn_args G_IDENT type
|
||||||
{$$ = add_fnargs($1, $2, $3);}
|
{$$ = add_fnargs($2, $3);}
|
||||||
;
|
;
|
||||||
|
|
||||||
expr:
|
expr:
|
||||||
|
|
Loading…
Add table
Reference in a new issue