py/lexer: Reorder operator tokens to match corresponding binary ops.

This commit is contained in:
Damien George 2019-07-25 12:10:45 +10:00
parent 78e0e76b4f
commit 6ce7c051e8
3 changed files with 40 additions and 36 deletions

View File

@ -96,25 +96,43 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_KW_WITH, MP_TOKEN_KW_WITH,
MP_TOKEN_KW_YIELD, MP_TOKEN_KW_YIELD,
MP_TOKEN_OP_TILDE,
// Order of these 6 matches corresponding mp_binary_op_t operator
MP_TOKEN_OP_LESS,
MP_TOKEN_OP_MORE,
MP_TOKEN_OP_DBL_EQUAL,
MP_TOKEN_OP_LESS_EQUAL,
MP_TOKEN_OP_MORE_EQUAL,
MP_TOKEN_OP_NOT_EQUAL,
// Order of these 12 matches corresponding mp_binary_op_t operator
MP_TOKEN_OP_PIPE,
MP_TOKEN_OP_CARET,
MP_TOKEN_OP_AMPERSAND,
MP_TOKEN_OP_DBL_LESS,
MP_TOKEN_OP_DBL_MORE,
MP_TOKEN_OP_PLUS, MP_TOKEN_OP_PLUS,
MP_TOKEN_OP_MINUS, MP_TOKEN_OP_MINUS,
MP_TOKEN_OP_STAR, MP_TOKEN_OP_STAR,
MP_TOKEN_OP_DBL_STAR,
MP_TOKEN_OP_SLASH,
MP_TOKEN_OP_DBL_SLASH, MP_TOKEN_OP_DBL_SLASH,
MP_TOKEN_OP_SLASH,
MP_TOKEN_OP_PERCENT, MP_TOKEN_OP_PERCENT,
MP_TOKEN_OP_LESS, MP_TOKEN_OP_DBL_STAR,
MP_TOKEN_OP_DBL_LESS,
MP_TOKEN_OP_MORE, // Order of these 12 matches corresponding mp_binary_op_t operator
MP_TOKEN_OP_DBL_MORE, MP_TOKEN_DEL_PIPE_EQUAL,
MP_TOKEN_OP_AMPERSAND, MP_TOKEN_DEL_CARET_EQUAL,
MP_TOKEN_OP_PIPE, MP_TOKEN_DEL_AMPERSAND_EQUAL,
MP_TOKEN_OP_CARET, MP_TOKEN_DEL_DBL_LESS_EQUAL,
MP_TOKEN_OP_TILDE, MP_TOKEN_DEL_DBL_MORE_EQUAL,
MP_TOKEN_OP_LESS_EQUAL, MP_TOKEN_DEL_PLUS_EQUAL,
MP_TOKEN_OP_MORE_EQUAL, MP_TOKEN_DEL_MINUS_EQUAL,
MP_TOKEN_OP_DBL_EQUAL, MP_TOKEN_DEL_STAR_EQUAL,
MP_TOKEN_OP_NOT_EQUAL, MP_TOKEN_DEL_DBL_SLASH_EQUAL,
MP_TOKEN_DEL_SLASH_EQUAL,
MP_TOKEN_DEL_PERCENT_EQUAL,
MP_TOKEN_DEL_DBL_STAR_EQUAL,
MP_TOKEN_DEL_PAREN_OPEN, MP_TOKEN_DEL_PAREN_OPEN,
MP_TOKEN_DEL_PAREN_CLOSE, MP_TOKEN_DEL_PAREN_CLOSE,
@ -128,18 +146,6 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_DEL_SEMICOLON, MP_TOKEN_DEL_SEMICOLON,
MP_TOKEN_DEL_AT, MP_TOKEN_DEL_AT,
MP_TOKEN_DEL_EQUAL, MP_TOKEN_DEL_EQUAL,
MP_TOKEN_DEL_PLUS_EQUAL,
MP_TOKEN_DEL_MINUS_EQUAL,
MP_TOKEN_DEL_STAR_EQUAL,
MP_TOKEN_DEL_SLASH_EQUAL,
MP_TOKEN_DEL_DBL_SLASH_EQUAL,
MP_TOKEN_DEL_PERCENT_EQUAL,
MP_TOKEN_DEL_AMPERSAND_EQUAL,
MP_TOKEN_DEL_PIPE_EQUAL,
MP_TOKEN_DEL_CARET_EQUAL,
MP_TOKEN_DEL_DBL_MORE_EQUAL,
MP_TOKEN_DEL_DBL_LESS_EQUAL,
MP_TOKEN_DEL_DBL_STAR_EQUAL,
MP_TOKEN_DEL_MINUS_MORE, MP_TOKEN_DEL_MINUS_MORE,
} mp_token_kind_t; } mp_token_kind_t;

View File

@ -645,19 +645,17 @@ STATIC bool fold_constants(parser_t *parser, uint8_t rule_id, size_t num_args) {
} }
mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(peek_result(parser, i)); mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(peek_result(parser, i));
static const uint8_t token_to_op[] = { static const uint8_t token_to_op[] = {
MP_BINARY_OP_LSHIFT,
MP_BINARY_OP_RSHIFT,
MP_BINARY_OP_ADD, MP_BINARY_OP_ADD,
MP_BINARY_OP_SUBTRACT, MP_BINARY_OP_SUBTRACT,
MP_BINARY_OP_MULTIPLY, MP_BINARY_OP_MULTIPLY,
255,//MP_BINARY_OP_POWER,
255,//MP_BINARY_OP_TRUE_DIVIDE,
MP_BINARY_OP_FLOOR_DIVIDE, MP_BINARY_OP_FLOOR_DIVIDE,
255,//MP_BINARY_OP_TRUE_DIVIDE,
MP_BINARY_OP_MODULO, MP_BINARY_OP_MODULO,
255,//MP_BINARY_OP_LESS 255,//MP_BINARY_OP_POWER,
MP_BINARY_OP_LSHIFT,
255,//MP_BINARY_OP_MORE
MP_BINARY_OP_RSHIFT,
}; };
mp_binary_op_t op = token_to_op[tok - MP_TOKEN_OP_PLUS]; mp_binary_op_t op = token_to_op[tok - MP_TOKEN_OP_DBL_LESS];
if (op == (mp_binary_op_t)255) { if (op == (mp_binary_op_t)255) {
return false; return false;
} }

View File

@ -70,7 +70,7 @@ typedef enum {
// Note: the first 9+12+12 of these are used in bytecode and changing // Note: the first 9+12+12 of these are used in bytecode and changing
// them requires changing the bytecode version. // them requires changing the bytecode version.
typedef enum { typedef enum {
// 9 relational operations, should return a bool // 9 relational operations, should return a bool; order of first 6 matches corresponding mp_token_kind_t
MP_BINARY_OP_LESS, MP_BINARY_OP_LESS,
MP_BINARY_OP_MORE, MP_BINARY_OP_MORE,
MP_BINARY_OP_EQUAL, MP_BINARY_OP_EQUAL,
@ -81,7 +81,7 @@ typedef enum {
MP_BINARY_OP_IS, MP_BINARY_OP_IS,
MP_BINARY_OP_EXCEPTION_MATCH, MP_BINARY_OP_EXCEPTION_MATCH,
// 12 inplace arithmetic operations // 12 inplace arithmetic operations; order matches corresponding mp_token_kind_t
MP_BINARY_OP_INPLACE_OR, MP_BINARY_OP_INPLACE_OR,
MP_BINARY_OP_INPLACE_XOR, MP_BINARY_OP_INPLACE_XOR,
MP_BINARY_OP_INPLACE_AND, MP_BINARY_OP_INPLACE_AND,
@ -95,7 +95,7 @@ typedef enum {
MP_BINARY_OP_INPLACE_MODULO, MP_BINARY_OP_INPLACE_MODULO,
MP_BINARY_OP_INPLACE_POWER, MP_BINARY_OP_INPLACE_POWER,
// 12 normal arithmetic operations // 12 normal arithmetic operations; order matches corresponding mp_token_kind_t
MP_BINARY_OP_OR, MP_BINARY_OP_OR,
MP_BINARY_OP_XOR, MP_BINARY_OP_XOR,
MP_BINARY_OP_AND, MP_BINARY_OP_AND,