summaryrefslogtreecommitdiff
path: root/parser.c
diff options
context:
space:
mode:
authorCarson Fleming <cflems@cflems.net>2026-07-30 00:20:30 -0400
committerCarson Fleming <cflems@cflems.net>2026-07-30 00:20:52 -0400
commit61531e58066e4d803624ab2c6dac84445fb75e59 (patch)
treec4ed43fdfdbd94c461e7429bbfa6cd46e4e78992 /parser.c
parent3445947c072a4cd533f5f3a53178f77361859123 (diff)
downloadccc-61531e58066e4d803624ab2c6dac84445fb75e59.tar.gz
casting
Diffstat (limited to 'parser.c')
-rw-r--r--parser.c46
1 files changed, 29 insertions, 17 deletions
diff --git a/parser.c b/parser.c
index b92ff8b..29aea36 100644
--- a/parser.c
+++ b/parser.c
@@ -269,11 +269,35 @@ static void parse_expr_binary(struct expr_node* p_node) {
parse_expr(rhs);
}
-static void parse_paren(struct paren_node* p_node) {
+static bool is_type_token() {
+ if (tok.type != TK_IDENT) return false;
+
+ /* check primitive types */
+ for (integral_t i = 0; primitive_types[i] != NULL; i++) {
+ if (strcmp(tok.data.ident, primitive_types[i]->name) == 0) return true;
+ }
+
+ /* check type aliases */
+ return scope_get_type(scope, NULL, tok.data.ident);
+}
+
+static void parse_paren(struct expr_node* p_node) {
expect(TK_LPAREN);
- p_node->expr_list = ccc_alloc(sizeof(struct expr_list_node));
- parse_expr_list(p_node->expr_list);
- expect(TK_RPAREN);
+ peek_or_panic();
+ if (is_type_token()) {
+ p_node->type = EXPR_CAST;
+ struct cast_node* cast_node = &p_node->inner.cast;
+ parse_type_ref(&cast_node->type);
+ expect(TK_RPAREN);
+ cast_node->expr = ccc_alloc(sizeof(struct expr_node));
+ parse_expr(cast_node->expr);
+ } else {
+ p_node->type = EXPR_PAREN;
+ struct paren_node* paren_node = &p_node->inner.paren;
+ paren_node->expr_list = ccc_alloc(sizeof(struct expr_list_node));
+ parse_expr_list(paren_node->expr_list);
+ expect(TK_RPAREN);
+ }
}
static void parse_expr(struct expr_node* p_node) {
@@ -281,7 +305,7 @@ static void parse_expr(struct expr_node* p_node) {
switch (tok.type) {
case TK_LPAREN:
p_node->type = EXPR_PAREN;
- parse_paren(&p_node->inner.paren);
+ parse_paren(p_node);
break;
case TK_NEG:
p_node->type = EXPR_UNARY;
@@ -432,18 +456,6 @@ static void parse_while(struct loop_node* p_node) {
scope_pop(&scope);
}
-static bool is_type_token() {
- if (tok.type != TK_IDENT) return false;
-
- /* check primitive types */
- for (integral_t i = 0; primitive_types[i] != NULL; i++) {
- if (strcmp(tok.data.ident, primitive_types[i]->name) == 0) return true;
- }
-
- /* check type aliases */
- return scope_get_type(scope, NULL, tok.data.ident);
-}
-
static void parse_loop_init(struct loop_init_node* p_node) {
peek_or_panic();
if (is_type_token()) {