It's alive !
This commit is contained in:
parent
b48a2e542e
commit
0b9f06f2d3
6 changed files with 31 additions and 22 deletions
|
@ -46,6 +46,7 @@ enum BuiltinStatements {
|
|||
BI_push = 30,
|
||||
BI_pop = 31,
|
||||
BI_write = 32,
|
||||
BI_nwline = 33,
|
||||
BI__end
|
||||
};
|
||||
|
||||
|
|
|
@ -233,6 +233,12 @@ Value write(Value a){
|
|||
default:
|
||||
break;
|
||||
}
|
||||
return (Value){.tag={0,T_null}};
|
||||
}
|
||||
|
||||
Value nwline(){
|
||||
printf("\n");
|
||||
return (Value){.tag={0,T_null}};
|
||||
}
|
||||
|
||||
const Tag tany = {0,T_null};
|
||||
|
@ -262,7 +268,8 @@ FnSig builtins[] = {
|
|||
{{tvec} ,1,1,{.bi = BICAST(len)}},
|
||||
{{tvec,tany},2,1,{.bi = BICAST(push)}},
|
||||
{{tvec,tint},2,1,{.bi = BICAST(pop)}},
|
||||
{{tany} ,1,1,{.bi = BICAST(write)}}
|
||||
{{tany} ,1,1,{.bi = BICAST(write)}},
|
||||
{{0} ,0,1,{.bi = BICAST(nwline)}}
|
||||
};
|
||||
|
||||
char *bi_names[] = {
|
||||
|
@ -282,7 +289,8 @@ char *bi_names[] = {
|
|||
"len",
|
||||
"push",
|
||||
"pop",
|
||||
"write"
|
||||
"write",
|
||||
"newline"
|
||||
};
|
||||
|
||||
void symbol_map_add_bi(HashMap *map){
|
||||
|
|
|
@ -53,7 +53,9 @@ Value execute(Statement *stat){
|
|||
break;
|
||||
case ST_Call:
|
||||
FnSig *fn = stat->func;
|
||||
Statement **params = ((Statement*)stat->children[0])->children;
|
||||
Statement **params;
|
||||
if(fn->n_param)
|
||||
params = ((Statement*)stat->children[0])->children;
|
||||
if(fn->is_builtin){
|
||||
switch(fn->n_param){
|
||||
case 0:
|
||||
|
|
|
@ -38,7 +38,6 @@ char *bi_type_names[6] = {
|
|||
};
|
||||
|
||||
Tag get_type(char *str){
|
||||
puts("get_type");
|
||||
for(int i = 1; i < 6; i++){
|
||||
if(!strcmp(bi_type_names[i], str))
|
||||
return (Tag){0, i};
|
||||
|
@ -47,7 +46,6 @@ Tag get_type(char *str){
|
|||
}
|
||||
|
||||
Statement *make_iconst(i32 val){
|
||||
puts("make_iconst");
|
||||
Statement *stat = &stack->statements[stack->curr_statement++];
|
||||
stat->type = ST_Const;
|
||||
stat->cons = (Value){{.v4B = {val}},{.is_array = 0, .type = T_int}};
|
||||
|
@ -58,7 +56,6 @@ Statement *make_iconst(i32 val){
|
|||
Statement *declare(i32 type, Tag vtype, char *name){
|
||||
i32 len = strcspn(name, " \t\n)");
|
||||
char *name2 = strndup(name, len);
|
||||
printf("declare : %s\n", name2);
|
||||
Statement *stat = &stack->statements[stack->curr_statement++];
|
||||
stat->type = type;
|
||||
if(hashmap_get(&stack->symbol_map, name2)){
|
||||
|
@ -74,7 +71,6 @@ Statement *declare(i32 type, Tag vtype, char *name){
|
|||
}
|
||||
|
||||
Statement *variable_get(char *name){
|
||||
puts("variable_get");
|
||||
Statement *stat = &stack->statements[stack->curr_statement++];
|
||||
MapItem *ret = hashmap_get(&stack->symbol_map, name);
|
||||
if(!ret)
|
||||
|
@ -87,7 +83,6 @@ Statement *variable_get(char *name){
|
|||
}
|
||||
|
||||
Statement *make_block(Statement *first){
|
||||
puts("make_block");
|
||||
Statement *stat = &stack->statements[stack->curr_statement++];
|
||||
stat->type = ST_Block;
|
||||
stat->children = malloc(sizeof(void*));
|
||||
|
@ -97,7 +92,6 @@ Statement *make_block(Statement *first){
|
|||
}
|
||||
|
||||
Statement *add_block(Statement *block, Statement *add){
|
||||
puts("add_block");
|
||||
block->children = realloc(block->children, sizeof(void*)*(block->child_n+1));
|
||||
block->children[block->child_n++] = add;
|
||||
return block;
|
||||
|
@ -109,43 +103,48 @@ FnSig bi_sig[] = {
|
|||
|
||||
};
|
||||
|
||||
Statement *make_operation(i32 type, i32 extrainf, char *name, i32 nparam,
|
||||
Statement *param, ...){
|
||||
printf("make_operation: ");
|
||||
Statement *make_operation(i32 type, i32 extrainf, char *name, i32 nparam, ...){
|
||||
Statement *stat = &stack->statements[stack->curr_statement++];
|
||||
if(nparam)
|
||||
stat->children = malloc(sizeof(void*)*nparam);
|
||||
stat->child_n = 0;
|
||||
FnSig *fnsig = NULL;
|
||||
|
||||
va_list prm;
|
||||
va_start(prm, nparam);
|
||||
Statement *param = va_arg(prm, Statement*);
|
||||
|
||||
if(!type){
|
||||
i32 len = strcspn(name, " \t\n");
|
||||
char *name2 = strndup(name, len);
|
||||
puts(name2);
|
||||
MapItem *ret = hashmap_get(&stack->symbol_map, name2);
|
||||
if(!ret)
|
||||
yyerror("Undefined identifer");
|
||||
if(nparam > 1 && ret->type != T_fn)
|
||||
yyerror("Too many parameters to assignement");
|
||||
if(ret->type != T_fn){
|
||||
if(((Tag*)&ret->type)->type != T_fn){
|
||||
type = BI_assign;
|
||||
assign_sig.params[0] = *((Tag*)&ret->type);
|
||||
fnsig = &assign_sig;
|
||||
}
|
||||
else {
|
||||
fnsig = ret->fnsig;
|
||||
type = ST_Call;
|
||||
stat->func = fnsig;
|
||||
}
|
||||
if(param->child_n > fnsig->n_param)
|
||||
yyerror("Too many parameters to function");
|
||||
}
|
||||
stat->type = type;
|
||||
|
||||
va_list prm;
|
||||
va_start(prm, param);
|
||||
for(int i = 0; i < nparam; i++){
|
||||
if(nparam)
|
||||
stat->children[stat->child_n++] = param;
|
||||
// TODO : Type checks
|
||||
for(int i = 1; i < nparam; i++){
|
||||
Statement *prm_n = va_arg(prm, Statement*);
|
||||
stat->children[stat->child_n++] = prm_n;
|
||||
stack->curr_statement++;
|
||||
}
|
||||
va_end(prm);
|
||||
|
||||
return stat;
|
||||
}
|
||||
|
|
|
@ -24,8 +24,7 @@ Statement *make_block(Statement *first);
|
|||
|
||||
Statement *add_block(Statement *block, Statement *add);
|
||||
|
||||
Statement *make_operation(i32 type, i32 extainf, char *str, i32 nparam,
|
||||
Statement *param, ...);
|
||||
Statement *make_operation(i32 type, i32 extainf, char *str, i32 nparam, ...);
|
||||
|
||||
void set_entry_point(Statement *statement);
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ fn_args:
|
|||
|
||||
expr:
|
||||
'(' G_IDENT ')'
|
||||
{$$ = make_operation(ST_Call, 0, $2, 0, NULL);}
|
||||
{$$ = make_operation(ST_Call,0,$2,0);}
|
||||
| '(' VAR G_IDENT ')'
|
||||
{ $$ = declare(BI_var,ntag,$3);}
|
||||
| '(' VAR type G_IDENT ')'
|
||||
|
@ -116,7 +116,7 @@ void yyerror(char *s){
|
|||
}
|
||||
|
||||
int main(){
|
||||
printf("%d\n", sizeof(ValueStr));
|
||||
printf("fLisp interactive env - v0.1\nFcalva 2025\nUnder the terms of the GPL v3.0 license\n");
|
||||
make_stack();
|
||||
yyparse();
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Reference in a new issue