WIP2
This commit is contained in:
parent
4eddd7778b
commit
6128ab0884
8 changed files with 98 additions and 3 deletions
3
TODO.md
3
TODO.md
|
@ -1,5 +1,6 @@
|
||||||
tmp:
|
tmp:
|
||||||
- Finish varuse
|
- Finish varuse : ~
|
||||||
|
- Fix crash on (newline)
|
||||||
|
|
||||||
- Load files
|
- Load files
|
||||||
- Benchmarks
|
- Benchmarks
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
// Debug checks
|
||||||
// 0 : None (still features runtime debug)
|
// 0 : None (still features runtime debug)
|
||||||
// 1 : Some checks and prints
|
// 1 : Some checks
|
||||||
// 2 : All checks
|
// 2 : All checks
|
||||||
#define DEBUG 2
|
#define DEBUG 2
|
||||||
|
|
||||||
|
// Debug logs
|
||||||
|
// 0 : None
|
||||||
|
// 1 : All misc debug logs
|
||||||
|
#define LOG 1
|
||||||
|
|
||||||
// TODO : Automate this
|
// TODO : Automate this
|
||||||
#define INTERPR
|
#define INTERPR
|
||||||
|
|
|
@ -103,6 +103,73 @@ void *get_addr(u32 pos){
|
||||||
return internal_buf + 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
|
// Lesser special functions
|
||||||
|
|
||||||
Value is(Tag tag, Value b){
|
Value is(Tag tag, Value b){
|
||||||
|
|
|
@ -75,6 +75,7 @@ MapItem *hashmap_insert(HashMap *map, char *str){
|
||||||
|
|
||||||
if(!map->bit_free[hsh/32]){
|
if(!map->bit_free[hsh/32]){
|
||||||
map->buffer[hsh].hash = hsh;
|
map->buffer[hsh].hash = hsh;
|
||||||
|
map->buffer[hsh].id = map->curr_id++;
|
||||||
strncpy(map->buffer[hsh].str, str, 32);
|
strncpy(map->buffer[hsh].str, str, 32);
|
||||||
set_bit(map->bit_free, hsh);
|
set_bit(map->bit_free, hsh);
|
||||||
map->item_n++;
|
map->item_n++;
|
||||||
|
@ -96,6 +97,7 @@ MapItem *hashmap_insert(HashMap *map, char *str){
|
||||||
|
|
||||||
if(!taken){
|
if(!taken){
|
||||||
map->buffer[pos].hash = hsh;
|
map->buffer[pos].hash = hsh;
|
||||||
|
map->buffer[pos].id = map->curr_id++;
|
||||||
strncpy(map->buffer[pos].str, str, 32);
|
strncpy(map->buffer[pos].str, str, 32);
|
||||||
set_bit(map->bit_free, hsh);
|
set_bit(map->bit_free, hsh);
|
||||||
map->item_n++;
|
map->item_n++;
|
||||||
|
|
|
@ -25,6 +25,7 @@ typedef struct {
|
||||||
MapItem *buffer;
|
MapItem *buffer;
|
||||||
u32 *bit_free; // Bit map to track usage
|
u32 *bit_free; // Bit map to track usage
|
||||||
i32 curr_len; // In items
|
i32 curr_len; // In items
|
||||||
|
i32 curr_id;
|
||||||
i32 item_n;
|
i32 item_n;
|
||||||
i32 is_heap; // TODO
|
i32 is_heap; // TODO
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,9 @@ Value vars[SYMBOL_MAP_S];
|
||||||
Value execute(Statement *stat);
|
Value execute(Statement *stat);
|
||||||
|
|
||||||
void assign(Value *dst, Value src){
|
void assign(Value *dst, Value src){
|
||||||
|
#if LOG
|
||||||
printf("Assign : %d -> %d\n", src.tag.type, dst->tag.type);
|
printf("Assign : %d -> %d\n", src.tag.type, dst->tag.type);
|
||||||
|
#endif
|
||||||
if((dst->tag.type == src.tag.type || dst->tag.type == T_any)
|
if((dst->tag.type == src.tag.type || dst->tag.type == T_any)
|
||||||
&& dst->tag.is_array == src.tag.is_array)
|
&& dst->tag.is_array == src.tag.is_array)
|
||||||
*dst = src;
|
*dst = src;
|
||||||
|
@ -72,7 +74,9 @@ Value execute(Statement *stat){
|
||||||
fncall(fn, params);
|
fncall(fn, params);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
#if LOG
|
||||||
printf("stvar : %d\n", vars[stat->var_id].tag.type);
|
printf("stvar : %d\n", vars[stat->var_id].tag.type);
|
||||||
|
#endif
|
||||||
assign(&vars[stat->var_id], execute(stat->children[0]));
|
assign(&vars[stat->var_id], execute(stat->children[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +98,9 @@ Value execute(Statement *stat){
|
||||||
case BI_var:
|
case BI_var:
|
||||||
case BI_let:
|
case BI_let:
|
||||||
// Init the relevant value for correct type checks
|
// Init the relevant value for correct type checks
|
||||||
|
#if LOG
|
||||||
printf("var : %d\n", stat->var_type.type);
|
printf("var : %d\n", stat->var_type.type);
|
||||||
|
#endif
|
||||||
vars[stat->var_id].tag = stat->var_type;
|
vars[stat->var_id].tag = stat->var_type;
|
||||||
if(stat->child_n)
|
if(stat->child_n)
|
||||||
assign(&vars[stat->var_id],execute(stat->children[0]));
|
assign(&vars[stat->var_id],execute(stat->children[0]));
|
||||||
|
|
|
@ -62,7 +62,9 @@ Statement *make_strconst(char *str){
|
||||||
|
|
||||||
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
|
||||||
printf("declare : %d\n", vtype.type);
|
printf("declare : %d\n", vtype.type);
|
||||||
|
#endif
|
||||||
char *name2 = strndup(name, len);
|
char *name2 = strndup(name, len);
|
||||||
Statement *stat = &stack->statements[stack->curr_statement++];
|
Statement *stat = &stack->statements[stack->curr_statement++];
|
||||||
stat->type = type;
|
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);
|
MapItem *ret = hashmap_insert(&stack->symbol_map, name2);
|
||||||
ret->type = *((i16*)&vtype);
|
ret->type = *((i16*)&vtype);
|
||||||
|
#if LOG
|
||||||
|
printf("id : %d\n", ret->id);
|
||||||
|
#endif
|
||||||
stat->var_id = ret->id;
|
stat->var_id = ret->id;
|
||||||
stat->var_type = vtype;
|
stat->var_type = vtype;
|
||||||
if(assign){
|
if(assign){
|
||||||
|
@ -129,11 +134,16 @@ Statement *make_operation(i32 type, Tag vtype, char *name, i32 nparam, ...){
|
||||||
if(!type || type == ST_Call){
|
if(!type || type == ST_Call){
|
||||||
i32 len = strcspn(name, " \t\n()");
|
i32 len = strcspn(name, " \t\n()");
|
||||||
char *name2 = strndup(name, len);
|
char *name2 = strndup(name, len);
|
||||||
|
#if LOG
|
||||||
printf("name : \"%s\" ; name2 : \"%s\"\n",name,name2);
|
printf("name : \"%s\" ; name2 : \"%s\"\n",name,name2);
|
||||||
|
#endif
|
||||||
MapItem *ret = hashmap_get(&stack->symbol_map, name2);
|
MapItem *ret = hashmap_get(&stack->symbol_map, name2);
|
||||||
free(name2);
|
free(name2);
|
||||||
if(!ret)
|
if(!ret)
|
||||||
yyerror("Undefined identifer");
|
yyerror("Undefined identifer");
|
||||||
|
#if LOG
|
||||||
|
printf("id : %d\n", ret->id);
|
||||||
|
#endif
|
||||||
if(ret->is_const && ret->type == T_fn){
|
if(ret->is_const && ret->type == T_fn){
|
||||||
type = ST_Call;
|
type = ST_Call;
|
||||||
stat->func = ret->fnsig;
|
stat->func = ret->fnsig;
|
||||||
|
@ -165,6 +175,7 @@ Statement *make_operation(i32 type, Tag vtype, char *name, i32 nparam, ...){
|
||||||
return stat;
|
return stat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
void set_entry_point(Statement *statement){
|
void set_entry_point(Statement *statement){
|
||||||
printf("set_entry_point\n");
|
printf("set_entry_point\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include "exec_common.h"
|
#include "exec_common.h"
|
||||||
|
|
||||||
int yylex();
|
int yylex();
|
||||||
|
extern void print_ast(Statement *base);
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%union {
|
%union {
|
||||||
|
@ -36,7 +37,7 @@
|
||||||
%%
|
%%
|
||||||
|
|
||||||
program:
|
program:
|
||||||
expr_list ';' {execute($1);}
|
expr_list ';' {print_ast($1);execute($1);}
|
||||||
| expr_list YYEOF {execute($1);}
|
| expr_list YYEOF {execute($1);}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue