From 2047e97655d5635eff00ba540bc3f4fe6db1203e Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 11 Jun 2025 22:12:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E5=AF=B9=E5=B8=83?= =?UTF-8?q?=E5=B0=94=E5=8F=98=E9=87=8F=E5=A3=B0=E6=98=8E=E5=B9=B6=E8=B5=8B?= =?UTF-8?q?=E5=80=BC=E7=9A=84=E5=AE=8C=E6=95=B4=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- playground/Demo4/Main.snow | 6 ++-- .../backend/generator/LoadConstGenerator.java | 1 + .../ir/builder/ExpressionBuilder.java | 28 +++++++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/playground/Demo4/Main.snow b/playground/Demo4/Main.snow index 1641146..850bfc9 100644 --- a/playground/Demo4/Main.snow +++ b/playground/Demo4/Main.snow @@ -1,11 +1,11 @@ module: Main function: main parameter: - return_type: int + return_type: boolean body: - declare b1: boolean + declare b1: boolean =true - return 0 + return b1 end body end function end module diff --git a/src/main/java/org/jcnc/snow/compiler/backend/generator/LoadConstGenerator.java b/src/main/java/org/jcnc/snow/compiler/backend/generator/LoadConstGenerator.java index 8c8ee32..9e75f22 100644 --- a/src/main/java/org/jcnc/snow/compiler/backend/generator/LoadConstGenerator.java +++ b/src/main/java/org/jcnc/snow/compiler/backend/generator/LoadConstGenerator.java @@ -64,6 +64,7 @@ public class LoadConstGenerator implements InstructionGenerator 'B'; // 字节型 case Double _ -> 'D'; // 双精度浮点型 case Float _ -> 'F'; // 单精度浮点型 + case Boolean _ -> 'I'; // 布尔(作为 0/1 整型存储) case null, default -> throw new IllegalStateException("Unknown const type: " + (value != null ? value.getClass() : null)); }; diff --git a/src/main/java/org/jcnc/snow/compiler/ir/builder/ExpressionBuilder.java b/src/main/java/org/jcnc/snow/compiler/ir/builder/ExpressionBuilder.java index a68120f..a658d9d 100644 --- a/src/main/java/org/jcnc/snow/compiler/ir/builder/ExpressionBuilder.java +++ b/src/main/java/org/jcnc/snow/compiler/ir/builder/ExpressionBuilder.java @@ -44,23 +44,27 @@ public record ExpressionBuilder(IRContext ctx) { */ public IRVirtualRegister build(ExpressionNode expr) { return switch (expr) { - // 数字字面量,如 "123", "1.0f" + // 数字字面量 case NumberLiteralNode n -> buildNumberLiteral(n.value()); - // 标识符,如变量x,直接查作用域 + // 布尔字面量 + case BoolLiteralNode b -> buildBoolLiteral(b.getValue()); + // 标识符 case IdentifierNode id -> { IRVirtualRegister reg = ctx.getScope().lookup(id.name()); - if (reg == null) throw new IllegalStateException("未定义标识符: " + id.name()); + if (reg == null) + throw new IllegalStateException("未定义标识符: " + id.name()); yield reg; } - // 二元表达式,如 "a + b" + // 二元表达式 case BinaryExpressionNode bin -> buildBinary(bin); - // 函数调用,如 foo(a, b) + // 函数调用 case CallExpressionNode call -> buildCall(call); - // 其它不支持 - default -> throw new IllegalStateException("不支持的表达式类型: " + expr.getClass().getSimpleName()); + default -> throw new IllegalStateException( + "不支持的表达式类型: " + expr.getClass().getSimpleName()); }; } + /** * 直接将表达式计算结果写入指定的目标寄存器(dest)。 *

@@ -91,8 +95,6 @@ public record ExpressionBuilder(IRContext ctx) { } } - // ===================== 内部私有方法 ===================== - /** * 构建二元表达式的IR,生成新寄存器存储结果。 *

@@ -179,4 +181,12 @@ public record ExpressionBuilder(IRContext ctx) { ctx.addInstruction(new LoadConstInstruction(reg, constant)); return reg; } + + /** 布尔字面量 → CONST (true=1,false=0)*/ + private IRVirtualRegister buildBoolLiteral(boolean value) { + IRConstant constant = new IRConstant(value ? 1 : 0); + IRVirtualRegister reg = ctx.newRegister(); + ctx.addInstruction(new LoadConstInstruction(reg, constant)); + return reg; + } }