From 2147449f4d6f37b4624990ff26bce58ebf3cfb4b Mon Sep 17 00:00:00 2001 From: _ov4 Date: Wed, 14 Oct 2026 03:31:47 +0000 Subject: feat(parser): add explicit parenthesized type casts Co-Authored-By: Claude Fable 5.1 --- lib/Parser/Parser.cc | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'lib/Parser/Parser.cc') 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 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(TypeName, std::move(Operand)); + } auto V = ParseExpression(); if (!V) return nullptr; if (CurTok != ')') { @@ -226,12 +243,10 @@ std::unique_ptr Parser::ParseUnary() { } if (CurTok == '-') { - int Line = Lex.getLine(); getNextToken(); auto Operand = ParseUnary(); if (!Operand) return nullptr; - auto Zero = std::make_unique(0); - return std::make_unique('-', std::move(Zero), std::move(Operand), Line); + return std::make_unique('-', std::move(Operand)); } return ParsePrimary(); -- cgit v1.3.1-4-g156e