]> ocean-lang.org Git - ocean/commitdiff
oceani: fix merging of conditionally-scoped variables.
authorNeilBrown <neil@brown.name>
Sat, 18 May 2019 13:51:24 +0000 (23:51 +1000)
committerNeilBrown <neil@brown.name>
Sat, 18 May 2019 13:56:43 +0000 (23:56 +1000)
The problem here was that the list seen in ->in_scope
includes more than just what is currently in-scope.
It also contains things that have been replaced by new instances
of the name.
These can be detected a they aren't the first variable listed
under their name any more.

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

index 42505ce0fbe6957fbb9a89f0a9c7142745f4563d..102c957b0b4e459327e1418aa1f7e4b33ba3dde8 100644 (file)
@@ -163,6 +163,27 @@ Now some contants
        are there 5 ?
        3.14159 I like Pie but The cake is a lie
 
+Test merging of variables from multiple cases
+
+###### test list
+       oceani_tests += varmerge
+
+###### test: varmerge
+
+       program:
+               for i:=0; then i=i+1; while i < 5:
+                       switch i
+                               case 0: num:="zero"
+                               case 1: num:="one"
+                               case 2: num:="two"
+                               case 3: num:="three"
+                               else:   num:="many"
+                       print num,", ",
+               print
+
+###### output: varmerge
+       zero , one , two , three , many , 
+
 ## Conditions and Loops
 
 Now we need to test if/else and some different loops
index 1cf84edc20206976ad5d171afe8274ff45727f91..6b43096b13cd2e1fff4cf3df214d35b9db6e02d3 100644 (file)
@@ -1172,7 +1172,7 @@ all pending-scope variables become conditionally scoped.
                switch (v ? v->scope : OutScope) {
                case OutScope:
                case PendingScope:
-                       /* Signal an error - once that is possible */
+                       /* Caller will report the error */
                        return NULL;
                case CondScope:
                        /* All CondScope variables of this name need to be merged
@@ -1193,14 +1193,14 @@ all pending-scope variables become conditionally scoped.
 
        static void var_block_close(struct parse_context *c, enum closetype ct)
        {
-               /* close of all variables that are in_scope */
+               /* Close off all variables that are in_scope */
                struct variable *v, **vp, *v2;
 
                scope_pop(c);
                for (vp = &c->in_scope;
                     v = *vp, v && v->depth > c->scope_depth && v->min_depth > c->scope_depth;
                     ) {
-                       switch (ct) {
+                       if (v->name->var == v) switch (ct) {
                        case CloseElse:
                        case CloseParallel: /* handle PendingScope */
                                switch(v->scope) {
@@ -1261,7 +1261,7 @@ all pending-scope variables become conditionally scoped.
                                }
                                break;
                        }
-                       if (v->scope == OutScope)
+                       if (v->scope == OutScope || v->name->var != v)
                                *vp = v->in_scope;
                        else
                                vp = &v->in_scope;