Skip to content

Commit

Permalink
Merge pull request #589 from KashParty/master
Browse files Browse the repository at this point in the history
Implement the comma operator
  • Loading branch information
Paul Lancaster authored Jul 29, 2020
2 parents 356ce1a + 2df2db3 commit 9a3b69f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions boa/src/exec/operator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ impl Executable for BinOp {
}
_ => Ok(Value::undefined()),
},
op::BinOp::Comma => {
self.lhs().run(interpreter)?;
Ok(self.rhs().run(interpreter)?)
}
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions boa/src/exec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,3 +1171,22 @@ fn not_a_function() {
"\"TypeError: not a function\""
);
}

#[test]
fn comma_operator() {
let scenario = r#"
var a, b;
b = 10;
a = (b++, b);
a
"#;
assert_eq!(&exec(scenario), "11");

let scenario = r#"
var a, b;
b = 10;
a = (b += 5, b /= 3, b - 3);
a
"#;
assert_eq!(&exec(scenario), "2");
}
4 changes: 4 additions & 0 deletions boa/src/syntax/ast/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,9 @@ pub enum BinOp {
///
/// see: [`AssignOp`](enum.AssignOp.html).
Assign(AssignOp),

/// Comma operation.
Comma,
}

impl From<NumOp> for BinOp {
Expand Down Expand Up @@ -748,6 +751,7 @@ impl Display for BinOp {
Self::Comp(ref op) => op.to_string(),
Self::Log(ref op) => op.to_string(),
Self::Assign(ref op) => op.to_string(),
Self::Comma => ",".to_string(),
}
)
}
Expand Down
1 change: 1 addition & 0 deletions boa/src/syntax/ast/punctuator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl Punctuator {
Self::LeftSh => Some(BinOp::Bit(BitOp::Shl)),
Self::RightSh => Some(BinOp::Bit(BitOp::Shr)),
Self::URightSh => Some(BinOp::Bit(BitOp::UShr)),
Self::Comma => Some(BinOp::Comma),
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion boa/src/syntax/parser/expression/assignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl TokenParser for AssignmentExpression {
TokenKind::Punctuator(Punctuator::Assign) => {
lhs = Assign::new(lhs, self.parse(cursor)?).into();
}
TokenKind::Punctuator(p) if p.as_binop().is_some() => {
TokenKind::Punctuator(p) if p.as_binop().is_some() && p != Punctuator::Comma => {
let expr = self.parse(cursor)?;
let binop = p.as_binop().expect("binop disappeared");
lhs = BinOp::new(binop, lhs, expr).into();
Expand Down

0 comments on commit 9a3b69f

Please sign in to comment.