ADD: syntaxic sugar for function definitions.

main
bog 2023-08-28 14:49:42 +02:00
parent 9ab09e85f6
commit 5825f5b316
5 changed files with 74 additions and 22 deletions

View File

@ -167,6 +167,28 @@ void compile_node(compiler* self, node* root, program* prog)
program_add_instr(prog, OP_ADEREF, NO_PARAM); program_add_instr(prog, OP_ADEREF, NO_PARAM);
} }
// Variables stuff // Variables stuff
else if (root->type == NODE_FUNDECL)
{
cstatic cs;
cstatic_init(&cs);
type* ty = cstatic_resolve_new(&cs,
self->sym,
root->children.data[1]);
size_t id = symtable_declare(
self->sym,
root->children.data[0]->value,
ty,
root->children.data[1]
);
compile_node(self, root->children.data[1], prog);
program_add_instr(prog, OP_STORE, id);
type_free(ty);
free(ty);
cstatic_free(&cs);
}
else if (root->type == NODE_CONSTDECL) else if (root->type == NODE_CONSTDECL)
{ {
cstatic cs; cstatic cs;

View File

@ -726,7 +726,8 @@ int cstatic_check(cstatic* self, node* ast, symtable* sym, char* msg, size_t siz
return 1; return 1;
} }
if (ast->type == NODE_VARDECL if (ast->type == NODE_VARDECL
|| ast->type == NODE_CONSTDECL) || ast->type == NODE_CONSTDECL
|| ast->type == NODE_FUNDECL)
{ {
char const* name = ast->children.data[0]->value; char const* name = ast->children.data[0]->value;
symentry* entry = symtable_find(sym, name); symentry* entry = symtable_find(sym, name);

View File

@ -16,7 +16,8 @@
G(NODE_IF), G(NODE_ELSE), G(NODE_BLOCK), \ G(NODE_IF), G(NODE_ELSE), G(NODE_BLOCK), \
G(NODE_WHILE), G(NODE_CONTINUE), G(NODE_BREAK), \ G(NODE_WHILE), G(NODE_CONTINUE), G(NODE_BREAK), \
G(NODE_COLON), G(NODE_FUN_TYPE), G(NODE_RETURN), G(NODE_FUN), \ G(NODE_COLON), G(NODE_FUN_TYPE), G(NODE_RETURN), G(NODE_FUN), \
G(NODE_FUN_PARAM), G(NODE_FUN_RET), G(NODE_CALL), G(NODE_ARGS) G(NODE_FUN_PARAM), G(NODE_FUN_RET), G(NODE_CALL), G(NODE_ARGS), \
G(NODE_FUNDECL)
#include "mutils.h" #include "mutils.h"

View File

@ -29,7 +29,7 @@
%token BREAK %token BREAK
%left ASSERT %left ASSERT
%token <str> TYPE BOOLEAN INTEGER FLOAT STRING IDENT %token <str> TYPE BOOLEAN INTEGER FLOAT STRING IDENT
%type <n_children> expr exprs prog array builtins %type <n_children> expr exprs prog array builtins fun_body
%type <n_children> expr_list type type_list ident if %type <n_children> expr_list type type_list ident if
%type <n_children> fun_rets fun_params fun_param fun %type <n_children> fun_rets fun_params fun_param fun
%type <n_children> fun_ret_list fun_call fun_args any instr %type <n_children> fun_ret_list fun_call fun_args any instr
@ -595,33 +595,49 @@ fun_rets:
; ;
fun: fun:
FUN OPAR fun_params CPAR fun_rets block END { FUN ident fun_body {
node* n = malloc(sizeof(node)); node* n = malloc(sizeof(node));
node_init(n, NODE_FUN, "", line); node_init(n, NODE_FUN, "", line);
size_t const SZ = $3;
node* block_node = stack_pop(); node* all[SZ];
assert(block_node);
node* ret_node = stack_pop(); for (size_t i=0; i<SZ; i++)
assert(ret_node);
if ($3 > 0)
{ {
node* params[$3]; all[SZ - 1 - i] = stack_pop();
}
node* ident_node = stack_pop();
for (size_t i=0; i<$3; i++) for (size_t i=0; i<SZ; i++)
{ {
params[i] = stack_pop(); node_add_child(n, all[i]);
} }
node* fdecl = malloc(sizeof(node));
node_init(fdecl, NODE_FUNDECL, "", line);
node_add_child(fdecl, ident_node);
node_add_child(fdecl, n);
stack_push(fdecl);
$$ = 1;
}
for (size_t i=0; i<$3; i++) | FUN fun_body {
{ node* n = malloc(sizeof(node));
node_add_child(n, params[$3 - 1 - i]); node_init(n, NODE_FUN, "", line);
} size_t const SZ = $2;
node* all[SZ];
for (size_t i=0; i<SZ; i++)
{
all[SZ - 1 - i] = stack_pop();
} }
node_add_child(n, ret_node); for (size_t i=0; i<SZ; i++)
node_add_child(n, block_node); {
node_add_child(n, all[i]);
}
stack_push(n); stack_push(n);
@ -629,6 +645,12 @@ fun:
} }
; ;
fun_body:
OPAR fun_params CPAR fun_rets block END {
$$ = $2 + $4 + $5;
}
;
fun_args: fun_args:
expr_list { expr_list {
node* n = malloc(sizeof(node)); node* n = malloc(sizeof(node));

View File

@ -30,3 +30,9 @@ end
assert 43 == *d assert 43 == *d
fun e (n as int) as int
return n * 2
end
assert 14 == e 7