mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 17:10:23 +00:00
LibJS: Implement exponentiation (** operator)
This commit is contained in:
parent
eafd3dbaf8
commit
0403845d3e
Notes:
sideshowbarker
2024-07-19 07:53:39 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/0403845d3ed Pull-request: https://github.com/SerenityOS/serenity/pull/1627
7 changed files with 48 additions and 1 deletions
|
@ -283,6 +283,8 @@ Value BinaryExpression::execute(Interpreter& interpreter) const
|
|||
return div(lhs_result, rhs_result);
|
||||
case BinaryOp::Modulo:
|
||||
return mod(lhs_result, rhs_result);
|
||||
case BinaryOp::Exponentiation:
|
||||
return exp(lhs_result, rhs_result);
|
||||
case BinaryOp::TypedEquals:
|
||||
return typed_eq(lhs_result, rhs_result);
|
||||
case BinaryOp::TypedInequals:
|
||||
|
@ -421,6 +423,9 @@ void BinaryExpression::dump(int indent) const
|
|||
case BinaryOp::Modulo:
|
||||
op_string = "%";
|
||||
break;
|
||||
case BinaryOp::Exponentiation:
|
||||
op_string = "**";
|
||||
break;
|
||||
case BinaryOp::TypedEquals:
|
||||
op_string = "===";
|
||||
break;
|
||||
|
|
|
@ -314,6 +314,7 @@ enum class BinaryOp {
|
|||
Multiplication,
|
||||
Division,
|
||||
Modulo,
|
||||
Exponentiation,
|
||||
TypedEquals,
|
||||
TypedInequals,
|
||||
AbstractEquals,
|
||||
|
|
|
@ -470,6 +470,9 @@ NonnullRefPtr<Expression> Parser::parse_secondary_expression(NonnullRefPtr<Expre
|
|||
case TokenType::Percent:
|
||||
consume();
|
||||
return create_ast_node<BinaryExpression>(BinaryOp::Modulo, move(lhs), parse_expression(min_precedence, associativity));
|
||||
case TokenType::DoubleAsterisk:
|
||||
consume();
|
||||
return create_ast_node<BinaryExpression>(BinaryOp::Exponentiation, move(lhs), parse_expression(min_precedence, associativity));
|
||||
case TokenType::GreaterThan:
|
||||
consume();
|
||||
return create_ast_node<BinaryExpression>(BinaryOp::GreaterThan, move(lhs), parse_expression(min_precedence, associativity));
|
||||
|
@ -905,6 +908,7 @@ bool Parser::match_secondary_expression() const
|
|||
|| type == TokenType::Slash
|
||||
|| type == TokenType::SlashEquals
|
||||
|| type == TokenType::Percent
|
||||
|| type == TokenType::DoubleAsterisk
|
||||
|| type == TokenType::Equals
|
||||
|| type == TokenType::EqualsEqualsEquals
|
||||
|| type == TokenType::ExclamationMarkEqualsEquals
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
#include <LibJS/Runtime/PrimitiveString.h>
|
||||
#include <LibJS/Runtime/StringObject.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
#include <math.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
|
@ -268,6 +269,11 @@ Value mod(Value lhs, Value rhs)
|
|||
return Value(index - trunc * period);
|
||||
}
|
||||
|
||||
Value exp(Value lhs, Value rhs)
|
||||
{
|
||||
return Value(pow(lhs.to_number().as_double(), rhs.to_number().as_double()));
|
||||
}
|
||||
|
||||
Value typed_eq(Value lhs, Value rhs)
|
||||
{
|
||||
if (rhs.type() != lhs.type())
|
||||
|
|
|
@ -196,6 +196,7 @@ Value sub(Value lhs, Value rhs);
|
|||
Value mul(Value lhs, Value rhs);
|
||||
Value div(Value lhs, Value rhs);
|
||||
Value mod(Value lhs, Value rhs);
|
||||
Value exp(Value lhs, Value rhs);
|
||||
Value eq(Value lhs, Value rhs);
|
||||
Value typed_eq(Value lhs, Value rhs);
|
||||
Value instance_of(Value lhs, Value rhs);
|
||||
|
|
30
Libraries/LibJS/Tests/exponentiation-basic.js
Normal file
30
Libraries/LibJS/Tests/exponentiation-basic.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
function assert(x) { if (!x) throw 1; }
|
||||
|
||||
try {
|
||||
assert(2 ** 0 === 1);
|
||||
assert(2 ** 1 === 2);
|
||||
assert(2 ** 2 === 4);
|
||||
assert(2 ** 3 === 8);
|
||||
assert(2 ** -3 === 0.125);
|
||||
assert(3 ** 2 === 9);
|
||||
assert(0 ** 0 === 1);
|
||||
assert(2 ** 3 ** 2 === 512);
|
||||
assert(2 ** (3 ** 2) === 512);
|
||||
assert((2 ** 3) ** 2 === 64);
|
||||
assert("2" ** "3" === 8);
|
||||
assert("" ** [] === 1);
|
||||
assert([] ** null === 1);
|
||||
assert(null ** null === 1);
|
||||
assert(undefined ** null === 1);
|
||||
assert(isNaN(NaN ** 2));
|
||||
assert(isNaN(2 ** NaN));
|
||||
assert(isNaN(undefined ** 2));
|
||||
assert(isNaN(2 ** undefined));
|
||||
assert(isNaN(null ** undefined));
|
||||
assert(isNaN(2 ** "foo"));
|
||||
assert(isNaN("foo" ** 2));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -3,7 +3,7 @@ try {
|
|||
assert(10.5 % 2.5 === 0.5);
|
||||
assert(-0.99 % 0.99 === -0);
|
||||
|
||||
// Examples form MDN:
|
||||
// Examples from MDN:
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
|
||||
assert(12 % 5 === 2);
|
||||
assert(-1 % 2 === -1);
|
||||
|
|
Loading…
Reference in a new issue