From 4113cec4ecaef37eaa5638592bdc80ad7783d530 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 21 Jul 2025 06:23:15 +0900 Subject: feat: implement substraction between pointers --- main.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 48f391c..ef19587 100644 --- a/main.c +++ b/main.c @@ -1459,8 +1459,15 @@ AstNode* parse_additive_expr(Parser* p) { next_token(p); rhs = parse_multiplicative_expr(p); if (lhs->ty->kind == TypeKind_ptr) { - lhs = ast_new_binary_expr( - op, lhs, ast_new_binary_expr(TokenKind_star, rhs, ast_new_int(type_sizeof(lhs->ty->to)))); + if (rhs->ty->kind == TypeKind_ptr) { + // (a - b) / sizeof(a) + lhs = ast_new_binary_expr(TokenKind_slash, ast_new_binary_expr(op, lhs, rhs), + ast_new_int(type_sizeof(lhs->ty->to))); + } else { + // a - b*sizeof(a) + lhs = ast_new_binary_expr( + op, lhs, ast_new_binary_expr(TokenKind_star, rhs, ast_new_int(type_sizeof(lhs->ty->to)))); + } } else { lhs = ast_new_binary_expr(op, lhs, rhs); } -- cgit v1.2.3-70-g09d2