implemented type casting parsing.
This commit is contained in:
parent
26ae74cc87
commit
403b2b7161
3 changed files with 18 additions and 1 deletions
4
lc.c
4
lc.c
|
|
@ -68,6 +68,10 @@ void print_ast(ast_node *node, int depth) {
|
||||||
case NODE_IDENTIFIER:
|
case NODE_IDENTIFIER:
|
||||||
printf("Identifier: %.*s\n", (int)node->expr.string.len, node->expr.string.start);
|
printf("Identifier: %.*s\n", (int)node->expr.string.len, node->expr.string.start);
|
||||||
break;
|
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:
|
case NODE_BINARY:
|
||||||
printf("BinaryOp (%s)\n", get_op_str(node->expr.binary.operator));
|
printf("BinaryOp (%s)\n", get_op_str(node->expr.binary.operator));
|
||||||
print_ast(node->expr.binary.left, depth + 1);
|
print_ast(node->expr.binary.left, depth + 1);
|
||||||
|
|
|
||||||
13
parser.c
13
parser.c
|
|
@ -177,6 +177,19 @@ ast_node *parse_unary(parser *p)
|
||||||
return node;
|
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:
|
end:
|
||||||
return parse_factor(p);
|
return parse_factor(p);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
test.c
2
test.c
|
|
@ -1 +1 @@
|
||||||
hello+3-(--ciao)
|
(u64) x
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue