]> ocean-lang.org Git - ocean/commitdiff
oceani: add a 'length' operator
authorNeilBrown <neil@brown.name>
Sat, 18 Dec 2021 03:07:39 +0000 (14:07 +1100)
committerNeilBrown <neil@brown.name>
Sat, 18 Dec 2021 03:09:44 +0000 (14:09 +1100)
array[] will now return the length of the array.

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

index 958575c63cb4e1860d4fb88d1501df726b8fc8a7..03aa85083bd25f866f0cf65ccb78402880f90d7f 100644 (file)
@@ -322,7 +322,8 @@ Here I break it into two parts, keeping the array code separate.
        func main(av:[ac::number]string)
                A := $av[1]; B := $av[2]
                astr := av[3]
-               bbool := av[ac-1] == "True"
+               l := av[]
+               bbool := av[l-1] == "True"
                print "Hello World, what lovely oceans you have!"
                /* When a variable is defined in both branches of an 'if',
                 * and used afterwards, the variables are merged.
@@ -754,7 +755,7 @@ A simple linked list example
 
                insert(list, "@start");
                insert(list, "~end")
-               for i:=1; then i=i+1; while i < ac:
+               for i:=1; then i=i+1; while i < argv[]:
                        insert(list, argv[i])
                insert(list, "Hello!")
                printlist(list)
@@ -948,6 +949,7 @@ various places that `type_err()` are called.
                while 1 if True else False:
                        print
                case 2: print "two"
+               print "one" ++ a4[], c[]
 
 ###### output: type_err3
        .tmp.code:8:12: error: expected number but variable 'c' is string
@@ -983,6 +985,8 @@ various places that `type_err()` are called.
        .tmp.code:34:14: error: cannot find requested field in foo
        .tmp.code:35:17: error: have string but need number
        .tmp.code:38:29: error: expected number found Boolean
+       .tmp.code:41:23: error: have number but need string
+       .tmp.code:41:29: error: string cannot provide length
        oceani: type error in program - not running.
 
 ###### test: type_err4
index 891ce74c2e4d097b0cb9e4143f9dac97c0ec1e77..fb154be366f3c5759231c29fd7a0309daa001581 100644 (file)
@@ -2576,7 +2576,7 @@ with a const size by whether they are prepared at parse time or not.
        } }$
 
 ###### Binode types
-       Index,
+       Index, Length,
 
 ###### term grammar
 
@@ -2588,6 +2588,13 @@ with a const size by whether they are prepared at parse time or not.
                $0 = b;
        } }$
 
+       | Term [ ] ${ {
+               struct binode *b = new(binode);
+               b->op = Length;
+               b->left = $<Term;
+               $0 = b;
+       } }$
+
 ###### print binode cases
        case Index:
                print_exec(b->left, -1, bracket);
@@ -2596,6 +2603,11 @@ with a const size by whether they are prepared at parse time or not.
                printf("]");
                break;
 
+       case Length:
+               print_exec(b->left, -1, bracket);
+               printf("[]");
+               break;
+
 ###### propagate binode cases
        case Index:
                /* left must be an array, right must be a number,
@@ -2615,6 +2627,20 @@ with a const size by whether they are prepared at parse time or not.
                }
                break;
 
+       case Length:
+               /* left must be an array, result is a number
+                */
+               t = propagate_types(b->left, c, perr, NULL, 0);
+               if (!t || t->compat != array_compat) {
+                       type_err(c, "error: %1 cannot provide length", prog, t, 0, NULL);
+                       return NULL;
+               }
+               if (!type_compat(type, Tnum, rules))
+                       type_err(c, "error: have %1 but need %2", prog,
+                                        Tnum, rules, type);
+               return Tnum;
+               break;
+
 ###### interp binode cases
        case Index: {
                mpz_t q;
@@ -2640,6 +2666,13 @@ with a const size by whether they are prepared at parse time or not.
                ltype = NULL;
                break;
        }
+       case Length: {
+               lleft = linterp_exec(c, b->left, &ltype);
+               mpq_set_ui(rv.num, ltype->array.size, 1);
+               ltype = NULL;
+               rvtype = Tnum;
+               break;
+       }
 
 #### Structs