]> ocean-lang.org Git - ocean/commitdiff
oceani: factor our common code in DeclareFunction
authorNeilBrown <neil@brown.name>
Fri, 12 Nov 2021 10:29:46 +0000 (21:29 +1100)
committerNeilBrown <neil@brown.name>
Sat, 13 Nov 2021 22:50:58 +0000 (09:50 +1100)
Rather than writing the same thing three times, use a function.

Signed-off-by: NeilBrown <neil@brown.name>
csrc/oceani.mdc

index 590add27c0782c52388c1752651b145fc0a5cfda..c89ecafc6c33890f838890eceb91b815e07575f3 100644 (file)
@@ -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.
 
 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
 ###### top level grammar
 
        $*variable
-       DeclareFunction -> func FuncName ( OpenScope Args ) Block Newlines ${ {
-                       struct text funcname = { " func", 5};
-                       $0 = $<FN;
-                       if ($0) {
-                               struct value fn = {.function = $<Bl};
-                               $0->type = add_type(c, funcname, &function_prototype);
-                               $0->type->function.params = reorder_bilist($<Ar);
-                               global_alloc(c, $0->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 = $<FN;
-                       if ($0) {
-                               struct value fn = {.function = $<Bl};
-                               $0->type = add_type(c, funcname, &function_prototype);
-                               $0->type->function.params = reorder_bilist($<Ar);
-                               global_alloc(c, $0->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 = $<FN;
-                       if ($0) {
-                               struct value fn = {.function = $<Bl};
-                               $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, $<FN, $<Ar, $<Bl);
+               }$
+               | func FuncName IN OpenScope OptNL Args OUT OptNL do Block Newlines ${
+                       $0 = declare_function(c, $<FN, $<Ar, $<Bl);
+               }$
+               | func FuncName NEWLINE OpenScope OptNL do Block Newlines ${
+                       $0 = declare_function(c, $<FN, NULL, $<Bl);
+               }$
 
 ###### print func decls
        {
 
 ###### print func decls
        {