Skip to content

Commit

Permalink
Fixed assign operator precedence (#311)
Browse files Browse the repository at this point in the history
Co-authored-by: Iban Eguia <[email protected]>
  • Loading branch information
HalidOdat and Razican authored Apr 13, 2020
1 parent 9c638cc commit 505df91
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
12 changes: 11 additions & 1 deletion boa/src/exec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn spread_with_arguments() {
function foo(...a) {
return arguments;
}
var result = foo(...a);
"#;
forward(&mut engine, scenario);
Expand Down Expand Up @@ -248,6 +248,16 @@ fn test_short_circuit_evaluation() {
assert_eq!(exec(short_circuit_eval), String::from("1"));
}

#[test]
fn assign_operator_precedence() {
let src = r#"
let a = 1;
a = a + 1;
a
"#;
assert_eq!(exec(src), String::from("2"));
}

#[test]
fn test_do_while_loop() {
let simple_one = r#"
Expand Down
8 changes: 0 additions & 8 deletions boa/src/syntax/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ macro_rules! expression { ( $name:ident, $lower:ident, [ $( $op:path ),* ] ) =>
let mut lhs = self. $lower ()?;
while let Some(tok) = self.peek_skip_lineterminator() {
match tok.kind {
// Parse assign expression
TokenKind::Punctuator(ref op) if op == &Punctuator::Assign => {
let _ = self.next_skip_lineterminator().expect("token disappeared");
lhs = Node::Assign(
Box::new(lhs),
Box::new(self. $lower ()?)
)
}
TokenKind::Punctuator(ref op) if $( op == &$op )||* => {
let _ = self.next_skip_lineterminator().expect("token disappeared");
lhs = Node::BinOp(
Expand Down
18 changes: 17 additions & 1 deletion boa/src/syntax/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ fn check_function_declarations() {
);
}

// Should be parsed as `new Class().method()` instead of `new (Class().method())`
#[test]
fn check_do_while() {
check_parser(
Expand Down Expand Up @@ -742,5 +743,20 @@ fn check_construct_call_precedence() {
)),
vec![],
)],
)
);
}

#[test]
fn assing_operator_precedence() {
check_parser(
"a = a + 1",
&[Node::Assign(
Box::new(Node::Local(String::from("a"))),
Box::new(create_bin_op(
BinOp::Num(NumOp::Add),
Node::Local(String::from("a")),
Node::Const(Const::Num(1.0)),
)),
)],
);
}

0 comments on commit 505df91

Please sign in to comment.