diff --git a/javatools/src/main/java/org/xvm/compiler/ast/CmpChainExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/CmpChainExpression.java index 493bd4fe2e8..6456c68342a 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/CmpChainExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/CmpChainExpression.java @@ -34,7 +34,6 @@ import org.xvm.asm.op.JumpLte; import org.xvm.asm.op.JumpNotEq; import org.xvm.asm.op.JumpNotNull; -import org.xvm.asm.op.JumpNull; import org.xvm.asm.op.Label; import org.xvm.asm.op.Move; @@ -365,14 +364,20 @@ public void generateConditionalJump( } if (constVal != null) { + Label labelFalse = fWhenTrue ? new Label("not_eq") : label; + boolean fNull = constVal.equals(pool().valNull()); for (int i = 0; i < cExprs; i++) { Argument arg = aArgs[i]; OpCondJump op = fNull - ? new JumpNull(arg, label) - : new JumpEq(typeCmp, arg, constVal, label); + ? new JumpNotNull(arg, labelFalse) + : new JumpNotEq(typeCmp, arg, constVal, labelFalse); code.add(op); } + if (fWhenTrue) { + code.add(new Jump(label)); + code.add(labelFalse); + } return; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java index 4e452ca66e8..3ac0f4b86fa 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java @@ -991,7 +991,7 @@ private boolean emitList(Context ctx, boolean fReachable, Code code, ErrorListen Register regTemp = code.createRegister(lval.getType()); code.add(new Var(regTemp)); code.add(new I_Get(argVal, index, regTemp)); - lvalVal.assign(regTemp, code, errs); + lval.assign(regTemp, code, errs); } } } else { diff --git a/lang/dsl/src/main/resources/templates/grammar.js.template b/lang/dsl/src/main/resources/templates/grammar.js.template index 0394c0ea219..50602c52c8b 100644 --- a/lang/dsl/src/main/resources/templates/grammar.js.template +++ b/lang/dsl/src/main/resources/templates/grammar.js.template @@ -157,15 +157,9 @@ module.exports = grammar({ // The 3-way variant above is not sufficient because some LR states // see only `parameter` and `tuple_assignment_element` as candidates. [$.parameter, $.tuple_assignment_element], - // After narrowing the bare-ident alt of `tuple_assignment_element` - // to `choice($.identifier, $.member_expression)` (no longer a full - // `_expression`), an LR state inside `for ((identifier ,...))` - // can interpret the leading identifier as any of: bare - // tuple_assignment_element, start-of-_expression, type_name, or - // qualified_type_name. GLR needs all four alive until the trailing - // `:` / `=` distinguishes for-iteration vs for-tuple-assignment vs - // bare expression. - [$.tuple_assignment_element, $._expression, $.type_name, $.qualified_type_name], + // In `for ((identifier, ...))`, retain tuple assignment, foreach target, + // expression, and type interpretations until `:` / `=` disambiguates. + [$.tuple_assignment_element, $._for_tuple_destructure_element, $._expression, $.type_name, $.qualified_type_name], // Conditional tuple conflicts [$.conditional_tuple_element, $._expression], @@ -1002,7 +996,7 @@ module.exports = grammar({ // for ((Type1 name1, Type2 name2) : iter) -- typed bindings // for ((_, _) : map) -- both wildcards (discard) // for ((_, Int index) : pairs) -- mixed - // Each element is either a wildcard `_` or `Type identifier`. + // Elements may declare a typed binding, discard a value, or assign an existing target. for_tuple_destructure: $ => seq( '(', $._for_tuple_destructure_element, @@ -1012,6 +1006,9 @@ module.exports = grammar({ _for_tuple_destructure_element: $ => choice( $.wildcard, seq($.type_expression, $.identifier), + $.identifier, + $.member_expression, + $.index_expression, ), // Supports conditional declaration: while (Element x := expr) diff --git a/lang/generated-examples/grammar.js b/lang/generated-examples/grammar.js index 9d8e760bf61..32b6e49251b 100644 --- a/lang/generated-examples/grammar.js +++ b/lang/generated-examples/grammar.js @@ -152,15 +152,9 @@ module.exports = grammar({ // The 3-way variant above is not sufficient because some LR states // see only `parameter` and `tuple_assignment_element` as candidates. [$.parameter, $.tuple_assignment_element], - // After narrowing the bare-ident alt of `tuple_assignment_element` - // to `choice($.identifier, $.member_expression)` (no longer a full - // `_expression`), an LR state inside `for ((identifier ,...))` - // can interpret the leading identifier as any of: bare - // tuple_assignment_element, start-of-_expression, type_name, or - // qualified_type_name. GLR needs all four alive until the trailing - // `:` / `=` distinguishes for-iteration vs for-tuple-assignment vs - // bare expression. - [$.tuple_assignment_element, $._expression, $.type_name, $.qualified_type_name], + // In `for ((identifier, ...))`, retain tuple assignment, foreach target, + // expression, and type interpretations until `:` / `=` disambiguates. + [$.tuple_assignment_element, $._for_tuple_destructure_element, $._expression, $.type_name, $.qualified_type_name], // Conditional tuple conflicts [$.conditional_tuple_element, $._expression], @@ -966,7 +960,7 @@ module.exports = grammar({ // for ((Type1 name1, Type2 name2) : iter) -- typed bindings // for ((_, _) : map) -- both wildcards (discard) // for ((_, Int index) : pairs) -- mixed - // Each element is either a wildcard `_` or `Type identifier`. + // Elements may declare a typed binding, discard a value, or assign an existing target. for_tuple_destructure: $ => seq( '(', $._for_tuple_destructure_element, @@ -976,6 +970,9 @@ module.exports = grammar({ _for_tuple_destructure_element: $ => choice( $.wildcard, seq($.type_expression, $.identifier), + $.identifier, + $.member_expression, + $.index_expression, ), // Supports conditional declaration: while (Element x := expr) diff --git a/manualTests/src/main/x/loop.x b/manualTests/src/main/x/loop.x index 0e1b5cd12d8..b5d5450460b 100644 --- a/manualTests/src/main/x/loop.x +++ b/manualTests/src/main/x/loop.x @@ -10,6 +10,7 @@ module TestLoops { testForEachCollection(); testForEachIterator(); testForEachSequence(); + testForEachTupleTargets(); testForEachConstRange(); testForEachEnumRange(); } @@ -78,6 +79,39 @@ module TestLoops { } } + void testForEachTupleTargets() { + console.print("\n** testForEachTupleTargets()"); + + TupleTargets target = new TupleTargets(); + Int sum = 0; + for ((target.first, target.second) : [(1, 2), (3, 4)]) { + sum += target.first + target.second; + } + assert sum == 10; + assert target.first == 3 && target.second == 4; + + Int[] indexed = new Int[2](_ -> 0); + sum = 0; + for ((indexed[0], indexed[1]) : [(1, 2), (3, 4)]) { + sum += indexed[0] + indexed[1]; + } + assert sum == 10; + assert indexed == [3, 4]; + + Int first = 0; + sum = 0; + for ((first, target.second) : [(5, 6), (7, 8)]) { + sum += first + target.second; + } + assert sum == 26; + assert first == 7 && target.second == 8; + } + + class TupleTargets { + Int first = 0; + Int second = 0; + } + void testForEachConstRange() { console.print("\n** testForEachConstRange()"); diff --git a/manualTests/src/main/x/misc.x b/manualTests/src/main/x/misc.x index 2cd66500b0f..97c2d71989c 100644 --- a/manualTests/src/main/x/misc.x +++ b/manualTests/src/main/x/misc.x @@ -5,6 +5,7 @@ module TestMisc { console.print("hello world!"); testBools(); + testEqualityChains(); testChars(); testInts(); testStrings(); @@ -92,6 +93,48 @@ module TestMisc { console.print("~b=" + ~b); } + void testEqualityChains() { + console.print("\n** testEqualityChains()"); + + for (Int left : [0, 3]) { + for (Int right : [0, 3]) { + Boolean expected = left == 3 && right == 3; + Boolean actual = left == 3 == right; + assert actual == expected; + + if (left == 3 == right) { + assert expected; + } else { + assert !expected; + } + if (!(left == 3 == right)) { + assert !expected; + } else { + assert expected; + } + } + } + + for (Int? left : [Null, 3]) { + for (Int? right : [Null, 3]) { + Boolean expected = left == Null && right == Null; + Boolean actual = left == Null == right; + assert actual == expected; + + if (left == Null == right) { + assert expected; + } else { + assert !expected; + } + if (!(left == Null == right)) { + assert !expected; + } else { + assert expected; + } + } + } + } + void testChars() { console.print("\n** testChars()");