WIP
This commit is contained in:
parent
74f6e88019
commit
61138fd82e
11 changed files with 137 additions and 84 deletions
3
TODO.md
3
TODO.md
|
@ -1,3 +1,6 @@
|
|||
tmp:
|
||||
- Finish varuse
|
||||
|
||||
- Load files
|
||||
- Benchmarks
|
||||
- Tests
|
||||
|
|
|
@ -10,43 +10,42 @@ enum StatementTypes {
|
|||
ST_None = 0, // Not processed yet
|
||||
ST_Call = 1, // Any kind of function call
|
||||
ST_Const = 2, // Constant
|
||||
ST_Var = 3, // Variable
|
||||
ST_Var = 3, // Free variable
|
||||
ST_Block = 4, // Scope blocks
|
||||
__stpass,
|
||||
ST_Varuse = 5, // Variable assign/potential func call
|
||||
ST__end
|
||||
};
|
||||
|
||||
// Special expressions and ones that map directly to bytecode
|
||||
// Should mesh with StatementTypes
|
||||
enum BuiltinStatements {
|
||||
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_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__end
|
||||
};
|
||||
|
||||
|
@ -89,4 +88,4 @@ typedef struct {
|
|||
|
||||
} ASTStack;
|
||||
|
||||
_Static_assert((int)BI_assign >= ST__end);
|
||||
_Static_assert((int)BI_var >= ST__end);
|
||||
|
|
|
@ -4,3 +4,6 @@
|
|||
// 1 : Some checks and prints
|
||||
// 2 : All checks
|
||||
#define DEBUG 2
|
||||
|
||||
// TODO : Automate this
|
||||
#define INTERPR
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "fixed.h"
|
||||
#include "types.h"
|
||||
#include "config.h"
|
||||
#include "code_defs.h"
|
||||
#include "byte_defs.h"
|
||||
#include "exec_common.h"
|
||||
|
@ -52,12 +53,9 @@ 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);
|
||||
|
@ -450,5 +448,6 @@ 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,4 +22,6 @@ void *get_addr(u32 pos);
|
|||
|
||||
Value is(Tag tag, Value b);
|
||||
|
||||
Value cast(Tag tag, Value b);
|
||||
|
||||
void symbol_map_add_bi(HashMap *map);
|
||||
|
|
|
@ -11,11 +11,12 @@
|
|||
|
||||
typedef struct {
|
||||
|
||||
i32 hash; // Hashs are internally 24bit for bytecode ops
|
||||
i32 hash;
|
||||
char str[32];
|
||||
i32 id;
|
||||
void *fnsig;
|
||||
i32 type;
|
||||
i32 is_const;
|
||||
|
||||
} MapItem;
|
||||
|
||||
|
|
|
@ -3,16 +3,80 @@
|
|||
#include <math.h>
|
||||
|
||||
#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;
|
||||
|
@ -27,13 +91,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 "is"
|
||||
// Init the relevant value for correct type checks
|
||||
printf("var : %d\n", stat->var_type.type);
|
||||
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])))
|
||||
|
@ -54,31 +118,15 @@ 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;
|
||||
Statement **params = NULL;
|
||||
if(fn->n_param)
|
||||
params = (Statement**)((Statement*)stat->children[0])->children;
|
||||
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;
|
||||
}
|
||||
}
|
||||
returnv = fncall(fn, params);
|
||||
break;
|
||||
}
|
||||
return returnv;
|
||||
|
|
|
@ -17,6 +17,8 @@ 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)
|
||||
|
@ -58,8 +60,9 @@ Statement *make_strconst(char *str){
|
|||
|
||||
}
|
||||
|
||||
Statement *declare(i32 type, Tag vtype, char *name){
|
||||
Statement *declare(i32 type, Tag vtype, char *name, Statement *assign){
|
||||
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;
|
||||
|
@ -70,6 +73,12 @@ Statement *declare(i32 type, Tag vtype, char *name){
|
|||
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;
|
||||
|
||||
return stat;
|
||||
|
@ -106,10 +115,6 @@ 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)
|
||||
|
@ -124,27 +129,18 @@ 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(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;
|
||||
if(ret->is_const && ret->type == T_fn){
|
||||
type = ST_Call;
|
||||
stat->func = ret->fnsig;
|
||||
}
|
||||
else {
|
||||
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");
|
||||
type = ST_Varuse;
|
||||
stat->var_id = ret->id;
|
||||
}
|
||||
}
|
||||
if(type == BI_is || type == BI_cast)
|
||||
|
|
|
@ -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 *declare(i32 type, Tag vtype, char *name, Statement *assign);
|
||||
|
||||
Statement *variable_get(char *name);
|
||||
|
||||
|
@ -29,3 +29,4 @@ Statement *make_operation(i32 type, Tag vtype, char *str, i32 nparam, ...);
|
|||
void set_entry_point(Statement *statement);
|
||||
|
||||
extern Tag ntag;
|
||||
extern Tag atag;
|
||||
|
|
|
@ -65,13 +65,13 @@ expr:
|
|||
'(' G_IDENT ')'
|
||||
{$$ = make_operation(ST_Call,ntag,$2,0);}
|
||||
| '(' VAR G_IDENT ')'
|
||||
{ $$ = declare(BI_var,ntag,$3);}
|
||||
{ $$ = declare(BI_var,atag,$3,NULL);}
|
||||
| '(' VAR type G_IDENT ')'
|
||||
{$$ = declare(BI_var, $3, $4);}
|
||||
{$$ = declare(BI_var, $3, $4, NULL);}
|
||||
| '(' VAR G_IDENT expr')'
|
||||
{$$ = declare(BI_var,ntag,$3);}
|
||||
{$$ = declare(BI_var,atag,$3,$4);}
|
||||
| '(' VAR type G_IDENT expr ')'
|
||||
{$$ = declare(BI_var,$3,$4);}
|
||||
{$$ = declare(BI_var,$3,$4,$5);}
|
||||
| '(' LET G_IDENT ')'
|
||||
{}
|
||||
| '(' LET type G_IDENT ')'
|
||||
|
|
|
@ -28,5 +28,6 @@ enum ValTypes {
|
|||
T_float = 3,
|
||||
T_str = 4,
|
||||
T_fn = 5,
|
||||
T_any = 6
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue