diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-14 03:31:47 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-14 03:31:47 +0000 |
| commit | 2147449f4d6f37b4624990ff26bce58ebf3cfb4b (patch) | |
| tree | bbf09cea539b8ea01ee3d392ec828067552fa4b7 /lib/CodeGen/CodeGen.cc | |
| parent | 699e18b65b07af73ccd14b28f83572b1843c7d37 (diff) | |
feat(parser): add explicit parenthesized type casts
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Diffstat (limited to 'lib/CodeGen/CodeGen.cc')
| -rw-r--r-- | lib/CodeGen/CodeGen.cc | 19 |
1 files changed, 18 insertions, 1 deletions
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<BooleanExprAST*>(Expr)) return resolveType("BOOLEAN"); if (dynamic_cast<CharExprAST*>(Expr)) return resolveType("CHAR"); if (dynamic_cast<StringExprAST*>(Expr)) return resolveType("STRING"); + if (auto *Cast = dynamic_cast<CastExprAST*>(Expr)) + return resolveType(Cast->getTypeName()); if (auto *Var = dynamic_cast<VariableExprAST*>(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<BinaryExprAST*>(Expr)) { @@ -572,6 +577,7 @@ void CodeGen::print(raw_ostream &Output) { } Value *CodeGen::emitExpr(ExprAST *Expr) { + if (auto *Cast = dynamic_cast<CastExprAST*>(Expr)) return emitCast(Cast); if (!Expr) return nullptr; if (auto *Num = dynamic_cast<IntegerExprAST*>(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; } |
