Browse Source

Merge pull request #589 from KashParty/master

Implement the comma operator
pull/599/head
Paul Lancaster 4 years ago committed by GitHub
parent
commit
9a3b69ff9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      boa/src/exec/operator/mod.rs
  2. 19
      boa/src/exec/tests.rs
  3. 4
      boa/src/syntax/ast/op.rs
  4. 1
      boa/src/syntax/ast/punctuator.rs
  5. 2
      boa/src/syntax/parser/expression/assignment/mod.rs

4
boa/src/exec/operator/mod.rs

@ -140,6 +140,10 @@ impl Executable for BinOp {
} }
_ => Ok(Value::undefined()), _ => Ok(Value::undefined()),
}, },
op::BinOp::Comma => {
self.lhs().run(interpreter)?;
Ok(self.rhs().run(interpreter)?)
}
} }
} }
} }

19
boa/src/exec/tests.rs

@ -1171,3 +1171,22 @@ fn not_a_function() {
"\"TypeError: 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
boa/src/syntax/ast/op.rs

@ -705,6 +705,9 @@ pub enum BinOp {
/// ///
/// see: [`AssignOp`](enum.AssignOp.html). /// see: [`AssignOp`](enum.AssignOp.html).
Assign(AssignOp), Assign(AssignOp),
/// Comma operation.
Comma,
} }
impl From<NumOp> for BinOp { impl From<NumOp> for BinOp {
@ -748,6 +751,7 @@ impl Display for BinOp {
Self::Comp(ref op) => op.to_string(), Self::Comp(ref op) => op.to_string(),
Self::Log(ref op) => op.to_string(), Self::Log(ref op) => op.to_string(),
Self::Assign(ref op) => op.to_string(), Self::Assign(ref op) => op.to_string(),
Self::Comma => ",".to_string(),
} }
) )
} }

1
boa/src/syntax/ast/punctuator.rs

@ -167,6 +167,7 @@ impl Punctuator {
Self::LeftSh => Some(BinOp::Bit(BitOp::Shl)), Self::LeftSh => Some(BinOp::Bit(BitOp::Shl)),
Self::RightSh => Some(BinOp::Bit(BitOp::Shr)), Self::RightSh => Some(BinOp::Bit(BitOp::Shr)),
Self::URightSh => Some(BinOp::Bit(BitOp::UShr)), Self::URightSh => Some(BinOp::Bit(BitOp::UShr)),
Self::Comma => Some(BinOp::Comma),
_ => None, _ => None,
} }
} }

2
boa/src/syntax/parser/expression/assignment/mod.rs

@ -116,7 +116,7 @@ impl TokenParser for AssignmentExpression {
TokenKind::Punctuator(Punctuator::Assign) => { TokenKind::Punctuator(Punctuator::Assign) => {
lhs = Assign::new(lhs, self.parse(cursor)?).into(); 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 expr = self.parse(cursor)?;
let binop = p.as_binop().expect("binop disappeared"); let binop = p.as_binop().expect("binop disappeared");
lhs = BinOp::new(binop, lhs, expr).into(); lhs = BinOp::new(binop, lhs, expr).into();

Loading…
Cancel
Save