From 6196bdfe5610e948366e052282bc73c56e7423fc Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Fri, 16 Oct 2026 00:03:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(gi):=20=E6=A8=A1=E5=9D=97=E7=BA=A7=E8=B4=9F?= =?UTF-8?q?=E6=B5=AE=E7=82=B9=E5=AD=97=E9=9D=A2=E9=87=8F=E6=8C=89=E6=B5=AE?= =?UTF-8?q?=E7=82=B9=E5=88=9D=E5=A7=8B=E5=8C=96(=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E9=80=81=E6=95=B4=E5=9E=8B=E6=8F=90=E5=8F=96=E5=99=A8)=20-=20?= =?UTF-8?q?=E4=BF=AE=20double=20=3D=20-100.0=20=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/lib/core/Gen/TranslatorGenMixin.vp | 28 ++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/App/lib/core/Gen/TranslatorGenMixin.vp b/App/lib/core/Gen/TranslatorGenMixin.vp index 6b9053c..fea52f2 100644 --- a/App/lib/core/Gen/TranslatorGenMixin.vp +++ b/App/lib/core/Gen/TranslatorGenMixin.vp @@ -610,8 +610,18 @@ novirtual class TranslatorGenMixin: init_sym_node = uop_g.operand init_kind = 5 else: - init_val = self.Gen.AssignGen.extract_cdefine_int_value(aa.value) - init_kind = 1 + # 负浮点字面量 `-100.0` = UnaryOp(USub, Constant(float)):按**浮点初始化** + # 处理(init_kind=4,值稍后从源码重读,故负号会被保留); + # 否则会被送进整型常量提取器 ⇒ TypeError: CDefine value extraction failed。 + is_neg_f: int = 0 + if uop_g.op == ast.OpKind.USub and uop_g.operand is not None and uop_g.operand.kind() == ast.ASTKind.Constant: + opf_d: ast.Constant* = ast.Constant*(uop_g.operand) + if opf_d.const_kind == ast.CONST_FLOAT: is_neg_f = 1 + if is_neg_f != 0: + init_kind = 4 + else: + init_val = self.Gen.AssignGen.extract_cdefine_int_value(aa.value) + init_kind = 1 elif val_kind == ast.ASTKind.List: init_list_node = ast.List*(aa.value) init_kind = 2 @@ -648,8 +658,18 @@ novirtual class TranslatorGenMixin: init_sym_node = uop_g2.operand init_kind = 5 else: - init_val = self.Gen.AssignGen.extract_cdefine_int_value(asgn.value) - init_kind = 1 + # 同 AnnAssign 分支:负浮点字面量按**浮点初始化**处理;无注解时还要把类型 + # 推断为 double(否则默认 i32),否则 value 会被送进整型常量提取器 ⇒ fatal。 + is_neg_f2: int = 0 + if uop_g2.op == ast.OpKind.USub and uop_g2.operand is not None and uop_g2.operand.kind() == ast.ASTKind.Constant: + opf_d2: ast.Constant* = ast.Constant*(uop_g2.operand) + if opf_d2.const_kind == ast.CONST_FLOAT: is_neg_f2 = 1 + if is_neg_f2 != 0: + init_kind = 4 + var_ty = mlirlite.Double(pool) + else: + init_val = self.Gen.AssignGen.extract_cdefine_int_value(asgn.value) + init_kind = 1 elif val_kind2 == ast.ASTKind.List: init_list_node = ast.List*(asgn.value) init_kind = 2