diff --git a/TODO.md b/TODO.md index f0227bb..07089b0 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,3 @@ -tmp: - - Finish varuse - - Load files - Benchmarks - Tests diff --git a/src/code_defs.h b/src/code_defs.h index e74e1e3..1838d8c 100755 --- a/src/code_defs.h +++ b/src/code_defs.h @@ -10,42 +10,43 @@ enum StatementTypes { ST_None = 0, // Not processed yet ST_Call = 1, // Any kind of function call ST_Const = 2, // Constant - ST_Var = 3, // Free variable + ST_Var = 3, // Variable ST_Block = 4, // Scope blocks - ST_Varuse = 5, // Variable assign/potential func call + __stpass, ST__end }; // Special expressions and ones that map directly to bytecode // Should mesh with StatementTypes enum BuiltinStatements { - BI_var = 6, - BI_let = 7, - BI_if = 8, - BI_else = 9, - BI_while = 10, - BI_fn = 11, - BI_import = 12, - BI_is = 13, - BI_cast = 14, - BI_add = 15, - BI_sub = 16, - BI_mul = 17, - BI_div = 18, - BI_mod = 19, - BI_sml = 20, - BI_sml_eq = 21, - BI_eq = 22, - BI_gt_eq = 23, - BI_gt = 24, - BI_not = 25, - BI_and = 26, - BI_or = 27, - BI_len = 28, - BI_push = 29, - BI_pop = 30, - BI_write = 31, - BI_nwline = 32, + BI_assign = 6, + BI_var = 7, + BI_let = 8, + BI_if = 9, + BI_else = 10, + BI_while = 11, + BI_fn = 12, + BI_import = 13, + BI_is = 14, + BI_cast = 15, + BI_add = 16, + BI_sub = 17, + BI_mul = 18, + BI_div = 19, + BI_mod = 20, + BI_sml = 21, + BI_sml_eq = 22, + BI_eq = 23, + BI_gt_eq = 24, + BI_gt = 25, + BI_not = 26, + BI_and = 27, + BI_or = 28, + BI_len = 29, + BI_push = 30, + BI_pop = 31, + BI_write = 32, + BI_nwline = 33, BI__end }; @@ -88,4 +89,4 @@ typedef struct { } ASTStack; -_Static_assert((int)BI_var >= ST__end); +_Static_assert((int)BI_assign >= ST__end); diff --git a/src/config.h b/src/config.h index d7cd04e..ae86007 100755 --- a/src/config.h +++ b/src/config.h @@ -4,6 +4,3 @@ // 1 : Some checks and prints // 2 : All checks #define DEBUG 2 - -// TODO : Automate this -#define INTERPR diff --git a/src/exec_common.c b/src/exec_common.c index ba87341..d208f10 100644 --- a/src/exec_common.c +++ b/src/exec_common.c @@ -5,7 +5,6 @@ #include "fixed.h" #include "types.h" -#include "config.h" #include "code_defs.h" #include "byte_defs.h" #include "exec_common.h" @@ -53,9 +52,12 @@ int evaluate(Value val){ int alloc_init(){ internal_buf = malloc(4096); + printf("55\n"); blocks = malloc(sizeof(AllocBlock)); + printf("57\n"); block_count = 0; usage = malloc(sizeof(u32)*4096/32/8); + printf("60\n"); if(!internal_buf || !blocks || !usage) return 1; memset(usage, 0, sizeof(u32)*4096/32/8); @@ -448,6 +450,5 @@ void symbol_map_add_bi(HashMap *map){ MapItem *ret = hashmap_insert(map, bi_names[i-BI_add]); ret->type = *((i16*)&fntag); ret->fnsig = &builtins[i-BI_add]; - ret->is_const = 1; } } diff --git a/src/exec_common.h b/src/exec_common.h index 602cdab..0f8a9c0 100644 --- a/src/exec_common.h +++ b/src/exec_common.h @@ -22,6 +22,4 @@ void *get_addr(u32 pos); Value is(Tag tag, Value b); -Value cast(Tag tag, Value b); - void symbol_map_add_bi(HashMap *map); diff --git a/src/hash.h b/src/hash.h index f89ec45..b26c0bf 100755 --- a/src/hash.h +++ b/src/hash.h @@ -11,12 +11,11 @@ typedef struct { - i32 hash; + i32 hash; // Hashs are internally 24bit for bytecode ops char str[32]; i32 id; void *fnsig; i32 type; - i32 is_const; } MapItem; diff --git a/src/interpreter.c b/src/interpreter.c index 33336c5..63b89fa 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -3,80 +3,16 @@ #include #include "fixed.h" -#include "hash.h" #include "types.h" -#include "config.h" #include "code_defs.h" #include "byte_defs.h" #include "exec_common.h" Value vars[SYMBOL_MAP_S]; -Value execute(Statement *stat); - -void assign(Value *dst, Value src){ - printf("Assign : %d -> %d\n", src.tag.type, dst->tag.type); - if((dst->tag.type == src.tag.type || dst->tag.type == T_any) - && dst->tag.is_array == src.tag.is_array) - *dst = src; - else{ - fprintf(stderr, "Warning : Possibly incorrect assignement\n" - "This may error in the future\n"); - *dst = src; - } -} - -Value fncall(FnSig *fn, Statement **params){ - Value returnv = {.tag = {0, T_null}}; - if(fn->is_builtin){ - switch(fn->n_param){ - case 0: - returnv = fn->bi(); - break; - case 1: - Value (*fn1)(Value) = (void*)fn->bi; - returnv = fn1(execute(params[0])); - break; - case 2: - Value (*fn2)(Value,Value) = (void*)fn->bi; - returnv = fn2(execute(params[0]),execute(params[1])); - break; - default: - runtime_err("Call to builtin with >2 params"); - break; - } - } - else { -#ifdef INTERPR - returnv = execute(fn->stmt); -#else - #warning "fncall not implemented for compiler" -#endif - } - return returnv; -} - -void init_exec(){ - -} - Value execute(Statement *stat){ Value returnv = {.tag = {0,T_null}}; switch(stat->type){ - case ST_Varuse: - if(vars[stat->var_id].tag.type == T_fn){ - Statement **params = NULL; - FnSig *fn = get_fnsig(&vars[stat->var_id]); - if(fn->n_param) - params = (Statement**)((Statement*)stat->children[0])->children; - fncall(fn, params); - } - else { - printf("stvar : %d\n", vars[stat->var_id].tag.type); - assign(&vars[stat->var_id], execute(stat->children[0])); - } - - break; case ST_None: runtime_err("ST_None to process !"); break; @@ -91,13 +27,13 @@ Value execute(Statement *stat){ returnv = execute(stat->children[i]); } break; + case BI_assign: + vars[stat->var_id] = execute(stat->children[0]); + break; case BI_var: case BI_let: - // Init the relevant value for correct type checks - printf("var : %d\n", stat->var_type.type); + // Init the relevant value for correct "is" vars[stat->var_id].tag = stat->var_type; - if(stat->child_n) - assign(&vars[stat->var_id],execute(stat->children[0])); break; case BI_if: if(evaluate(execute(stat->children[0]))) @@ -118,15 +54,31 @@ Value execute(Statement *stat){ case BI_is: returnv = is(stat->var_type, execute(stat->children[0])); break; + // TODO case BI_cast: - returnv = cast(stat->var_type, execute(stat->children[0])); break; case ST_Call: FnSig *fn = stat->func; - Statement **params = NULL; + Statement **params; if(fn->n_param) params = (Statement**)((Statement*)stat->children[0])->children; - returnv = fncall(fn, params); + if(fn->is_builtin){ + switch(fn->n_param){ + case 0: + returnv = fn->bi(); + break; + case 1: + Value (*fn1)(Value) = (void*)fn->bi; + returnv = fn1(execute(params[0])); + break; + case 2: + Value (*fn2)(Value,Value) = (void*)fn->bi; + returnv = fn2(execute(params[0]),execute(params[1])); + break; + default: + break; + } + } break; } return returnv; diff --git a/src/parse_utils.c b/src/parse_utils.c index 81fe5e2..9e4e7e3 100644 --- a/src/parse_utils.c +++ b/src/parse_utils.c @@ -17,8 +17,6 @@ void yyerror(char *s); Tag ntag = {0,T_null}; -Tag atag = {0,T_any}; - int make_stack(){ stack = malloc(sizeof(ASTStack)+sizeof(Statement)*2048); if(!stack) @@ -60,9 +58,8 @@ Statement *make_strconst(char *str){ } -Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){ +Statement *declare(i32 type, Tag vtype, char *name){ i32 len = strcspn(name, " \t\n)"); - printf("declare : %d\n", vtype.type); char *name2 = strndup(name, len); Statement *stat = &stack->statements[stack->curr_statement++]; stat->type = type; @@ -73,13 +70,7 @@ Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){ ret->type = *((i16*)&vtype); stat->var_id = ret->id; stat->var_type = vtype; - if(assign){ - stat->child_n = 1; - stat->children = malloc(sizeof(void*)); - stat->children[0] = assign; - } - else - stat->child_n = 0; + stat->child_n = 0; return stat; } @@ -115,6 +106,10 @@ Statement *add_block(Statement *block, Statement *add){ FnSig assign_sig = {.n_param = 1}; +FnSig bi_sig[] = { + +}; + Statement *make_operation(i32 type, Tag vtype, char *name, i32 nparam, ...){ Statement *stat = &stack->statements[stack->curr_statement++]; if(nparam) @@ -129,18 +124,27 @@ Statement *make_operation(i32 type, Tag vtype, char *name, i32 nparam, ...){ if(!type || type == ST_Call){ i32 len = strcspn(name, " \t\n()"); char *name2 = strndup(name, len); - printf("name : \"%s\" ; name2 : \"%s\"\n",name,name2); MapItem *ret = hashmap_get(&stack->symbol_map, name2); free(name2); if(!ret) yyerror("Undefined identifer"); - if(ret->is_const && ret->type == T_fn){ - type = ST_Call; - stat->func = ret->fnsig; + if(nparam > 1 && ret->type != T_fn) + yyerror("Too many parameters to assignement"); + if(((Tag*)&ret->type)->type != T_fn){ + type = BI_assign; + assign_sig.params[0] = *((Tag*)&ret->type); + fnsig = &assign_sig; } else { - type = ST_Varuse; - stat->var_id = ret->id; + fnsig = ret->fnsig; + type = ST_Call; + stat->func = fnsig; + } + if(nparam){ + if(!fnsig) + yyerror("Calling function that has not been specified"); + if(param->child_n > fnsig->n_param) + yyerror("Too many parameters to function"); } } if(type == BI_is || type == BI_cast) diff --git a/src/parser.h b/src/parser.h index 4e252f2..88382de 100644 --- a/src/parser.h +++ b/src/parser.h @@ -16,7 +16,7 @@ Statement *make_iconst(i32 val); Statement *make_fconst(float val); Statement *make_strconst(char *str); -Statement *declare(i32 type, Tag vtype, char *name, Statement *assign); +Statement *declare(i32 type, Tag vtype, char *name); Statement *variable_get(char *name); @@ -29,4 +29,3 @@ Statement *make_operation(i32 type, Tag vtype, char *str, i32 nparam, ...); void set_entry_point(Statement *statement); extern Tag ntag; -extern Tag atag; diff --git a/src/parser.y b/src/parser.y index 1e1554e..a2ad6ef 100644 --- a/src/parser.y +++ b/src/parser.y @@ -65,13 +65,13 @@ expr: '(' G_IDENT ')' {$$ = make_operation(ST_Call,ntag,$2,0);} | '(' VAR G_IDENT ')' - { $$ = declare(BI_var,atag,$3,NULL);} + { $$ = declare(BI_var,ntag,$3);} | '(' VAR type G_IDENT ')' - {$$ = declare(BI_var, $3, $4, NULL);} + {$$ = declare(BI_var, $3, $4);} | '(' VAR G_IDENT expr')' - {$$ = declare(BI_var,atag,$3,$4);} + {$$ = declare(BI_var,ntag,$3);} | '(' VAR type G_IDENT expr ')' - {$$ = declare(BI_var,$3,$4,$5);} + {$$ = declare(BI_var,$3,$4);} | '(' LET G_IDENT ')' {} | '(' LET type G_IDENT ')' diff --git a/src/types.h b/src/types.h index 87f77b4..b68655a 100755 --- a/src/types.h +++ b/src/types.h @@ -28,6 +28,5 @@ enum ValTypes { T_float = 3, T_str = 4, T_fn = 5, - T_any = 6 };