Skip to content

Commit 4bcdc24

Browse files
committed
Fix x instanceof Foo && otherExpr
1 parent 5c53933 commit 4bcdc24

6 files changed

Lines changed: 226 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
source: crates/rascal/src/internal/as2/lexer/tests.rs
3+
expression: tokens
4+
input_file: samples/as2/op_precedence/instanceof.as
5+
---
6+
- kind:
7+
Keyword: Var
8+
span:
9+
start: 0
10+
end: 3
11+
raw: var
12+
- kind: Identifier
13+
span:
14+
start: 4
15+
end: 5
16+
raw: x
17+
- kind:
18+
Operator: Assign
19+
span:
20+
start: 6
21+
end: 7
22+
raw: "="
23+
- kind: Identifier
24+
span:
25+
start: 8
26+
end: 17
27+
raw: toCompare
28+
- kind:
29+
Keyword: InstanceOf
30+
span:
31+
start: 18
32+
end: 28
33+
raw: instanceof
34+
- kind: Identifier
35+
span:
36+
start: 29
37+
end: 32
38+
raw: Foo
39+
- kind:
40+
Operator: LogicalAnd
41+
span:
42+
start: 33
43+
end: 35
44+
raw: "&&"
45+
- kind: Identifier
46+
span:
47+
start: 36
48+
end: 45
49+
raw: toCompare
50+
- kind: Period
51+
span:
52+
start: 45
53+
end: 46
54+
raw: "."
55+
- kind: Identifier
56+
span:
57+
start: 46
58+
end: 47
59+
raw: x
60+
- kind:
61+
Operator: Equal
62+
span:
63+
start: 48
64+
end: 50
65+
raw: "=="
66+
- kind: Integer
67+
span:
68+
start: 51
69+
end: 54
70+
raw: "123"
71+
- kind: Semicolon
72+
span:
73+
start: 54
74+
end: 55
75+
raw: ;
76+
- kind: Newline
77+
span:
78+
start: 55
79+
end: 56
80+
raw: "\n"

crates/rascal/src/internal/as2/parser/expression.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,40 @@ mod tests {
13271327
)
13281328
}
13291329

1330+
#[test]
1331+
fn test_instanceof_and_equality_precedence() {
1332+
let tokens = build_tokens(&[
1333+
(TokenKind::Identifier, "toCompare"),
1334+
(TokenKind::Keyword(Keyword::InstanceOf), "instanceof"),
1335+
(TokenKind::Identifier, "Foo"),
1336+
(TokenKind::Operator(Operator::LogicalAnd), "&&"),
1337+
(TokenKind::Identifier, "toCompare"),
1338+
(TokenKind::Period, "."),
1339+
(TokenKind::Identifier, "x"),
1340+
(TokenKind::Operator(Operator::Equal), "=="),
1341+
(TokenKind::Integer, "123"),
1342+
]);
1343+
assert_eq!(
1344+
parse_expr(&tokens),
1345+
Ok(ex(ExprKind::BinaryOperator(
1346+
BinaryOperator::LogicalAnd,
1347+
Box::new(ex(ExprKind::BinaryOperator(
1348+
BinaryOperator::InstanceOf,
1349+
Box::new(id("toCompare")),
1350+
Box::new(id("Foo")),
1351+
))),
1352+
Box::new(ex(ExprKind::BinaryOperator(
1353+
BinaryOperator::Equal,
1354+
Box::new(ex(ExprKind::Field(
1355+
Box::new(id("toCompare")),
1356+
Box::new(s("x"))
1357+
))),
1358+
Box::new(ex(ExprKind::Constant(ConstantKind::Integer(123)))),
1359+
))),
1360+
)))
1361+
)
1362+
}
1363+
13301364
#[test]
13311365
fn test_parenthesis() {
13321366
let tokens = build_tokens(&[

crates/rascal/src/internal/as2/parser/operator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl BinaryOperator {
7777
| BinaryOperator::BitShiftLeftAssign
7878
| BinaryOperator::BitShiftRightAssign
7979
| BinaryOperator::BitShiftRightUnsignedAssign => OperatorPrecedence::Assignment,
80-
BinaryOperator::InstanceOf => OperatorPrecedence::Other,
80+
BinaryOperator::InstanceOf => OperatorPrecedence::Comparison,
8181
}
8282
}
8383
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
source: crates/rascal/src/program.rs
3+
expression: parsed
4+
input_file: samples/as2/op_precedence/instanceof.as
5+
---
6+
Ok:
7+
initial_script:
8+
- Declare:
9+
name:
10+
span:
11+
start: 4
12+
end: 5
13+
value: x
14+
value:
15+
span:
16+
start: 8
17+
end: 54
18+
value:
19+
BinaryOperator:
20+
- LogicalAnd
21+
- span:
22+
start: 8
23+
end: 32
24+
value:
25+
BinaryOperator:
26+
- InstanceOf
27+
- span:
28+
start: 8
29+
end: 17
30+
value:
31+
Constant:
32+
Identifier: toCompare
33+
- span:
34+
start: 29
35+
end: 32
36+
value:
37+
Constant:
38+
Identifier: Foo
39+
- span:
40+
start: 45
41+
end: 54
42+
value:
43+
BinaryOperator:
44+
- Equal
45+
- span:
46+
start: 45
47+
end: 47
48+
value:
49+
Field:
50+
- span:
51+
start: 36
52+
end: 45
53+
value:
54+
Constant:
55+
Identifier: toCompare
56+
- span:
57+
start: 46
58+
end: 47
59+
value:
60+
Constant:
61+
String: x
62+
- span:
63+
start: 51
64+
end: 54
65+
value:
66+
Constant:
67+
Integer: 123
68+
type_name: ~
69+
interfaces: []
70+
classes: []
71+
custom_pcodes: []
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
source: crates/rascal/src/tests.rs
3+
expression: result
4+
input_file: samples/as2/op_precedence/instanceof.as
5+
---
6+
initializer:
7+
actions:
8+
- ConstantPool:
9+
- x
10+
- toCompare
11+
- Foo
12+
- Push:
13+
- Constant: 0
14+
- Constant: 1
15+
- GetVariable
16+
- Push:
17+
- Constant: 2
18+
- GetVariable
19+
- InstanceOf
20+
- PushDuplicate
21+
- Not
22+
- If: loc0000
23+
- Pop
24+
- Push:
25+
- Constant: 1
26+
- GetVariable
27+
- Push:
28+
- Constant: 0
29+
- GetMember
30+
- Push:
31+
- Integer: 123
32+
- Equals2
33+
- DefineLocal
34+
label_positions:
35+
loc0000: 16
36+
extra_modules: []
37+
compile_options:
38+
swf_version: 15
39+
custom_pcodes: []
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var x = toCompare instanceof Foo && toCompare.x == 123;

0 commit comments

Comments
 (0)