Implement exponentiation operation. (#3692)

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2020-04-27 11:43:35 +02:00
committed by GitHub
parent daeee77d63
commit 4e8dac8ce1
18 changed files with 319 additions and 32 deletions
+2
View File
@@ -396,6 +396,8 @@
DIV) \
CBC_BINARY_OPERATION (CBC_MODULO, \
MOD) \
CBC_BINARY_OPERATION (CBC_EXPONENTIATION, \
EXP) \
\
/* Unary lvalue opcodes. */ \
CBC_OPCODE (CBC_DELETE_PUSH_RESULT, CBC_NO_FLAG, -1, \
+97 -6
View File
@@ -1401,7 +1401,7 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
* @param type1 type
*/
#define LEXER_TYPE_A_TOKEN(char1, type1) \
case (uint8_t) (char1) : \
case (uint8_t) (char1): \
{ \
context_p->token.type = (type1); \
length = 1; \
@@ -1417,7 +1417,7 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
* @param type2 type of the second character
*/
#define LEXER_TYPE_B_TOKEN(char1, type1, char2, type2) \
case (uint8_t) (char1) : \
case (uint8_t) (char1): \
{ \
if (length >= 2 && context_p->source_p[1] == (uint8_t) (char2)) \
{ \
@@ -1442,7 +1442,7 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
* @param type3 type of the third character
*/
#define LEXER_TYPE_C_TOKEN(char1, type1, char2, type2, char3, type3) \
case (uint8_t) (char1) : \
case (uint8_t) (char1): \
{ \
if (length >= 2) \
{ \
@@ -1671,8 +1671,39 @@ lexer_next_token (parser_context_t *context_p) /**< context */
LEXER_TYPE_C_TOKEN (LIT_CHAR_MINUS, LEXER_SUBTRACT, LIT_CHAR_EQUALS,
LEXER_ASSIGN_SUBTRACT, LIT_CHAR_MINUS, LEXER_DECREASE)
LEXER_TYPE_B_TOKEN (LIT_CHAR_ASTERISK, LEXER_MULTIPLY, LIT_CHAR_EQUALS,
LEXER_ASSIGN_MULTIPLY)
case (uint8_t) LIT_CHAR_ASTERISK:
{
if (length >= 2)
{
if (context_p->source_p[1] == (uint8_t) LIT_CHAR_EQUALS)
{
context_p->token.type = LEXER_ASSIGN_MULTIPLY;
length = 2;
break;
}
#if ENABLED (JERRY_ES2015)
if (context_p->source_p[1] == (uint8_t) LIT_CHAR_ASTERISK)
{
if (length >= 3 && context_p->source_p[2] == (uint8_t) LIT_CHAR_EQUALS)
{
context_p->token.type = LEXER_ASSIGN_EXPONENTIATION;
length = 3;
break;
}
context_p->token.type = LEXER_EXPONENTIATION;
length = 2;
break;
}
#endif /* ENABLED (JERRY_ES2015) */
}
context_p->token.type = LEXER_MULTIPLY;
length = 1;
break;
}
LEXER_TYPE_B_TOKEN (LIT_CHAR_SLASH, LEXER_DIVIDE, LIT_CHAR_EQUALS,
LEXER_ASSIGN_DIVIDE)
LEXER_TYPE_B_TOKEN (LIT_CHAR_PERCENT, LEXER_MODULO, LIT_CHAR_EQUALS,
@@ -1763,7 +1794,7 @@ lexer_check_next_characters (parser_context_t *context_p, /**< context */
* @return consumed character
*/
inline uint8_t JERRY_ATTR_ALWAYS_INLINE
lexer_consume_next_character (parser_context_t *context_p)
lexer_consume_next_character (parser_context_t *context_p) /**< context */
{
JERRY_ASSERT (context_p->source_p < context_p->source_end_p);
@@ -1773,6 +1804,59 @@ lexer_consume_next_character (parser_context_t *context_p)
return *context_p->source_p++;
} /* lexer_consume_next_character */
/**
* Checks whether the next character can be the start of a post primary expression
*
* Note:
* the result is not precise, but this inprecise result
* has no side effects for negating number literals
*
* @return true if the next character can be the start of a post primary expression
*/
bool
lexer_check_post_primary_exp (parser_context_t *context_p) /**< context */
{
if (!(context_p->token.flags & LEXER_NO_SKIP_SPACES))
{
lexer_skip_spaces (context_p);
context_p->token.flags = (uint8_t) (context_p->token.flags | LEXER_NO_SKIP_SPACES);
}
if (context_p->source_p >= context_p->source_end_p)
{
return false;
}
switch (context_p->source_p[0])
{
case LIT_CHAR_DOT:
case LIT_CHAR_LEFT_PAREN:
case LIT_CHAR_LEFT_SQUARE:
#if ENABLED (JERRY_ES2015)
case LIT_CHAR_GRAVE_ACCENT:
#endif /* ENABLED (JERRY_ES2015) */
{
return true;
}
case LIT_CHAR_PLUS:
case LIT_CHAR_MINUS:
{
return (!(context_p->token.flags & LEXER_WAS_NEWLINE)
&& context_p->source_p + 1 < context_p->source_end_p
&& context_p->source_p[1] == context_p->source_p[0]);
}
#if ENABLED (JERRY_ES2015)
case LIT_CHAR_ASTERISK:
{
return (context_p->source_p + 1 < context_p->source_end_p
&& context_p->source_p[1] == (uint8_t) LIT_CHAR_ASTERISK);
}
#endif /* ENABLED (JERRY_ES2015) */
}
return false;
} /* lexer_check_post_primary_exp */
#if ENABLED (JERRY_ES2015)
/**
@@ -3295,10 +3379,17 @@ lexer_convert_binary_lvalue_token_to_binary (uint8_t token) /**< binary lvalue t
JERRY_ASSERT (LEXER_IS_BINARY_LVALUE_TOKEN (token));
JERRY_ASSERT (token != LEXER_ASSIGN);
#if ENABLED (JERRY_ES2015)
if (token <= LEXER_ASSIGN_EXPONENTIATION)
{
return (uint8_t) (LEXER_ADD + (token - LEXER_ASSIGN_ADD));
}
#else /* !ENABLED (JERRY_ES2015) */
if (token <= LEXER_ASSIGN_MODULO)
{
return (uint8_t) (LEXER_ADD + (token - LEXER_ASSIGN_ADD));
}
#endif /* ENABLED (JERRY_ES2015) */
if (token <= LEXER_ASSIGN_UNS_RIGHT_SHIFT)
{
+13
View File
@@ -66,10 +66,17 @@ typedef enum
* IMPORTANT: update CBC_BINARY_OP_TOKEN_TO_OPCODE,
* CBC_BINARY_LVALUE_OP_TOKEN_TO_OPCODE and
* parser_binary_precedence_table after changes. */
#if ENABLED (JERRY_ES2015)
#define LEXER_IS_BINARY_OP_TOKEN(token_type) \
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_EXPONENTIATION)
#else /* !ENABLED (JERRY_ES2015) */
#define LEXER_IS_BINARY_OP_TOKEN(token_type) \
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_MODULO)
#endif /* ENABLED (JERRY_ES2015) */
#define LEXER_IS_BINARY_LVALUE_TOKEN(token_type) \
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_ASSIGN_BIT_XOR)
#define LEXER_FIRST_BINARY_OP LEXER_ASSIGN
LEXER_ASSIGN, /**< "=" (prec: 3) */
@@ -78,6 +85,9 @@ typedef enum
LEXER_ASSIGN_MULTIPLY, /**< "*=" (prec: 3) */
LEXER_ASSIGN_DIVIDE, /**< "/=" (prec: 3) */
LEXER_ASSIGN_MODULO, /**< "%=" (prec: 3) */
#if ENABLED (JERRY_ES2015)
LEXER_ASSIGN_EXPONENTIATION, /**< "**=" (prec: 3) */
#endif /* ENABLED (JERRY_ES2015) */
LEXER_ASSIGN_LEFT_SHIFT, /**< "<<=" (prec: 3) */
LEXER_ASSIGN_RIGHT_SHIFT, /**< ">>=" (prec: 3) */
LEXER_ASSIGN_UNS_RIGHT_SHIFT, /**< ">>>=" (prec: 3) */
@@ -108,6 +118,9 @@ typedef enum
LEXER_MULTIPLY, /**< "*" (prec: 14) */
LEXER_DIVIDE, /**< "/" (prec: 14) */
LEXER_MODULO, /**< "%" (prec: 14) */
#if ENABLED (JERRY_ES2015)
LEXER_EXPONENTIATION, /**< "**" (prec: 15) */
#endif /* ENABLED (JERRY_ES2015) */
LEXER_LEFT_BRACE, /**< "{" */
LEXER_LEFT_PAREN, /**< "(" */
+59 -8
View File
@@ -38,10 +38,15 @@
#define PARSER_RIGHT_TO_LEFT_ORDER_MAX_PRECEDENCE 6
/**
* Precende for ternary operation.
* Precedence for ternary operation.
*/
#define PARSER_RIGHT_TO_LEFT_ORDER_TERNARY_PRECEDENCE 4
/**
* Precedence for exponentiation operation.
*/
#define PARSER_RIGHT_TO_LEFT_ORDER_EXPONENTIATION 15
/**
* Value of grouping level increase and decrease.
*/
@@ -53,14 +58,28 @@
* See also:
* lexer_token_type_t
*/
static const uint8_t parser_binary_precedence_table[36] =
static const uint8_t parser_binary_precedence_table[] =
{
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
#if ENABLED (JERRY_ES2015)
3,
#endif /* ENABLED (JERRY_ES2015) */
4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
11, 11, 11, 11, 11, 11, 12, 12, 12,
13, 13, 14, 14, 14
13, 13, 14, 14, 14,
#if ENABLED (JERRY_ES2015)
15,
#endif /* ENABLED (JERRY_ES2015) */
};
#if ENABLED (JERRY_ES2015)
JERRY_STATIC_ASSERT (sizeof (parser_binary_precedence_table) == 38,
parser_binary_precedence_table_should_have_38_values_in_es2015);
#else /* !ENABLED (JERRY_ES2015) */
JERRY_STATIC_ASSERT (sizeof (parser_binary_precedence_table) == 36,
parser_binary_precedence_table_should_have_36_values_in_es51);
#endif /* ENABLED (JERRY_ES2015) */
/**
* Generate byte code for operators with lvalue.
*/
@@ -1562,14 +1581,19 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
{
bool is_negative_number = false;
while (context_p->stack_top_uint8 == LEXER_PLUS
|| context_p->stack_top_uint8 == LEXER_NEGATE)
if ((context_p->stack_top_uint8 == LEXER_PLUS || context_p->stack_top_uint8 == LEXER_NEGATE)
&& !lexer_check_post_primary_exp (context_p))
{
if (context_p->stack_top_uint8 == LEXER_NEGATE)
do
{
is_negative_number = !is_negative_number;
if (context_p->stack_top_uint8 == LEXER_NEGATE)
{
is_negative_number = !is_negative_number;
}
parser_stack_pop_uint8 (context_p);
}
parser_stack_pop_uint8 (context_p);
while (context_p->stack_top_uint8 == LEXER_PLUS
|| context_p->stack_top_uint8 == LEXER_NEGATE);
}
if (lexer_construct_number_object (context_p, true, is_negative_number))
@@ -2123,15 +2147,31 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
break;
}
#if ENABLED (JERRY_ES2015)
uint8_t last_unary_token = LEXER_INCREASE;
#endif /* ENABLED (JERRY_ES2015) */
/* Generate byte code for the unary operators. */
while (true)
{
uint8_t token = context_p->stack_top_uint8;
if (!LEXER_IS_UNARY_OP_TOKEN (token))
{
#if ENABLED (JERRY_ES2015)
if (context_p->token.type == LEXER_EXPONENTIATION
&& last_unary_token != LEXER_INCREASE
&& last_unary_token != LEXER_DECREASE)
{
parser_raise_error (context_p, PARSER_ERR_INVALID_EXPONENTIATION);
}
#endif /* ENABLED (JERRY_ES2015) */
break;
}
#if ENABLED (JERRY_ES2015)
last_unary_token = token;
#endif /* ENABLED (JERRY_ES2015) */
parser_push_result (context_p);
parser_stack_pop_uint8 (context_p);
@@ -3142,6 +3182,16 @@ process_unary_expression:
{
min_prec_treshold = parser_binary_precedence_table[context_p->token.type - LEXER_FIRST_BINARY_OP];
#if ENABLED (JERRY_ES2015)
/* Check for BINARY_LVALUE tokens + LEXER_LOGICAL_OR + LEXER_LOGICAL_AND + LEXER_EXPONENTIATION */
if ((min_prec_treshold == PARSER_RIGHT_TO_LEFT_ORDER_EXPONENTIATION)
|| (min_prec_treshold <= PARSER_RIGHT_TO_LEFT_ORDER_MAX_PRECEDENCE
&& min_prec_treshold != PARSER_RIGHT_TO_LEFT_ORDER_TERNARY_PRECEDENCE))
{
/* Right-to-left evaluation order. */
min_prec_treshold++;
}
#else /* !ENABLED (JERRY_ES2015) */
/* Check for BINARY_LVALUE tokens + LEXER_LOGICAL_OR + LEXER_LOGICAL_AND */
if (min_prec_treshold <= PARSER_RIGHT_TO_LEFT_ORDER_MAX_PRECEDENCE
&& min_prec_treshold != PARSER_RIGHT_TO_LEFT_ORDER_TERNARY_PRECEDENCE)
@@ -3149,6 +3199,7 @@ process_unary_expression:
/* Right-to-left evaluation order. */
min_prec_treshold++;
}
#endif /* ENABLED (JERRY_ES2015) */
}
parser_process_binary_opcodes (context_p, min_prec_treshold);
@@ -667,6 +667,7 @@ bool lexer_check_next_character (parser_context_t *context_p, lit_utf8_byte_t ch
bool lexer_check_next_characters (parser_context_t *context_p, lit_utf8_byte_t character1,
lit_utf8_byte_t character2);
uint8_t lexer_consume_next_character (parser_context_t *context_p);
bool lexer_check_post_primary_exp (parser_context_t *context_p);
#if ENABLED (JERRY_ES2015)
void lexer_skip_empty_statements (parser_context_t *context_p);
bool lexer_check_arrow (parser_context_t *context_p);
+4
View File
@@ -1179,6 +1179,10 @@ parser_error_to_string (parser_error_t error) /**< error code */
{
return "Illegal property in declaration context.";
}
case PARSER_ERR_INVALID_EXPONENTIATION:
{
return "Left operand of ** operator cannot be unary expression.";
}
case PARSER_ERR_FORMAL_PARAM_AFTER_REST_PARAMETER:
{
return "Rest parameter must be the last formal parameter.";
+1
View File
@@ -146,6 +146,7 @@ typedef enum
PARSER_ERR_DUPLICATED_ARGUMENT_NAMES, /**< duplicated argument names */
PARSER_ERR_INVALID_DESTRUCTURING_PATTERN, /**< invalid destructuring pattern */
PARSER_ERR_ILLEGAL_PROPERTY_IN_DECLARATION, /**< illegal property in declaration context */
PARSER_ERR_INVALID_EXPONENTIATION, /**< left operand of ** operator cannot be unary expression */
PARSER_ERR_NEW_TARGET_EXPECTED, /**< expected new.target expression */
PARSER_ERR_NEW_TARGET_NOT_ALLOWED, /**< new.target is not allowed in the given context */
#endif /* ENABLED (JERRY_ES2015) */