Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 7 additions & 10 deletions lang/dsl/src/main/resources/templates/grammar.js.template
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
17 changes: 7 additions & 10 deletions lang/generated-examples/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions manualTests/src/main/x/loop.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module TestLoops {
testForEachCollection();
testForEachIterator();
testForEachSequence();
testForEachTupleTargets();
testForEachConstRange();
testForEachEnumRange();
}
Expand Down Expand Up @@ -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()");

Expand Down
43 changes: 43 additions & 0 deletions manualTests/src/main/x/misc.x
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module TestMisc {
console.print("hello world!");

testBools();
testEqualityChains();
testChars();
testInts();
testStrings();
Expand Down Expand Up @@ -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()");

Expand Down
Loading