diff --git a/lang/include/ivy/lang/operator.h b/lang/include/ivy/lang/operator.h index 5ff7f07..0bccdb5 100644 --- a/lang/include/ivy/lang/operator.h +++ b/lang/include/ivy/lang/operator.h @@ -77,7 +77,10 @@ enum ivy_operator_id { IVY_OP_UNDERSTANDS, IVY_OP_SELF_ACCESS, IVY_OP_PKG_ACCESS, + + /* these are not real operators, and are just used internally by the parser. */ IVY_OP_MSG, + IVY_OP_LEFT_PAREN, }; struct ivy_operator { @@ -90,5 +93,6 @@ struct ivy_operator { }; IVY_API const struct ivy_operator *ivy_operator_get(unsigned int token); +IVY_API const char *ivy_operator_id_to_string(enum ivy_operator_id op); #endif \ No newline at end of file diff --git a/lang/operator.c b/lang/operator.c index 8ca81a6..881041a 100644 --- a/lang/operator.c +++ b/lang/operator.c @@ -34,7 +34,6 @@ /* clang-format off */ static const struct ivy_operator operators[] = { - TOK_OP(MSG, IDENT, UNARY_MSG, LEFT, POSTFIX, UNARY), SYM_OP(ASSIGN, EQUAL, ASSIGN, RIGHT, INFIX, BINARY), SYM_OP(ADD, PLUS, ADDITION, LEFT, INFIX, BINARY), SYM_OP(SUBTRACT, HYPHEN, ADDITION, LEFT, INFIX, BINARY), @@ -70,6 +69,10 @@ static const struct ivy_operator operators[] = { KW_OP(UNDERSTANDS, UNDERSTANDS, IS, LEFT, INFIX, BINARY), SYM_OP(SELF_ACCESS, DOUBLE_COLON, SUBSCRIPT, LEFT, INFIX, BINARY), SYM_OP(PKG_ACCESS, HYPHEN_RIGHT_ANGLE, SUBSCRIPT, LEFT, INFIX, BINARY), + + /* parser-internal pseudo-operators. */ + TOK_OP(MSG, IDENT, UNARY_MSG, LEFT, POSTFIX, UNARY), + SYM_OP(LEFT_PAREN, LEFT_PAREN, SUBSCRIPT, LEFT, INFIX, UNARY), }; static const size_t nr_operators = sizeof operators / sizeof operators[0]; /* clang-format on */ @@ -87,3 +90,54 @@ const struct ivy_operator *ivy_operator_get(unsigned int token) return op; } + +#define ENUM_STR(x) \ + case x: \ + return #x + + +const char *ivy_operator_id_to_string(enum ivy_operator_id op) +{ + switch (op) { + ENUM_STR(IVY_OP_NONE); + ENUM_STR(IVY_OP_ASSIGN); + ENUM_STR(IVY_OP_ADD); + ENUM_STR(IVY_OP_SUBTRACT); + ENUM_STR(IVY_OP_MULTIPLY); + ENUM_STR(IVY_OP_DIVIDE); + ENUM_STR(IVY_OP_MODULO); + ENUM_STR(IVY_OP_LEFT_SHIFT); + ENUM_STR(IVY_OP_RIGHT_SHIFT); + ENUM_STR(IVY_OP_BINARY_AND); + ENUM_STR(IVY_OP_BINARY_OR); + ENUM_STR(IVY_OP_BINARY_XOR); + ENUM_STR(IVY_OP_LESS_THAN); + ENUM_STR(IVY_OP_GREATER_THAN); + ENUM_STR(IVY_OP_CASCADE); + ENUM_STR(IVY_OP_EQUAL); + ENUM_STR(IVY_OP_NOT_EQUAL); + ENUM_STR(IVY_OP_LESS_EQUAL); + ENUM_STR(IVY_OP_GREATER_EQUAL); + ENUM_STR(IVY_OP_ADD_ASSIGN); + ENUM_STR(IVY_OP_SUBTRACT_ASSIGN); + ENUM_STR(IVY_OP_MULTIPLY_ASSIGN); + ENUM_STR(IVY_OP_DIVIDE_ASSIGN); + ENUM_STR(IVY_OP_MODULO_ASSIGN); + ENUM_STR(IVY_OP_LEFT_SHIFT_ASSIGN); + ENUM_STR(IVY_OP_RIGHT_SHIFT_ASSIGN); + ENUM_STR(IVY_OP_BINARY_AND_ASSIGN); + ENUM_STR(IVY_OP_BINARY_OR_ASSIGN); + ENUM_STR(IVY_OP_BINARY_XOR_ASSIGN); + ENUM_STR(IVY_OP_AND); + ENUM_STR(IVY_OP_OR); + ENUM_STR(IVY_OP_IS); + ENUM_STR(IVY_OP_NOT); + ENUM_STR(IVY_OP_UNDERSTANDS); + ENUM_STR(IVY_OP_SELF_ACCESS); + ENUM_STR(IVY_OP_PKG_ACCESS); + ENUM_STR(IVY_OP_MSG); + ENUM_STR(IVY_OP_LEFT_PAREN); + default: + return ""; + } +}