aboutsummaryrefslogtreecommitdiff
path: root/lib/Parser
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-14 03:31:47 +0000
committer_ov4 <ov4@ov4.org>2026-10-14 03:31:47 +0000
commit2147449f4d6f37b4624990ff26bce58ebf3cfb4b (patch)
treebbf09cea539b8ea01ee3d392ec828067552fa4b7 /lib/Parser
parent699e18b65b07af73ccd14b28f83572b1843c7d37 (diff)
feat(parser): add explicit parenthesized type casts
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Diffstat (limited to 'lib/Parser')
-rw-r--r--lib/Parser/Parser.cc21
-rw-r--r--lib/Parser/ParserType.cc1
2 files changed, 19 insertions, 3 deletions
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc
index 3bc095e..db2790b 100644
--- a/lib/Parser/Parser.cc
+++ b/lib/Parser/Parser.cc
@@ -155,6 +155,23 @@ std::string Parser::ParseTypeName(bool AllowVoid) {
std::unique_ptr<ExprAST> Parser::ParseParenExpr() {
getNextToken();
+ bool IsType = CurTok == tok_integer_kw || CurTok == tok_real_kw ||
+ CurTok == tok_boolean_kw || CurTok == tok_string_kw || CurTok == tok_char_kw ||
+ CurTok == tok_c_int || CurTok == tok_c_char || CurTok == tok_c_string ||
+ CurTok == tok_c_double ||
+ (CurTok == tok_identifier && TypeNames.count(Lex.IdentifierStr));
+ if (IsType) {
+ std::string TypeName = ParseTypeName();
+ if (TypeName.empty()) return nullptr;
+ if (CurTok != ')') {
+ fprintf(stderr, "error: Expected ')' after cast type at line %d\n", Lex.getLine());
+ return nullptr;
+ }
+ getNextToken();
+ auto Operand = ParseUnary();
+ if (!Operand) return nullptr;
+ return std::make_unique<CastExprAST>(TypeName, std::move(Operand));
+ }
auto V = ParseExpression();
if (!V) return nullptr;
if (CurTok != ')') {
@@ -226,12 +243,10 @@ std::unique_ptr<ExprAST> Parser::ParseUnary() {
}
if (CurTok == '-') {
- int Line = Lex.getLine();
getNextToken();
auto Operand = ParseUnary();
if (!Operand) return nullptr;
- auto Zero = std::make_unique<IntegerExprAST>(0);
- return std::make_unique<BinaryExprAST>('-', std::move(Zero), std::move(Operand), Line);
+ return std::make_unique<UnaryExprAST>('-', std::move(Operand));
}
return ParsePrimary();
diff --git a/lib/Parser/ParserType.cc b/lib/Parser/ParserType.cc
index e249c4c..de1f3ea 100644
--- a/lib/Parser/ParserType.cc
+++ b/lib/Parser/ParserType.cc
@@ -101,6 +101,7 @@ std::unique_ptr<StmtAST> Parser::ParseTypeDecl() {
fprintf(stderr, "error: %s declarations are only allowed at top level at line %d\n", Keyword, Line);
return nullptr;
}
+ TypeNames.insert(Name);
return Decl;
}