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/CodeGen/CodeGen.cc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'lib/CodeGen/CodeGen.cc') diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc index 8c01972..ce34ebc 100644 --- a/lib/CodeGen/CodeGen.cc +++ b/lib/CodeGen/CodeGen.cc @@ -185,6 +185,8 @@ const TypeInfo *CodeGen::getExprTypeInfo(ExprAST *Expr) const { if (dynamic_cast(Expr)) return resolveType("BOOLEAN"); if (dynamic_cast(Expr)) return resolveType("CHAR"); if (dynamic_cast(Expr)) return resolveType("STRING"); + if (auto *Cast = dynamic_cast(Expr)) + return resolveType(Cast->getTypeName()); if (auto *Var = dynamic_cast(Expr)) { const SymbolInfo *Info = getSymbolInfo(Var->getName()); @@ -240,7 +242,10 @@ const TypeInfo *CodeGen::getExprTypeInfo(ExprAST *Expr) const { if (Unary->getOp() == tok_not) { return resolveType("BOOLEAN"); } - return getExprTypeInfo(Unary->getOperand()); + const TypeInfo *Operand = getExprTypeInfo(Unary->getOperand()); + if (Operand && !Operand->isC() && !Operand->isUserKind()) + return Operand->isReal() ? resolveType("REAL") : resolveType("INTEGER"); + return nullptr; } if (auto *Bin = dynamic_cast(Expr)) { @@ -572,6 +577,7 @@ void CodeGen::print(raw_ostream &Output) { } Value *CodeGen::emitExpr(ExprAST *Expr) { + if (auto *Cast = dynamic_cast(Expr)) return emitCast(Cast); if (!Expr) return nullptr; if (auto *Num = dynamic_cast(Expr)) { @@ -632,6 +638,17 @@ Value *CodeGen::emitExpr(ExprAST *Expr) { if (!Operand) return nullptr; return Builder->CreateNot(Operand, "nottmp"); } + if (Unary->getOp() == '-') { + const TypeInfo *Info = getExprTypeInfo(Unary->getOperand()); + if (!Info || Info->isC() || Info->isUserKind() || Info->isString() || Info->isVoid()) { + reportError("Unary '-' requires a supported numeric operand"); + return nullptr; + } + Value *Operand = emitExpr(Unary->getOperand()); + if (!Operand) return nullptr; + return ArithHandler->emitBinaryOp('-', ConstantInt::get(Type::getInt64Ty(*TheContext), 0), + Operand, 0); + } return nullptr; } -- cgit v1.3.1-4-g156e