aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c119
1 files changed, 1 insertions, 118 deletions
diff --git a/src/parser.c b/src/parser.c
index e391500..fe0802f 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -329,123 +329,6 @@ Node *parse_identifier(Lexer *lexer)
return NULL;
}
-Node *handle_unary_expr_types(Node *node, Token *token)
-{
- Type *old_type = node->unary_expr->expr_type;
-
- if (node->type == OP_NOT) {
- node->expr_type = type_new(TYPE_INT);
- } else if (node->type == OP_ADDROF) {
- Type *ptr = type_new(TYPE_PTR);
- ptr->ptr = old_type;
- node->expr_type = ptr;
- } else if (node->type == OP_DEREF) {
- if (old_type->type != TYPE_PTR)
- die_location(token->loc, "Cannot dereference non-pointer type");
- node->expr_type = old_type->ptr;
- } else if (node->type == OP_NEG) {
- if (old_type->type != TYPE_INT)
- die_location(token->loc, "Cannot negate non-integer type");
- node->expr_type = type_new(TYPE_INT);
- } else {
- // Default to not changing the type
- node->expr_type = old_type;
- }
- // die_location(token->loc, "Unknown unary expression type in handle_unary_expr_types\n");
- return node;
-}
-
-Node *handle_binary_expr_types(Node *node, Token *token)
-{
- Type *left = node->binary.left->expr_type;
- Type *right = node->binary.right->expr_type;
-
- switch (node->type)
- {
- case OP_PLUS: {
- if (left->type == TYPE_INT && right->type == TYPE_INT) {
- node->expr_type = type_new(TYPE_INT);
- } else if (left->type == TYPE_PTR && right->type == TYPE_INT) {
- node->expr_type = left;
- // Pointer arithmetic!
- Node *mul = Node_new(OP_MUL);
- mul->binary.left = node->binary.right;
- mul->binary.right = Node_new(AST_LITERAL);
- mul->binary.right->literal.type = type_new(TYPE_INT);
- mul->binary.right->literal.as_int = size_for_type(left->ptr);
- node->binary.right = mul;
- } else if (left->type == TYPE_INT && right->type == TYPE_PTR) {
- node->expr_type = right;
- // Pointer arithmetic!
- Node *mul = Node_new(OP_MUL);
- mul->binary.left = node->binary.left;
- mul->binary.right = Node_new(AST_LITERAL);
- mul->binary.right->literal.type = type_new(TYPE_INT);
- mul->binary.right->literal.as_int = size_for_type(right->ptr);
- node->binary.left = mul;
- } else {
- die_location(token->loc, "Cannot add non-integer types");
- }
- } break;
-
- case OP_MINUS: {
- if (left->type == TYPE_INT && right->type == TYPE_INT) {
- node->expr_type = type_new(TYPE_INT);
- } else if (left->type == TYPE_PTR && right->type == TYPE_INT) {
- node->expr_type = left;
- // Pointer arithmetic!
- Node *mul = Node_new(OP_MUL);
- mul->binary.left = node->binary.right;
- mul->binary.right = Node_from_int_literal(size_for_type(left->ptr));
- node->binary.right = mul;
- } else if (left->type == TYPE_INT && right->type == TYPE_PTR) {
- node->expr_type = right;
- // Pointer arithmetic!
- Node *mul = Node_new(OP_MUL);
- mul->binary.left = node->binary.left;
- mul->binary.right = Node_from_int_literal(size_for_type(right->ptr));
- node->binary.left = mul;
- } else if (left->type == TYPE_PTR && right->type == TYPE_PTR) {
- // TODO: Check for different pointer types
- node->expr_type = type_new(TYPE_INT);
- // Divide by size of pointer
- Node *div = Node_new(OP_DIV);
- div->binary.left = node;
- div->binary.right = Node_from_int_literal(size_for_type(left->ptr));
- div->expr_type = node->expr_type;
- node = div;
- } else {
- die_location(token->loc, "Cannot subtract non-integer types");
- }
- } break;
-
- case OP_DIV:
- case OP_MOD:
- case OP_MUL: {
- if (left->type == TYPE_INT && right->type == TYPE_INT) {
- node->expr_type = left;
- } else {
- die_location(token->loc, "Cannot do operation `%s` non-integer types", node_type_to_str(node->type));
- }
- } break;
-
- case OP_EQ:
- case OP_NEQ:
- case OP_LT:
- case OP_GT:
- case OP_LEQ:
- case OP_GEQ:
- case OP_AND:
- case OP_OR: {
- node->expr_type = type_new(TYPE_INT);
- } break;
-
- default:
- die_location(token->loc, "Unknown binary expression type in handle_binary_expr_types\n");
- }
- return node;
-}
-
Node *parse_factor(Lexer *lexer)
{
// TODO: We need to properly handle type conversions / operations with different types
@@ -506,7 +389,7 @@ Node *parse_factor(Lexer *lexer)
Node *right = next_parser(lexer); \
op->binary.left = expr; \
op->binary.right = right; \
- op = handle_binary_expr_types(op, &token); \
+ op = handle_binary_expr_types(op, &token); \
expr = op; \
token = Lexer_peek(lexer); \
} \