From 9a2f89b1a0846809c6855814a8f72e8fa74f0e43 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sun, 3 Oct 2021 09:36:50 +1100 Subject: [PATCH] ocean: introduce prefix op for string->number conversion. 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 --- csrc/oceani-tests.mdc | 3 ++- csrc/oceani.mdc | 49 +++++++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/csrc/oceani-tests.mdc b/csrc/oceani-tests.mdc index e3739ed..5e91192 100644 --- a/csrc/oceani-tests.mdc +++ b/csrc/oceani-tests.mdc @@ -282,7 +282,8 @@ Here I break it into two parts, keeping the array code separate. ###### 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. diff --git a/csrc/oceani.mdc b/csrc/oceani.mdc index 520212b..d690b70 100644 --- a/csrc/oceani.mdc +++ b/csrc/oceani.mdc @@ -2772,9 +2772,14 @@ expression operator, and the `CMPop` non-terminal will match one of them. ### 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. @@ -2788,12 +2793,13 @@ should only insert brackets were needed for precedence. Times, Divide, Rem, Concat, Absolute, Negate, + StringConv, Bracket, ###### expr precedence $LEFT + - Eop $LEFT * / % ++ Top - $LEFT Uop + $LEFT Uop $ $TERM ( ) ###### expression grammar @@ -2834,6 +2840,7 @@ should only insert brackets were needed for precedence. Uop -> + ${ $0.op = Absolute; }$ | - ${ $0.op = Negate; }$ + | $ ${ $0.op = StringConv; }$ 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("("); - printf("+"); - print_exec(b->right, indent, bracket); - if (bracket) printf(")"); - break; case Negate: + case StringConv: 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; @@ -2906,6 +2914,15 @@ should only insert brackets were needed for precedence. 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); @@ -2963,6 +2980,14 @@ should only insert brackets were needed for precedence. 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 @@ -4248,11 +4273,13 @@ things which will likely grow as the languages grows. 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 + A := $Astr; B := $Bstr + /* When a variable is defined in both branches of an 'if', * and used afterwards, the variables are merged. */ -- 2.43.0