ADD: high order functions.

main
bog 2023-08-28 22:43:37 +02:00
parent 9b31a14723
commit bc869aa6e8
3 changed files with 18 additions and 11 deletions

View File

@ -63,7 +63,6 @@ type* cstatic_resolve_new(cstatic* self, symtable* sym, node* ast)
type_new_from_node( type_new_from_node(
ast->children.data[i]->children.data[1] ast->children.data[i]->children.data[1]
); );
type_add_sub_type(ty, param_ty); type_add_sub_type(ty, param_ty);
type_free(param_ty); free(param_ty); type_free(param_ty); free(param_ty);
@ -74,7 +73,7 @@ type* cstatic_resolve_new(cstatic* self, symtable* sym, node* ast)
&& ast->children.data[i]->type == NODE_FUN_RET) && ast->children.data[i]->type == NODE_FUN_RET)
{ {
node* ret = ast->children.data[i]; node* ret = ast->children.data[i];
for (size_t i=0; i<ret->children.size; i++) for (size_t i=0; i<ret->children.size; i++)
{ {
type* r = type_new_from_node(ret->children.data[i]); type* r = type_new_from_node(ret->children.data[i]);
@ -90,7 +89,6 @@ type* cstatic_resolve_new(cstatic* self, symtable* sym, node* ast)
if (ast->type == NODE_CALL) if (ast->type == NODE_CALL)
{ {
char const* fun_name = ast->children.data[0]->value; char const* fun_name = ast->children.data[0]->value;
symentry* entry = symtable_find(sym, fun_name); symentry* entry = symtable_find(sym, fun_name);
fun_info* info = NULL; fun_info* info = NULL;
@ -121,17 +119,18 @@ type* cstatic_resolve_new(cstatic* self, symtable* sym, node* ast)
assert(fun_type); assert(fun_type);
size_t ret_count = fun_type->sub_types.size; size_t ret_count = fun_type->sub_types.size;
if (ret_count > 0) if (ret_count > 0)
{ {
type* ret_type = fun_type->sub_types.data[ret_count - 1]; type* ret_type =
fun_type->sub_types.data[ret_count - 1];
assert(ret_type); assert(ret_type);
if (ret_type->kind == KIND_RETURN) if (ret_type->kind == KIND_RETURN)
{ {
type* res = type_new_clone(ret_type); type* res = type_new_clone(ret_type);
res->kind = KIND_CONJUNCTION; res->kind = KIND_CONJUNCTION;
return res; return res;
@ -190,8 +189,6 @@ type* cstatic_resolve_new(cstatic* self, symtable* sym, node* ast)
|| ast->type == NODE_CONSTDECL || ast->type == NODE_CONSTDECL
|| ast->type == NODE_FUNDECL) || ast->type == NODE_FUNDECL)
{ {
char* name = ast->children.data[0]->value;
printf("-> %s\n", name);
type* ty = cstatic_resolve_new(self, type* ty = cstatic_resolve_new(self,
sym, sym,
ast->children.data[1]); ast->children.data[1]);
@ -448,7 +445,7 @@ int cstatic_check(cstatic* self, node* ast, symtable* sym, char* msg, size_t siz
} }
assert(fun_res); assert(fun_res);
assert(ret_ty);
int status = cstatic_check_same_type_ptr( int status = cstatic_check_same_type_ptr(
self, self,
ast, ast,
@ -458,7 +455,7 @@ int cstatic_check(cstatic* self, node* ast, symtable* sym, char* msg, size_t siz
msg, msg,
size size
); );
if (status) if (status)
{ {
if (fun_res) if (fun_res)

View File

@ -35,6 +35,7 @@ type* type_init_from_node(node* root)
for (size_t i=0; i< rets->children.size; i++) for (size_t i=0; i< rets->children.size; i++)
{ {
type* t = type_new_from_node(rets->children.data[i]); type* t = type_new_from_node(rets->children.data[i]);
t->kind = KIND_RETURN;
type_add_sub_type(ty, t); type_add_sub_type(ty, t);
type_free(t); free(t); type_free(t); free(t);
} }

View File

@ -46,3 +46,12 @@ end
assert 120 == f 5 assert 120 == f 5
fun h (n as int, m as fun<int:int>) as int
return (m (m n))
end
fun i (n as int) as int
return n * 3
end
assert 27 == h 3, i