aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/parse.c
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-08-31 17:07:52 +0900
committernsfisis <nsfisis@gmail.com>2025-08-31 17:07:52 +0900
commit7bbfa1e32537ea0c5e98c0dbb5f77f7bf342118b (patch)
tree43f46ccc3f2610b342672a55fec82ab7dd74cff5 /src/parse.c
parent8ac323ccf3e7c012912d8f333abf267e32628a0f (diff)
downloadducc-7bbfa1e32537ea0c5e98c0dbb5f77f7bf342118b.tar.gz
ducc-7bbfa1e32537ea0c5e98c0dbb5f77f7bf342118b.tar.zst
ducc-7bbfa1e32537ea0c5e98c0dbb5f77f7bf342118b.zip
feat: support cast expression (any types can be coerced to any types for now)
Diffstat (limited to 'src/parse.c')
-rw-r--r--src/parse.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/parse.c b/src/parse.c
index f65c065..467e702 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -183,6 +183,10 @@ static Token* peek_token(Parser* p) {
return &p->tokens->data[p->pos];
}
+static Token* peek_token2(Parser* p) {
+ return &p->tokens->data[p->pos + 1];
+}
+
static Token* next_token(Parser* p) {
return &p->tokens->data[p->pos++];
}
@@ -644,13 +648,27 @@ static AstNode* parse_prefix_expr(Parser* p) {
return parse_postfix_expr(p);
}
+static AstNode* parse_cast_expr(Parser* p) {
+ if (peek_token(p)->kind == TokenKind_paren_l && is_type_token(p, peek_token2(p))) {
+ next_token(p);
+ Type* ty = parse_type(p);
+ expect(p, TokenKind_paren_r);
+
+ // TODO: check whether the original type can be casted to the result type.
+ AstNode* e = parse_cast_expr(p);
+ e->ty = ty;
+ return e;
+ }
+ return parse_prefix_expr(p);
+}
+
static AstNode* parse_multiplicative_expr(Parser* p) {
- AstNode* lhs = parse_prefix_expr(p);
+ AstNode* lhs = parse_cast_expr(p);
while (1) {
TokenKind op = peek_token(p)->kind;
if (op == TokenKind_star || op == TokenKind_slash || op == TokenKind_percent) {
next_token(p);
- AstNode* rhs = parse_prefix_expr(p);
+ AstNode* rhs = parse_cast_expr(p);
lhs = ast_new_binary_expr(op, lhs, rhs);
} else {
break;