diff --git a/lc.c b/lc.c index 3cb4539..d3eae80 100644 --- a/lc.c +++ b/lc.c @@ -68,6 +68,10 @@ void print_ast(ast_node *node, int depth) { case NODE_IDENTIFIER: printf("Identifier: %.*s\n", (int)node->expr.string.len, node->expr.string.start); break; + case NODE_CAST: + printf("Cast: %.*s\n", (int)node->expr.cast.type_len, node->expr.cast.type); + print_ast(node->expr.cast.value, depth + 1); + break; case NODE_BINARY: printf("BinaryOp (%s)\n", get_op_str(node->expr.binary.operator)); print_ast(node->expr.binary.left, depth + 1); diff --git a/parser.c b/parser.c index 0a7fe32..6c5b348 100644 --- a/parser.c +++ b/parser.c @@ -177,6 +177,19 @@ ast_node *parse_unary(parser *p) return node; } + /* Type cast. */ + if (match_peek(p, TOKEN_LPAREN) && p->tokens->next && p->tokens->next->type == TOKEN_IDENTIFIER && p->tokens->next->next && p->tokens->next->next->type == TOKEN_RPAREN) { + advance(p); + ast_node *node = arena_alloc(p->allocator, sizeof(ast_node)); + node->type = NODE_CAST; + node->expr.cast.type = peek(p)->lexeme; + node->expr.cast.type_len = peek(p)->lexeme_len; + advance(p); + advance(p); + node->expr.cast.value = parse_expression(p); + return node; + } + end: return parse_factor(p); } diff --git a/test.c b/test.c index dbfa1f4..955b7f9 100644 --- a/test.c +++ b/test.c @@ -1 +1 @@ -hello+3-(--ciao) +(u64) x