]> ocean-lang.org Git - ocean/blobdiff - csrc/oceani.mdc
oceani: Improve Rboolok handling.
[ocean] / csrc / oceani.mdc
index f257736edc328baf8ba313d28f3bd7826e4e5004..19c9471cec06bb1bc331f26a3e3cb4fecd1864c1 100644 (file)
@@ -1575,7 +1575,7 @@ propagation is needed.
 
 ###### ast
 
-       enum val_rules {Rnolabel = 1<<0, Rboolok = 1<<1, Rnoconstant = 2<<1};
+       enum val_rules {Rnolabel = 1<<0, Rboolok = 1<<1, Rnoconstant = 1<<2};
 
 ###### format cases
        case 'r':
@@ -2340,19 +2340,22 @@ has a type which includes the set of parameters and the return value.
 As yet these types cannot be declared separately from the function
 itself.
 
-The parameters can be specified either in parentheses as a list, such as
+The parameters can be specified either in parentheses as a ';' separated
+list, such as
 
 ##### Example: function 1
 
-       func main(av:[ac::number]string)
+       func main(av:[ac::number]string; env:[envc::number]string)
                code block
 
-or as an indented list of one parameter per line
+or as an indented list of one parameter per line (though each line can
+be a ';' separated list)
 
 ##### Example: function 2
 
        func main
                argv:[argc::number]string
+               env:[envc::number]string
        do
                code block
 
@@ -2479,12 +2482,21 @@ further detailed when Expression Lists are introduced.
 
 
        $*binode
-       Args -> ${ $0 = NULL; }$
+       Args -> ArgsLine NEWLINE ${ $0 = $<AL; }$
+               | Args ArgsLine NEWLINE ${ {
+                       struct binode *b = $<AL;
+                       struct binode **bp = &b;
+                       while (*bp)
+                               bp = (struct binode **)&(*bp)->left;
+                       *bp = $<A;
+                       $0 = b;
+               } }$
+
+       ArgsLine -> ${ $0 = NULL; }$
                | Varlist ${ $0 = $<1; }$
                | Varlist ; ${ $0 = $<1; }$
-               | Varlist NEWLINE ${ $0 = $<1; }$
 
-       Varlist -> Varlist ; ArgDecl ${ // UNTESTED
+       Varlist -> Varlist ; ArgDecl ${
                        $0 = new(binode);
                        $0->op = List;
                        $0->left = $<Vl;
@@ -3734,9 +3746,14 @@ is in-place.
 
                for (e = b; e; e = cast(binode, e->right)) {
                        t = propagate_types(e->left, c, ok, NULL, rules);
-                       if ((rules & Rboolok) && t == Tbool)
+                       if ((rules & Rboolok) && (t == Tbool || t == Tnone))
                                t = NULL;
-                       if (t && t != Tnone && t != Tbool) {
+                       if (t == Tnone && e->right)
+                               /* Only the final statement *must* return a value
+                                * when not Rboolok
+                                */
+                               t = NULL;
+                       if (t) {
                                if (!type)
                                        type = t;
                                else if (t != type)
@@ -4703,46 +4720,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 = $<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 then IN OpenScope OptNL Args OUT OptNL do Block Newlines ${ {
-                       // FIXME that 'then' should not be there.
-                       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 ArgsLine ) Block Newlines ${
+                       $0 = declare_function(c, $<FN, $<Ar, $<Bl);
+               }$
+               | func FuncName IN OpenScope 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
        {
@@ -4779,9 +4787,10 @@ analysis is a bit more interesting at this level.
        static int analyse_funcs(struct parse_context *c)
        {
                struct variable *v;
-               int ok = 1;
-               for (v = c->in_scope; ok && v; v = v->in_scope) {
+               int all_ok = 1;
+               for (v = c->in_scope; v; v = v->in_scope) {
                        struct value *val;
+                       int ok = 1;
                        if (v->depth != 0 || !v->type || !v->type->check_args)
                                continue;
                        val = var_value(c, v);
@@ -4792,9 +4801,11 @@ analysis is a bit more interesting at this level.
                        if (ok)
                                /* Make sure everything is still consistent */
                                propagate_types(val->function, c, &ok, Tnone, 0);
+                       if (!ok)
+                               all_ok = 0;
                        v->type->function.local_size = scope_finalize(c);
                }
-               return ok;
+               return all_ok;
        }
 
        static int analyse_main(struct type *type, struct parse_context *c)