diff --git a/compiler/src/Codegen.cpp b/compiler/src/Codegen.cpp index 9d9c4a1..f4a824c 100644 --- a/compiler/src/Codegen.cpp +++ b/compiler/src/Codegen.cpp @@ -590,6 +590,15 @@ namespace { E_rr(f, "NOT", rd, t); return true; } + if (e.kind == ExprKind::Neg) { + const uint8_t t = alloc_temp(f); + if (!compile_expr(f, *e.operand, t)) { + return false; + } + const size_t s = E_rr(f, "NEG", rd, t); + set_func(f.code.data() + s, expr_func(f, *e.operand, *e.operand)); + return true; + } if (e.kind == ExprKind::And || e.kind == ExprKind::Or) { if (!compile_expr(f, *e.lhs, rd)) { return false; diff --git a/compiler/src/Parser.cpp b/compiler/src/Parser.cpp index cf6e684..d918be2 100644 --- a/compiler/src/Parser.cpp +++ b/compiler/src/Parser.cpp @@ -919,6 +919,26 @@ namespace { e->int_value = cur().int_value; advance(); return true; + case Tok::FLOAT_LIT: + e->kind = ExprKind::LitReal; + e->double_value = cur().double_value; + advance(); + return true; + case Tok::DATE_LIT: + e->kind = ExprKind::LitDate; + e->int_value = cur().int_value; + advance(); + return true; + case Tok::TOD_LIT: + e->kind = ExprKind::LitTod; + e->int_value = cur().int_value; + advance(); + return true; + case Tok::DT_LIT: + e->kind = ExprKind::LitDt; + e->int_value = cur().int_value; + advance(); + return true; case Tok::TRUE: e->kind = ExprKind::LitBool; e->int_value = 1; diff --git a/compiler/src/Typecheck.cpp b/compiler/src/Typecheck.cpp index b1839f2..50f652f 100644 --- a/compiler/src/Typecheck.cpp +++ b/compiler/src/Typecheck.cpp @@ -243,6 +243,19 @@ namespace { * @return true 值域内;false(err 已写) */ bool literal_range_ok(const std::string& file, const Expr& e, TType want) { + // 一元负 + 字面量:折叠负号后按负值检查(-1 赋无符号 → 越域) + if (e.kind == ExprKind::Neg && e.operand != nullptr && + (e.operand->kind == ExprKind::LitInt || + e.operand->kind == ExprKind::LitReal)) { + Expr folded; + folded.kind = e.operand->kind; + if (e.operand->kind == ExprKind::LitInt) { + folded.int_value = -e.operand->int_value; + } else { + folded.double_value = -e.operand->double_value; + } + return literal_range_ok(file, folded, want); + } if (e.kind == ExprKind::LitInt) { if (want == TType::Real || want == TType::Lreal) { return true; // 整数字面量 → 浮点目标合法