From 499033323e79fc5d2b757c2055ad5bd0f55bc7f3 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Fri, 12 Nov 2021 21:29:46 +1100 Subject: [PATCH] oceani: factor our common code in DeclareFunction Rather than writing the same thing three times, use a function. Signed-off-by: NeilBrown --- csrc/oceani.mdc | 64 ++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/csrc/oceani.mdc b/csrc/oceani.mdc index 590add2..c89ecaf 100644 --- a/csrc/oceani.mdc +++ b/csrc/oceani.mdc @@ -4703,45 +4703,37 @@ The function is not interpreted by `interp_exec` as that isn't passed the argument list which the program requires. Similarly type analysis is a bit more interesting at this level. +###### ast functions + + static struct variable *declare_function(struct parse_context *c, + struct variable *name, + struct binode *args, + struct exec *code) + { + struct text funcname = {" func", 5}; + if (name) { + struct value fn = {.function = code}; + name->type = add_type(c, funcname, &function_prototype); + name->type->function.params = reorder_bilist(args); + global_alloc(c, name->type, name, &fn); + var_block_close(c, CloseSequential, code); + } else + var_block_close(c, CloseSequential, NULL); + return name; + } + ###### top level grammar $*variable - DeclareFunction -> func FuncName ( OpenScope Args ) Block Newlines ${ { - struct text funcname = { " func", 5}; - $0 = $type = add_type(c, funcname, &function_prototype); - $0->type->function.params = reorder_bilist($type, $0, &fn); - var_block_close(c, CloseSequential, fn.function); - } else - var_block_close(c, CloseSequential, NULL); - } }$ - | func FuncName IN OpenScope OptNL Args OUT OptNL do Block Newlines ${ { - struct text funcname = { " func", 5}; - $0 = $type = add_type(c, funcname, &function_prototype); - $0->type->function.params = reorder_bilist($type, $0, &fn); - var_block_close(c, CloseSequential, fn.function); - } else - var_block_close(c, CloseSequential, NULL); - } }$ - | func FuncName NEWLINE OpenScope OptNL do Block Newlines ${ { - struct text funcname = { " func", 5}; - $0 = $type = add_type(c, funcname, &function_prototype); - $0->type->function.params = NULL; - global_alloc(c, $0->type, $0, &fn); - var_block_close(c, CloseSequential, fn.function); - } else - var_block_close(c, CloseSequential, NULL); - } }$ + DeclareFunction -> func FuncName ( OpenScope Args ) Block Newlines ${ + $0 = declare_function(c, $