优化标识符

This commit is contained in:
Luke 2025-05-06 20:41:21 +08:00
parent 1b13c55b0d
commit f886933d7c
3 changed files with 34 additions and 35 deletions

View File

@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager"> <component name="ProjectRunConfigurationManager">
<configuration default="false" name="SnowCompiler" type="Application" factoryName="Application" activateToolWindowBeforeRun="false" nameIsGenerated="true"> <configuration default="false" name="SnowCompiler" type="Application" factoryName="Application" activateToolWindowBeforeRun="false" nameIsGenerated="true">
<option name="ALTERNATIVE_JRE_PATH" value="graalvm-ce-23" /> <option name="ALTERNATIVE_JRE_PATH" value="D:/Downloads/DevDownloads/JDK/graalvm-jdk-23" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" /> <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="MAIN_CLASS_NAME" value="org.jcnc.snow.compiler.cli.SnowCompiler" /> <option name="MAIN_CLASS_NAME" value="org.jcnc.snow.compiler.cli.SnowCompiler" />
<module name="SCompiler" /> <module name="SCompiler" />

View File

@ -1,9 +1,9 @@
package org.jcnc.snow.compiler.builder; package org.jcnc.snow.compiler.builder;
import org.jcnc.snow.compiler.ir.core.IRFunction;
import org.jcnc.snow.compiler.ir.core.IROp; import org.jcnc.snow.compiler.ir.core.IROp;
import org.jcnc.snow.compiler.ir.core.IRProgram; import org.jcnc.snow.compiler.ir.core.IRProgram;
import org.jcnc.snow.compiler.ir.instr.BinOpInstruction; import org.jcnc.snow.compiler.ir.instr.BinOpInstruction;
import org.jcnc.snow.compiler.ir.core.IRFunction;
import org.jcnc.snow.compiler.ir.instr.LoadConstInstruction; import org.jcnc.snow.compiler.ir.instr.LoadConstInstruction;
import org.jcnc.snow.compiler.ir.instr.ReturnInstruction; import org.jcnc.snow.compiler.ir.instr.ReturnInstruction;
import org.jcnc.snow.compiler.ir.value.Constant; import org.jcnc.snow.compiler.ir.value.Constant;
@ -98,10 +98,6 @@ public final class BasicIRBuilder {
// 编译函数体中的每条语句 // 编译函数体中的每条语句
for (StatementNode statementNode : functionNode.body()) stmt(statementNode); for (StatementNode statementNode : functionNode.body()) stmt(statementNode);
// 如果函数末尾没有 return自动添加一个 return无返回值
if (currentFn.body().isEmpty() || !(currentFn.body().getLast() instanceof ReturnInstruction))
currentFn.add(new ReturnInstruction(null));
return currentFn; return currentFn;
} }
@ -110,28 +106,30 @@ public final class BasicIRBuilder {
/** /**
* 编译单个语句节点为 IR 指令 * 编译单个语句节点为 IR 指令
* *
* @param n 语句 AST 节点 * @param statementNode 语句 AST 节点
*/ */
private void stmt(StatementNode n) { private void stmt(StatementNode statementNode) {
switch (n) { switch (statementNode) {
case ExpressionStatementNode expressionStatementNode -> expr(expressionStatementNode.expression()); // 表达式语句只求值无需保存 case ExpressionStatementNode expressionStatementNode ->
expr(expressionStatementNode.expression()); // 表达式语句只求值无需保存
case AssignmentNode assignmentNode -> { // 赋值语句 case AssignmentNode assignmentNode -> { // 赋值语句
VirtualRegister virtualRegister = expr(assignmentNode.value()); VirtualRegister virtualRegister = expr(assignmentNode.value());
variables.put(assignmentNode.variable(), virtualRegister); variables.put(assignmentNode.variable(), virtualRegister);
} }
case DeclarationNode d -> { // 变量声明 case DeclarationNode declarationNode -> { // 变量声明
if (d.getInitializer().isPresent()) { // 有初始化器 if (declarationNode.getInitializer().isPresent()) { // 有初始化器
VirtualRegister init = expr(d.getInitializer().get()); VirtualRegister virtualRegister = expr(declarationNode.getInitializer().get());
variables.put(d.getName(), init); variables.put(declarationNode.getName(), virtualRegister);
} else { // 无初始化器分配空寄存器 } else { // 无初始化器分配空寄存器
variables.put(d.getName(), currentFn.newRegister()); variables.put(declarationNode.getName(), currentFn.newRegister());
} }
} }
case ReturnNode r -> { // 返回语句 case ReturnNode returnNode -> { // 返回语句
if (r.getExpression().isPresent()) currentFn.add(new ReturnInstruction(expr(r.getExpression().get()))); if (returnNode.getExpression().isPresent())
currentFn.add(new ReturnInstruction(expr(returnNode.getExpression().get())));
else currentFn.add(new ReturnInstruction(null)); else currentFn.add(new ReturnInstruction(null));
} }
default -> throw new IllegalStateException("不支持的语句: " + n); default -> throw new IllegalStateException("不支持的语句: " + statementNode);
} }
} }
@ -140,24 +138,25 @@ public final class BasicIRBuilder {
/** /**
* 编译单个表达式节点结果保存在一个新的寄存器中 * 编译单个表达式节点结果保存在一个新的寄存器中
* *
* @param e 表达式 AST 节点 * @param expressionNode 表达式 AST 节点
* @return 保存结果的虚拟寄存器 * @return 保存结果的虚拟寄存器
*/ */
private VirtualRegister expr(ExpressionNode e) { private VirtualRegister expr(ExpressionNode expressionNode) {
return switch (e) { return switch (expressionNode) {
case BinaryExpressionNode b -> bin(b); // 二元运算 case BinaryExpressionNode binaryExpressionNode -> bin(binaryExpressionNode); // 二元运算
case NumberLiteralNode n -> number(n); // 数字字面量 case NumberLiteralNode numberLiteralNode -> number(numberLiteralNode); // 数字字面量
case IdentifierNode id -> { // 变量引用 case IdentifierNode identifierNode -> { // 变量引用
VirtualRegister v = variables.get(id.name()); VirtualRegister virtualRegister = variables.get(identifierNode.name());
if (v == null) throw new IllegalStateException("变量 " + id.name() + " 未定义"); if (virtualRegister == null)
yield v; throw new IllegalStateException("变量 " + identifierNode.name() + " 未定义");
yield virtualRegister;
} }
default -> throw new IllegalStateException("不支持的表达式: " + e); default -> throw new IllegalStateException("不支持的表达式: " + expressionNode);
}; };
} }
/** /**
* 编译一个二元运算表达式如加减乘除 * 二元运算表达式如加减乘除
* *
* @param b 二元表达式节点 * @param b 二元表达式节点
* @return 保存结果的寄存器 * @return 保存结果的寄存器
@ -183,12 +182,12 @@ public final class BasicIRBuilder {
/** /**
* 编译一个整数字面量 * 编译一个整数字面量
* *
* @param n 数字字面量节点 * @param numberLiteralNode 数字字面量节点
* @return 保存常量的寄存器 * @return 保存常量的寄存器
*/ */
private VirtualRegister number(NumberLiteralNode n) { private VirtualRegister number(NumberLiteralNode numberLiteralNode) {
VirtualRegister d = currentFn.newRegister(); // 分配一个新的寄存器 VirtualRegister virtualRegister = currentFn.newRegister(); // 分配一个新的寄存器
currentFn.add(new LoadConstInstruction(d, new Constant(Integer.parseInt(n.value())))); // 加载常量指令 currentFn.add(new LoadConstInstruction(virtualRegister, new Constant(Integer.parseInt(numberLiteralNode.value())))); // 加载常量指令
return d; return virtualRegister;
} }
} }

2
test
View File

@ -8,7 +8,7 @@ module: CommonTasks
body: body:
num1 = 10 num1 = 10
num2 = 20 num2 = 20
declare result:int = num1+num2 declare result:int = num1+num2+1
end body end body
end function end function
end module end module