WIP
This commit is contained in:
parent
d8d8efe117
commit
234a066b52
6 changed files with 63 additions and 22 deletions
|
@ -155,7 +155,7 @@ typedef struct {
|
||||||
#ifndef win
|
#ifndef win
|
||||||
_Static_assert(sizeof(Value) == 8, "Value isn't the right size");
|
_Static_assert(sizeof(Value) == 8, "Value isn't the right size");
|
||||||
#endif
|
#endif
|
||||||
_Static_assert(sizeof(Tag) <= sizeof(i32), "Tag is too large for tomfoolery");
|
_Static_assert(sizeof(Tag) <= 2, "Tag is too large for tomfoolery");
|
||||||
|
|
||||||
static inline FnSig *get_fnsig(Value *x){
|
static inline FnSig *get_fnsig(Value *x){
|
||||||
u64 rval = *((u64*)x);
|
u64 rval = *((u64*)x);
|
||||||
|
|
|
@ -69,13 +69,13 @@ enum BuiltinStatements {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct Statement {
|
struct PACKED Statement {
|
||||||
|
|
||||||
i32 type;
|
u8 type;
|
||||||
i16 is_const; // Statement is constant, != is a constant - TODO : implem
|
u8 is_const; // Statement is constant, != is a constant - TODO : implem
|
||||||
i16 scope;
|
i16 scope;
|
||||||
void **children;
|
|
||||||
i32 child_n;
|
i32 child_n;
|
||||||
|
void **children;
|
||||||
void *parent;
|
void *parent;
|
||||||
union {
|
union {
|
||||||
Value cons;
|
Value cons;
|
||||||
|
@ -91,6 +91,14 @@ struct Statement {
|
||||||
|
|
||||||
typedef struct Statement Statement;
|
typedef struct Statement Statement;
|
||||||
|
|
||||||
|
// Function signature for the tree walk interpreter
|
||||||
|
typedef struct {
|
||||||
|
|
||||||
|
i32 param_id[8]; // Ids to fill for parameters
|
||||||
|
Statement *stat; // Statement to jump to
|
||||||
|
|
||||||
|
} InterprSig;
|
||||||
|
|
||||||
// In practice, this is allocated in parse.c
|
// In practice, this is allocated in parse.c
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
|
@ -106,6 +114,9 @@ typedef struct {
|
||||||
|
|
||||||
i32 fn_n;
|
i32 fn_n;
|
||||||
FnSig *funcs;
|
FnSig *funcs;
|
||||||
|
#ifdef INTERPR
|
||||||
|
InterprSig *interpr_sigs;
|
||||||
|
#endif
|
||||||
|
|
||||||
i32 stack_size;
|
i32 stack_size;
|
||||||
i32 curr_statement;
|
i32 curr_statement;
|
||||||
|
|
|
@ -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
|
||||||
|
|
15
src/hash.h
15
src/hash.h
|
@ -24,20 +24,22 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define HASH_NULL 0xFFFFFF
|
#define HASH_NULL 0x7FFFFF
|
||||||
|
|
||||||
#define RELOCATE_TRY_MAX 32
|
#define RELOCATE_TRY_MAX 32
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
i32 hash;
|
union {
|
||||||
char str[36];
|
|
||||||
i32 id;
|
i32 id;
|
||||||
void *fnsig;
|
void *fnsig;
|
||||||
i16 type;
|
};
|
||||||
i16 is_const;
|
uint hash : 23;
|
||||||
|
uint is_const : 1;
|
||||||
|
u16 type;
|
||||||
|
char str[36];
|
||||||
|
|
||||||
} MapItem;
|
} PACKED NOPOS MapItem;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
|
@ -69,3 +71,4 @@ MapItem *hashmap_get(HashMap *map, char *str);
|
||||||
void hashmap_remove(HashMap *map, char *str);
|
void hashmap_remove(HashMap *map, char *str);
|
||||||
|
|
||||||
void hashmap_remove_id(HashMap *map, i32 id);
|
void hashmap_remove_id(HashMap *map, i32 id);
|
||||||
|
|
||||||
|
|
|
@ -86,6 +86,10 @@ Statement *get_statement(){
|
||||||
return &stack->statements[stack->curr_statement++];
|
return &stack->statements[stack->curr_statement++];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i32 curr_scope(){
|
||||||
|
return stack->scope_stack[stack->curr_scope];
|
||||||
|
}
|
||||||
|
|
||||||
char *bi_type_names[6] = {
|
char *bi_type_names[6] = {
|
||||||
"",
|
"",
|
||||||
"int",
|
"int",
|
||||||
|
@ -144,18 +148,22 @@ void mangle_name(char *dst, i32 scope, char *name){
|
||||||
}
|
}
|
||||||
|
|
||||||
Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){
|
Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){
|
||||||
#if LOG
|
char tmpbuf[36];
|
||||||
printf("declare : %d\n", vtype.type);
|
|
||||||
#endif
|
|
||||||
clean_name(name);
|
clean_name(name);
|
||||||
Statement *stat = get_statement();
|
Statement *stat = get_statement();
|
||||||
stat->type = type;
|
stat->type = type;
|
||||||
|
if(type == BI_let){
|
||||||
|
mangle_name(tmpbuf, curr_scope(), name);
|
||||||
|
name = tmpbuf;
|
||||||
|
}
|
||||||
|
|
||||||
if(hashmap_get(&stack->symbol_map, name))
|
if(hashmap_get(&stack->symbol_map, name))
|
||||||
yyerror("Redeclaring existing identifier");
|
yyerror("Redeclaring existing identifier");
|
||||||
MapItem *ret = hashmap_insert(&stack->symbol_map, name);
|
MapItem *ret = hashmap_insert(&stack->symbol_map, name);
|
||||||
ret->type = *((i16*)&vtype);
|
ret->type = *((i16*)&vtype);
|
||||||
#if LOG
|
#if LOG
|
||||||
printf("id : %d\n", ret->id);
|
printf("declare : id : %d\n", ret->id);
|
||||||
#endif
|
#endif
|
||||||
stat->var_id = ret->id;
|
stat->var_id = ret->id;
|
||||||
stat->var_type = vtype;
|
stat->var_type = vtype;
|
||||||
|
@ -176,8 +184,13 @@ Statement *variable_get(char *name){
|
||||||
clean_name(name);
|
clean_name(name);
|
||||||
Statement *stat = get_statement();
|
Statement *stat = get_statement();
|
||||||
MapItem *ret = hashmap_get(&stack->symbol_map, name);
|
MapItem *ret = hashmap_get(&stack->symbol_map, name);
|
||||||
|
if(!ret){
|
||||||
|
char tmpbuf[36];
|
||||||
|
mangle_name(tmpbuf, curr_scope(), name);
|
||||||
|
ret = hashmap_get(&stack->symbol_map, tmpbuf);
|
||||||
if(!ret)
|
if(!ret)
|
||||||
yyerror("Undefined identifier");
|
yyerror("Undefined identifier");
|
||||||
|
}
|
||||||
stat->type = ST_Var;
|
stat->type = ST_Var;
|
||||||
stat->var_id = ret->id;
|
stat->var_id = ret->id;
|
||||||
stat->var_type = *((Tag*)&ret->type);
|
stat->var_type = *((Tag*)&ret->type);
|
||||||
|
@ -274,7 +287,9 @@ Statement *make_operation(i32 type, Tag vtype, char *name, i32 nparam, ...){
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_scope(){
|
void push_scope(){
|
||||||
|
#if LOG
|
||||||
printf("push_scope %d\n", stack->scope_id);
|
printf("push_scope %d\n", stack->scope_id);
|
||||||
|
#endif
|
||||||
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++;
|
||||||
|
@ -282,27 +297,36 @@ void push_scope(){
|
||||||
|
|
||||||
void pop_scope(){
|
void pop_scope(){
|
||||||
stack->curr_scope--;
|
stack->curr_scope--;
|
||||||
|
#if LOG
|
||||||
printf("pop_scope %d\n", stack->scope_stack[stack->curr_scope]);
|
printf("pop_scope %d\n", stack->scope_stack[stack->curr_scope]);
|
||||||
|
#endif
|
||||||
if(stack->curr_scope < 0)
|
if(stack->curr_scope < 0)
|
||||||
yyerror("Weird scope error");
|
yyerror("Weird scope error");
|
||||||
}
|
}
|
||||||
|
|
||||||
FnArgs *make_fnargs(char *name, Tag type){
|
FnArgs *make_fnargs(char *name, Tag type){
|
||||||
|
#if LOG
|
||||||
printf("fnargs : %s\n", name);
|
printf("fnargs : %s\n", name);
|
||||||
|
#endif
|
||||||
FnArgs *args = malloc(sizeof(FnArgs));
|
FnArgs *args = malloc(sizeof(FnArgs));
|
||||||
if(!args)
|
if(!args)
|
||||||
yyerror("alloc error");
|
yyerror("alloc error");
|
||||||
args->n_args = 1;
|
args->n_args = 1;
|
||||||
args->names[0] = name;
|
// TODO
|
||||||
|
args->param_decl[0] = NULL;
|
||||||
args->types[0] = type;
|
args->types[0] = type;
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
FnArgs *add_fnargs(FnArgs *args, char *name, Tag type){
|
FnArgs *add_fnargs(FnArgs *args, char *name, Tag type){
|
||||||
|
#if LOG
|
||||||
printf("fnargs : %s\n", name);
|
printf("fnargs : %s\n", name);
|
||||||
|
#endif
|
||||||
if(args->n_args >= 9)
|
if(args->n_args >= 9)
|
||||||
yyerror("Too many args to func");
|
yyerror("Too many args to func");
|
||||||
args->names[args->n_args] = name;
|
|
||||||
|
Statement *decl = declare(BI_let, type, name, NULL);
|
||||||
|
args->param_decl[args->n_args] = decl;
|
||||||
args->types[args->n_args++] = type;
|
args->types[args->n_args++] = type;
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
@ -311,6 +335,9 @@ Statement *make_function(FnArgs *fn_args, Statement *statements){
|
||||||
printf("make_function : %s\n", fn_args->names[0]);
|
printf("make_function : %s\n", fn_args->names[0]);
|
||||||
Statement *stat = get_statement();
|
Statement *stat = get_statement();
|
||||||
stat->type = BI_fn;
|
stat->type = BI_fn;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
free(fn_args);
|
free(fn_args);
|
||||||
return stat;
|
return stat;
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
// 0 is function name/output type
|
// 0 is function name/output type
|
||||||
char *names[9];
|
Statement *param_decl[9];
|
||||||
Tag types[9];
|
Tag types[9];
|
||||||
i32 n_args;
|
i32 n_args;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue