ADD: syntaxic sugar for function definitions.
parent
9ab09e85f6
commit
5825f5b316
|
@ -167,6 +167,28 @@ void compile_node(compiler* self, node* root, program* prog)
|
|||
program_add_instr(prog, OP_ADEREF, NO_PARAM);
|
||||
}
|
||||
// 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)
|
||||
{
|
||||
cstatic cs;
|
||||
|
|
|
@ -726,7 +726,8 @@ int cstatic_check(cstatic* self, node* ast, symtable* sym, char* msg, size_t siz
|
|||
return 1;
|
||||
}
|
||||
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;
|
||||
symentry* entry = symtable_find(sym, name);
|
||||
|
|
|
@ -16,7 +16,8 @@
|
|||
G(NODE_IF), G(NODE_ELSE), G(NODE_BLOCK), \
|
||||
G(NODE_WHILE), G(NODE_CONTINUE), G(NODE_BREAK), \
|
||||
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"
|
||||
|
|
60
src/parser.y
60
src/parser.y
|
@ -29,7 +29,7 @@
|
|||
%token BREAK
|
||||
%left ASSERT
|
||||
%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> fun_rets fun_params fun_param fun
|
||||
%type <n_children> fun_ret_list fun_call fun_args any instr
|
||||
|
@ -595,33 +595,49 @@ fun_rets:
|
|||
;
|
||||
|
||||
fun:
|
||||
FUN OPAR fun_params CPAR fun_rets block END {
|
||||
FUN ident fun_body {
|
||||
node* n = malloc(sizeof(node));
|
||||
node_init(n, NODE_FUN, "", line);
|
||||
size_t const SZ = $3;
|
||||
node* all[SZ];
|
||||
|
||||
node* block_node = stack_pop();
|
||||
assert(block_node);
|
||||
|
||||
node* ret_node = stack_pop();
|
||||
assert(ret_node);
|
||||
|
||||
if ($3 > 0)
|
||||
for (size_t i=0; i<SZ; i++)
|
||||
{
|
||||
node* params[$3];
|
||||
|
||||
for (size_t i=0; i<$3; i++)
|
||||
{
|
||||
params[i] = stack_pop();
|
||||
all[SZ - 1 - i] = stack_pop();
|
||||
}
|
||||
|
||||
for (size_t i=0; i<$3; i++)
|
||||
node* ident_node = stack_pop();
|
||||
|
||||
for (size_t i=0; i<SZ; i++)
|
||||
{
|
||||
node_add_child(n, params[$3 - 1 - i]);
|
||||
}
|
||||
node_add_child(n, all[i]);
|
||||
}
|
||||
|
||||
node_add_child(n, ret_node);
|
||||
node_add_child(n, block_node);
|
||||
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;
|
||||
}
|
||||
|
||||
| FUN fun_body {
|
||||
node* n = malloc(sizeof(node));
|
||||
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();
|
||||
}
|
||||
|
||||
for (size_t i=0; i<SZ; i++)
|
||||
{
|
||||
node_add_child(n, all[i]);
|
||||
}
|
||||
|
||||
stack_push(n);
|
||||
|
||||
|
@ -629,6 +645,12 @@ fun:
|
|||
}
|
||||
;
|
||||
|
||||
fun_body:
|
||||
OPAR fun_params CPAR fun_rets block END {
|
||||
$$ = $2 + $4 + $5;
|
||||
}
|
||||
;
|
||||
|
||||
fun_args:
|
||||
expr_list {
|
||||
node* n = malloc(sizeof(node));
|
||||
|
|
|
@ -30,3 +30,9 @@ end
|
|||
|
||||
assert 43 == *d
|
||||
|
||||
fun e (n as int) as int
|
||||
return n * 2
|
||||
end
|
||||
|
||||
assert 14 == e 7
|
||||
|
||||
|
|
Loading…
Reference in New Issue