Skip to content

Commit

Permalink
logical and relational operators parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
LesleyLai committed Dec 29, 2024
1 parent 473d41f commit 6c75460
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 19 deletions.
13 changes: 12 additions & 1 deletion include/mcc/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ typedef enum ExprType {

typedef enum UnaryOpType {
UNARY_OP_INVALID = 0,
UNARY_OP_MINUS, // -
UNARY_OP_NEGATION, // -
UNARY_OP_BITWISE_TYPE_COMPLEMENT, // ~
UNARY_OP_NOT, // -
} UnaryOpType;

typedef enum BinaryOpType {
Expand All @@ -33,6 +34,16 @@ typedef enum BinaryOpType {
BINARY_OP_BITWISE_XOR,
BINARY_OP_SHIFT_LEFT,
BINARY_OP_SHIFT_RIGHT,

BINARY_OP_AND, // logical and
BINARY_OP_OR, // logical or

BINARY_OP_EQUAL,
BINARY_OP_NOT_EQUAL,
BINARY_OP_LESS,
BINARY_OP_LESS_EQUAL,
BINARY_OP_GREATER,
BINARY_OP_GREATER_EQUAL,
} BinaryOpType;

typedef struct Expr Expr;
Expand Down
33 changes: 21 additions & 12 deletions src/frontend/ast_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ static const char* unary_op_name(UnaryOpType unary_op_type)
{
switch (unary_op_type) {
case UNARY_OP_INVALID: MCC_UNREACHABLE();
case UNARY_OP_MINUS: return "-";
case UNARY_OP_BITWISE_TYPE_COMPLEMENT: return "~";
case UNARY_OP_NOT: return "not";
case UNARY_OP_NEGATION: return "negation";
case UNARY_OP_BITWISE_TYPE_COMPLEMENT: return "complement";
}
MCC_ASSERT_MSG(false, "invalid enum");
}
Expand All @@ -25,16 +26,24 @@ static const char* binary_op_name(BinaryOpType binary_op_type)
{
switch (binary_op_type) {
case BINARY_OP_INVALID: MCC_UNREACHABLE();
case BINARY_OP_PLUS: return "+";
case BINARY_OP_MINUS: return "-";
case BINARY_OP_MULT: return "*";
case BINARY_OP_DIVIDE: return "/";
case BINARY_OP_MOD: return "%";
case BINARY_OP_BITWISE_AND: return "&";
case BINARY_OP_BITWISE_OR: return "|";
case BINARY_OP_BITWISE_XOR: return "^";
case BINARY_OP_SHIFT_LEFT: return "<<";
case BINARY_OP_SHIFT_RIGHT: return ">>";
case BINARY_OP_PLUS: return "plus";
case BINARY_OP_MINUS: return "minus";
case BINARY_OP_MULT: return "mult";
case BINARY_OP_DIVIDE: return "divide";
case BINARY_OP_MOD: return "mod";
case BINARY_OP_BITWISE_AND: return "bit-and";
case BINARY_OP_BITWISE_OR: return "bit-or";
case BINARY_OP_BITWISE_XOR: return "xor";
case BINARY_OP_SHIFT_LEFT: return "left-shift";
case BINARY_OP_SHIFT_RIGHT: return "right-shift";
case BINARY_OP_AND: return "logical-and";
case BINARY_OP_OR: return "logical-or";
case BINARY_OP_EQUAL: return "equal";
case BINARY_OP_NOT_EQUAL: return "not equal";
case BINARY_OP_LESS: return "less";
case BINARY_OP_LESS_EQUAL: return "less-equal";
case BINARY_OP_GREATER: return "greater";
case BINARY_OP_GREATER_EQUAL: return "greater-equal";
}
MCC_ASSERT_MSG(false, "invalid enum");
}
Expand Down
33 changes: 28 additions & 5 deletions src/frontend/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ static Expr* parse_number_literal(Parser* parser)
typedef enum Precedence {
PREC_NONE = 0,
PREC_ASSIGNMENT, // =
PREC_OR, // or
PREC_AND, // and
PREC_OR, // ||
PREC_AND, // &&
PREC_BITWISE_OR, // |
PREC_BITWISE_XOR, // ^
PREC_BITWISE_AND, // &
Expand Down Expand Up @@ -190,7 +190,22 @@ static ParseRule rules[TOKEN_TYPES_COUNT] = {
PREC_BITWISE_AND},
[TOKEN_CARET] = {NULL, parse_binary_op_left_associative, PREC_BITWISE_XOR},
[TOKEN_BAR] = {NULL, parse_binary_op_left_associative, PREC_BITWISE_OR},

[TOKEN_AMPERSAND_AMPERSAND] = {NULL, parse_binary_op_left_associative,
PREC_AND},
[TOKEN_BAR_BAR] = {NULL, parse_binary_op_left_associative, PREC_OR},
[TOKEN_EQUAL_EQUAL] = {NULL, parse_binary_op_left_associative,
PREC_EQUALITY},
[TOKEN_NOT] = {parse_unary_op, NULL, PREC_UNARY},
[TOKEN_NOT_EQUAL] = {NULL, parse_binary_op_left_associative, PREC_EQUALITY},

[TOKEN_LESS] = {NULL, parse_binary_op_left_associative, PREC_COMPARISON},
[TOKEN_LESS_EQUAL] = {NULL, parse_binary_op_left_associative,
PREC_COMPARISON},
[TOKEN_LESS_LESS] = {NULL, parse_binary_op_left_associative, PREC_SHIFT},
[TOKEN_GREATER] = {NULL, parse_binary_op_left_associative, PREC_COMPARISON},
[TOKEN_GREATER_EQUAL] = {NULL, parse_binary_op_left_associative,
PREC_COMPARISON},
[TOKEN_GREATER_GREATER] = {NULL, parse_binary_op_left_associative,
PREC_SHIFT},
[TOKEN_TILDE] = {parse_unary_op, NULL, PREC_TERM},
Expand Down Expand Up @@ -247,10 +262,11 @@ static Expr* parse_unary_op(Parser* parser)
{
Token operator_token = parser_previous_token(parser);

UnaryOpType operator_type = 0xdeadbeef;
UnaryOpType operator_type;
switch (operator_token.type) {
case TOKEN_MINUS: operator_type = UNARY_OP_MINUS; break;
case TOKEN_MINUS: operator_type = UNARY_OP_NEGATION; break;
case TOKEN_TILDE: operator_type = UNARY_OP_BITWISE_TYPE_COMPLEMENT; break;
case TOKEN_NOT: operator_type = UNARY_OP_NOT; break;
default: MCC_ASSERT_MSG(false, "Unexpected operator");
}

Expand Down Expand Up @@ -286,7 +302,14 @@ static BinaryOpType binop_type_from_token_type(TokenType token_type)
case TOKEN_AMPERSAND: return BINARY_OP_BITWISE_AND;
case TOKEN_CARET: return BINARY_OP_BITWISE_XOR;
case TOKEN_BAR: return BINARY_OP_BITWISE_OR;

case TOKEN_AMPERSAND_AMPERSAND: return BINARY_OP_AND;
case TOKEN_BAR_BAR: return BINARY_OP_OR;
case TOKEN_EQUAL_EQUAL: return BINARY_OP_EQUAL;
case TOKEN_NOT_EQUAL: return BINARY_OP_NOT_EQUAL;
case TOKEN_LESS: return BINARY_OP_LESS;
case TOKEN_LESS_EQUAL: return BINARY_OP_LESS_EQUAL;
case TOKEN_GREATER: return BINARY_OP_GREATER;
case TOKEN_GREATER_EQUAL: return BINARY_OP_GREATER_EQUAL;
default: MCC_UNREACHABLE();
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/ir/ir_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ static IRInstructionType instruction_typ_from_unary_op(UnaryOpType op_type)
{
switch (op_type) {
case UNARY_OP_INVALID: MCC_UNREACHABLE();
case UNARY_OP_MINUS: return IR_NEG;
case UNARY_OP_NEGATION: return IR_NEG;
case UNARY_OP_BITWISE_TYPE_COMPLEMENT: return IR_COMPLEMENT;
case UNARY_OP_NOT: MCC_UNIMPLEMENTED();
}
MCC_UNREACHABLE();
}
Expand All @@ -49,6 +50,14 @@ static IRInstructionType instruction_typ_from_binary_op(BinaryOpType op_type)
case BINARY_OP_BITWISE_XOR: return IR_BITWISE_XOR;
case BINARY_OP_SHIFT_LEFT: return IR_SHIFT_LEFT;
case BINARY_OP_SHIFT_RIGHT: return IR_SHIFT_RIGHT_ARITHMETIC;
case BINARY_OP_AND: MCC_UNIMPLEMENTED();
case BINARY_OP_OR: MCC_UNIMPLEMENTED();
case BINARY_OP_EQUAL: MCC_UNIMPLEMENTED();
case BINARY_OP_NOT_EQUAL: MCC_UNIMPLEMENTED();
case BINARY_OP_LESS: MCC_UNIMPLEMENTED();
case BINARY_OP_LESS_EQUAL: MCC_UNIMPLEMENTED();
case BINARY_OP_GREATER: MCC_UNIMPLEMENTED();
case BINARY_OP_GREATER_EQUAL: MCC_UNIMPLEMENTED();
}
MCC_UNREACHABLE();
}
Expand Down

0 comments on commit 6c75460

Please sign in to comment.