diff --git a/TODO.md b/TODO.md index f0227bb..fb1d8e8 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,6 @@ tmp: - - Finish varuse + - Finish varuse : ~ + - Fix crash on (newline) - Load files - Benchmarks diff --git a/src/config.h b/src/config.h index d7cd04e..68fdc74 100755 --- a/src/config.h +++ b/src/config.h @@ -1,9 +1,15 @@ #pragma once +// Debug checks // 0 : None (still features runtime debug) -// 1 : Some checks and prints +// 1 : Some checks // 2 : All checks #define DEBUG 2 +// Debug logs +// 0 : None +// 1 : All misc debug logs +#define LOG 1 + // TODO : Automate this #define INTERPR diff --git a/src/exec_common.c b/src/exec_common.c index ba87341..1773c16 100644 --- a/src/exec_common.c +++ b/src/exec_common.c @@ -103,6 +103,73 @@ void *get_addr(u32 pos){ return internal_buf + pos; } +Value write(Value a); + +void print_ast1(Statement *base, i32 indent){ + i32 close_parent = 1; + for(int i = 0; i < indent; i++) + printf(" "); + switch(base->type){ + case ST_None: + printf("(none\n"); + break; + case ST_Call: + printf("(call<%lx>\n", base->func); + break; + case ST_Const: + write(base->cons); + printf("\n"); + close_parent = 0; + break; + case ST_Var: + printf("<%d>\n", base->var_id); + close_parent = 0; + break; + case ST_Block: + printf("(block\n"); + break; + case ST_Varuse: + printf("(use<%d>\n", base->var_id); + break; + case BI_var: + printf("(var<%d>\n", base->var_id); + break; + case BI_if: + printf("(if\n"); + break; + case BI_else: + printf("(else\n"); + break; + case BI_while: + printf("(while\n"); + break; + case BI_is: + printf("(is<%d>\n", base->var_type.type); + break; + case BI_cast: + printf("(cast<%d>\n", base->var_type.type); + default: + printf("(other\n"); + break; + } + for(int i = 0; i < base->child_n; i++) + print_ast1(base->children[i], indent+1); + + if(close_parent){ + for(int i = 0; i < indent; i++) + printf(" "); + printf(")\n"); + } +} + +void print_ast(Statement *base){ +#if LOG + printf("AST :\n"); + print_ast1(base, 0); + printf("\n"); +#endif +} + // Lesser special functions Value is(Tag tag, Value b){ diff --git a/src/hash.c b/src/hash.c index ea25447..50d2f7a 100755 --- a/src/hash.c +++ b/src/hash.c @@ -75,6 +75,7 @@ MapItem *hashmap_insert(HashMap *map, char *str){ if(!map->bit_free[hsh/32]){ map->buffer[hsh].hash = hsh; + map->buffer[hsh].id = map->curr_id++; strncpy(map->buffer[hsh].str, str, 32); set_bit(map->bit_free, hsh); map->item_n++; @@ -96,6 +97,7 @@ MapItem *hashmap_insert(HashMap *map, char *str){ if(!taken){ map->buffer[pos].hash = hsh; + map->buffer[pos].id = map->curr_id++; strncpy(map->buffer[pos].str, str, 32); set_bit(map->bit_free, hsh); map->item_n++; diff --git a/src/hash.h b/src/hash.h index f89ec45..964e985 100755 --- a/src/hash.h +++ b/src/hash.h @@ -25,6 +25,7 @@ typedef struct { MapItem *buffer; u32 *bit_free; // Bit map to track usage i32 curr_len; // In items + i32 curr_id; i32 item_n; i32 is_heap; // TODO diff --git a/src/interpreter.c b/src/interpreter.c index 33336c5..e2b37ed 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -15,7 +15,9 @@ Value vars[SYMBOL_MAP_S]; Value execute(Statement *stat); void assign(Value *dst, Value src){ +#if LOG printf("Assign : %d -> %d\n", src.tag.type, dst->tag.type); +#endif if((dst->tag.type == src.tag.type || dst->tag.type == T_any) && dst->tag.is_array == src.tag.is_array) *dst = src; @@ -72,7 +74,9 @@ Value execute(Statement *stat){ fncall(fn, params); } else { +#if LOG printf("stvar : %d\n", vars[stat->var_id].tag.type); +#endif assign(&vars[stat->var_id], execute(stat->children[0])); } @@ -94,7 +98,9 @@ Value execute(Statement *stat){ case BI_var: case BI_let: // Init the relevant value for correct type checks +#if LOG printf("var : %d\n", stat->var_type.type); +#endif vars[stat->var_id].tag = stat->var_type; if(stat->child_n) assign(&vars[stat->var_id],execute(stat->children[0])); diff --git a/src/parse_utils.c b/src/parse_utils.c index 81fe5e2..0e1a1c8 100644 --- a/src/parse_utils.c +++ b/src/parse_utils.c @@ -62,7 +62,9 @@ Statement *make_strconst(char *str){ Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){ i32 len = strcspn(name, " \t\n)"); +#if LOG printf("declare : %d\n", vtype.type); +#endif char *name2 = strndup(name, len); Statement *stat = &stack->statements[stack->curr_statement++]; stat->type = type; @@ -71,6 +73,9 @@ Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){ } MapItem *ret = hashmap_insert(&stack->symbol_map, name2); ret->type = *((i16*)&vtype); +#if LOG + printf("id : %d\n", ret->id); +#endif stat->var_id = ret->id; stat->var_type = vtype; if(assign){ @@ -129,11 +134,16 @@ 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); +#if LOG printf("name : \"%s\" ; name2 : \"%s\"\n",name,name2); +#endif MapItem *ret = hashmap_get(&stack->symbol_map, name2); free(name2); if(!ret) yyerror("Undefined identifer"); +#if LOG + printf("id : %d\n", ret->id); +#endif if(ret->is_const && ret->type == T_fn){ type = ST_Call; stat->func = ret->fnsig; @@ -165,6 +175,7 @@ Statement *make_operation(i32 type, Tag vtype, char *name, i32 nparam, ...){ return stat; } +// TODO void set_entry_point(Statement *statement){ printf("set_entry_point\n"); } diff --git a/src/parser.y b/src/parser.y index 1e1554e..82f2f4c 100644 --- a/src/parser.y +++ b/src/parser.y @@ -9,6 +9,7 @@ #include "exec_common.h" int yylex(); + extern void print_ast(Statement *base); %} %union { @@ -36,7 +37,7 @@ %% program: - expr_list ';' {execute($1);} + expr_list ';' {print_ast($1);execute($1);} | expr_list YYEOF {execute($1);} ;