aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-04 07:38:53 -0500
committerMustafa Quraish <[email protected]>2022-02-05 08:56:15 -0500
commit6a376cfd6f89a6ceb39a8c425cf8095647170c7e (patch)
tree0f5b6e0dee971222824c71e8ec207bbdbd1eb3ec /src/parser.c
parentAllow `builtins.c` to inject constants into program, use for syscalls (diff)
downloadcup-6a376cfd6f89a6ceb39a8c425cf8095647170c7e.tar.xz
cup-6a376cfd6f89a6ceb39a8c425cf8095647170c7e.zip
Add `void` type and allow void* to be assigned to other ptr types
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/parser.c b/src/parser.c
index 267947c..49c9689 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -227,6 +227,9 @@ Type *parse_type(Lexer *lexer)
} else if (token.type == TOKEN_CHAR) {
Lexer_next(lexer);
type = type_new(TYPE_CHAR);
+ } else if (token.type == TOKEN_VOID) {
+ Lexer_next(lexer);
+ type = type_new(TYPE_VOID);
} else {
assert_token(token, TOKEN_IDENTIFIER);
// TODO: Don't allow a type to contain itself.
@@ -590,7 +593,6 @@ Node *parse_factor(Lexer *lexer)
} else if (token.type == TOKEN_MINUSMINUS) {
die_location(token.loc, "Post-decrementing is not supported\n");
} else if (token.type == TOKEN_DOT) {
- // TODO: Pointer to struct
if (!is_struct_or_struct_ptr(expr->expr_type))
die_location(token.loc, "Cannot access member of non-struct type");
@@ -607,8 +609,8 @@ Node *parse_factor(Lexer *lexer)
member->member.expr = expr;
member->member.offset = struct_type->fields.offset[index];
member->member.is_ptr = (expr->expr_type->type == TYPE_PTR);
- expr = member;
+ expr = decay_array_to_pointer(member, &token);
} else {
break;
}
@@ -685,8 +687,8 @@ Node *parse_expression(Lexer *lexer)
Lexer_next(lexer);
Node *assign = Node_new(OP_ASSIGN);
assign->assign.var = node;
- assign->assign.value = parse_expression(lexer);
-
+ Node *expr = parse_expression(lexer);
+ assign->assign.value = expr;
if (!is_convertible(node->expr_type, assign->assign.value->expr_type)) {
fprintf(stderr, "- Variable type: %s\n", type_to_str(assign->assign.var->expr_type));
@@ -901,7 +903,7 @@ Node *parse_func(Lexer *lexer)
func->func.return_type = parse_type(lexer);
} else {
// No return type, void fn.
- func->func.return_type = type_new(TYPE_NONE);
+ func->func.return_type = type_new(TYPE_VOID);
}
// Make sure there's no funny business with the stack offset