1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:18:12 +00:00

LibJS: Parse binary bitwise operators

The Lexer and AST already have all the functionality required in place,
so this is just updating Parser::match_secondary_expression() and
Parser::parse_expression() to handle TokenType::{Ampersand,Pipe,Caret},
as well as adding some tests.
This commit is contained in:
Linus Groh 2020-04-03 13:02:31 +01:00 committed by Andreas Kling
parent 0622181d1f
commit a58640ce6b
2 changed files with 76 additions and 1 deletions

View file

@ -490,6 +490,15 @@ NonnullRefPtr<Expression> Parser::parse_secondary_expression(NonnullRefPtr<Expre
case TokenType::Instanceof:
consume();
return create_ast_node<BinaryExpression>(BinaryOp::InstanceOf, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::Ampersand:
consume();
return create_ast_node<BinaryExpression>(BinaryOp::BitwiseAnd, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::Pipe:
consume();
return create_ast_node<BinaryExpression>(BinaryOp::BitwiseOr, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::Caret:
consume();
return create_ast_node<BinaryExpression>(BinaryOp::BitwiseXor, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::ParenOpen:
return parse_call_expression(move(lhs));
case TokenType::Equals:
@ -877,7 +886,10 @@ bool Parser::match_secondary_expression() const
|| type == TokenType::PlusPlus
|| type == TokenType::MinusMinus
|| type == TokenType::Instanceof
|| type == TokenType::QuestionMark;
|| type == TokenType::QuestionMark
|| type == TokenType::Ampersand
|| type == TokenType::Pipe
|| type == TokenType::Caret;
}
bool Parser::match_statement() const

View file

@ -0,0 +1,63 @@
function assert(x) { if (!x) throw 1; }
try {
assert((0 | 0) === 0);
assert((0 | 1) === 1);
assert((0 | 2) === 2);
assert((0 | 3) === 3);
assert((0 | 4) === 4);
assert((0 | 5) === 5);
assert((1 | 0) === 1);
assert((1 | 1) === 1);
assert((1 | 2) === 3);
assert((1 | 3) === 3);
assert((1 | 4) === 5);
assert((1 | 5) === 5);
assert((2 | 0) === 2);
assert((2 | 1) === 3);
assert((2 | 2) === 2);
assert((2 | 3) === 3);
assert((2 | 4) === 6);
assert((2 | 5) === 7);
assert((3 | 0) === 3);
assert((3 | 1) === 3);
assert((3 | 2) === 3);
assert((3 | 3) === 3);
assert((3 | 4) === 7);
assert((3 | 5) === 7);
assert((4 | 0) === 4);
assert((4 | 1) === 5);
assert((4 | 2) === 6);
assert((4 | 3) === 7);
assert((4 | 4) === 4);
assert((4 | 5) === 5);
assert((5 | 0) === 5);
assert((5 | 1) === 5);
assert((5 | 2) === 7);
assert((5 | 3) === 7);
assert((5 | 4) === 5);
assert((5 | 5) === 5);
var x = 3;
var y = 7;
assert(("42" | 6) === 46);
assert((x | y) === 7);
assert((x | [[[[12]]]]) === 15);
// FIXME: These should all return 0
// assert((undefined | y) === 7);
// assert(("a" | "b") === 0);
// assert((null | null) === 0);
// assert((undefined | undefined) === 0);
// assert((NaN | NaN) === 0);
// assert((Infinity | Infinity) === 0);
// assert((-Infinity | Infinity) === 0);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}