]> ocean-lang.org Git - ocean/commitdiff
ocean: introduce prefix op for string->number conversion.
authorNeilBrown <neil@brown.name>
Sat, 2 Oct 2021 22:36:50 +0000 (09:36 +1100)
committerNeilBrown <neil@brown.name>
Sat, 2 Oct 2021 22:36:50 +0000 (09:36 +1100)
Rather than having magic conversion of command line args to numbers as
needed, introduce '$' as a prefix op to to the conversion.
This is a step towards changing 'program' to be a 'main' function.

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

index e3739ed940802dd324eebc14fec1eefdc03c8be4..5e911925fd39d5a80c3eec0edc0c37614a486666 100644 (file)
@@ -282,7 +282,8 @@ Here I break it into two parts, keeping the array code separate.
 
 ###### test: sayhello
 
 
 ###### test: sayhello
 
-       program A B astr bbool:
+       program As Bs astr bbool:
+               A := $As; B := $Bs
                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.
                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.
index 520212be6d144365a586638bf695098689460058..d690b70fb5c795ee481483db747f794f28850381 100644 (file)
@@ -2772,9 +2772,14 @@ expression operator, and the `CMPop` non-terminal will match one of them.
 
 ### Expressions: The rest
 
 
 ### Expressions: The rest
 
-The remaining expressions with the highest precedence are arithmetic and
-string concatenation.  String concatenation (`++`) has the same
-precedence as multiplication and division, but lower than the uniary.
+The remaining expressions with the highest precedence are arithmetic,
+string concatenation, and string conversion.  String concatenation
+(`++`) has the same precedence as multiplication and division, but lower
+than the uniary.
+
+String conversion is a temporary feature until I get a better type
+system.  `$` is a prefix operator which expects a string and returns
+a number.
 
 `+` and `-` are both infix and prefix operations (where they are
 absolute value and negation).  These have different operator names.
 
 `+` and `-` are both infix and prefix operations (where they are
 absolute value and negation).  These have different operator names.
@@ -2788,12 +2793,13 @@ should only insert brackets were needed for precedence.
        Times, Divide, Rem,
        Concat,
        Absolute, Negate,
        Times, Divide, Rem,
        Concat,
        Absolute, Negate,
+       StringConv,
        Bracket,
 
 ###### expr precedence
        $LEFT + - Eop
        $LEFT * / % ++ Top
        Bracket,
 
 ###### expr precedence
        $LEFT + - Eop
        $LEFT * / % ++ Top
-       $LEFT Uop
+       $LEFT Uop $
        $TERM ( )
 
 ###### expression grammar
        $TERM ( )
 
 ###### expression grammar
@@ -2834,6 +2840,7 @@ should only insert brackets were needed for precedence.
 
        Uop ->    + ${ $0.op = Absolute; }$
                | - ${ $0.op = Negate; }$
 
        Uop ->    + ${ $0.op = Absolute; }$
                | - ${ $0.op = Negate; }$
+               | $ ${ $0.op = StringConv; }$
 
        Top ->    * ${ $0.op = Times; }$
                | / ${ $0.op = Divide; }$
 
        Top ->    * ${ $0.op = Times; }$
                | / ${ $0.op = Divide; }$
@@ -2862,14 +2869,15 @@ should only insert brackets were needed for precedence.
                if (bracket) printf(")");
                break;
        case Absolute:
                if (bracket) printf(")");
                break;
        case Absolute:
-               if (bracket) printf("(");
-               printf("+");
-               print_exec(b->right, indent, bracket);
-               if (bracket) printf(")");
-               break;
        case Negate:
        case Negate:
+       case StringConv:
                if (bracket) printf("(");
                if (bracket) printf("(");
-               printf("-");
+               switch (b->op) {
+               case Absolute:   fputs("+", stdout); break;
+               case Negate:     fputs("-", stdout); break;
+               case StringConv: fputs("$", stdout); break;
+               default: abort();       // NOTEST
+               }                       // NOTEST
                print_exec(b->right, indent, bracket);
                if (bracket) printf(")");
                break;
                print_exec(b->right, indent, bracket);
                if (bracket) printf(")");
                break;
@@ -2906,6 +2914,15 @@ should only insert brackets were needed for precedence.
                                   Tstr, rules, type);
                return Tstr;
 
                                   Tstr, rules, type);
                return Tstr;
 
+       case StringConv:
+               /* op must be string, result is number */
+               propagate_types(b->left, c, ok, Tstr, 0);
+               if (!type_compat(type, Tnum, 0))
+                       type_err(c,
+                         "error: Can only convert string to number, not %1",
+                               prog, type, 0, NULL);
+               return Tnum;
+
        case Bracket:
                return propagate_types(b->right, c, ok, type, 0);
 
        case Bracket:
                return propagate_types(b->right, c, ok, type, 0);
 
@@ -2963,6 +2980,14 @@ should only insert brackets were needed for precedence.
                rvtype = Tstr;
                rv.str = text_join(left.str, right.str);
                break;
                rvtype = Tstr;
                rv.str = text_join(left.str, right.str);
                break;
+       case StringConv:
+               right = interp_exec(b->right, &rvtype);
+               rtype = Tstr;
+               rvtype = Tnum;
+               char *str = strndup(right.str.txt, right.str.len);
+               parse_value(rvtype, str, &rv);
+               free(str);
+               break;
 
 ###### value functions
 
 
 ###### value functions
 
@@ -4248,11 +4273,13 @@ things which will likely grow as the languages grows.
                name:string
                alive:Boolean
 
                name:string
                alive:Boolean
 
-       program A B:
+       program Astr Bstr:
                print "Hello World, what lovely oceans you have!"
                print "Are there", five, "?"
                print pi, pie, "but", cake
 
                print "Hello World, what lovely oceans you have!"
                print "Are there", five, "?"
                print pi, pie, "but", cake
 
+               A := $Astr; B := $Bstr
+
                /* When a variable is defined in both branches of an 'if',
                 * and used afterwards, the variables are merged.
                 */
                /* When a variable is defined in both branches of an 'if',
                 * and used afterwards, the variables are merged.
                 */