修改标识符

This commit is contained in:
Luke 2025-05-07 19:52:50 +08:00
parent 670f0560bd
commit de21808c2c
3 changed files with 24 additions and 22 deletions

View File

@ -21,24 +21,24 @@ public class FunctionBuilder {
/**
* 构建函数的 IR 表示
*
* @param fn 要构建的函数 AST 节点
* @param functionNode 要构建的函数 AST 节点
* @return 构建完成的 IRFunction 对象
*/
public IRFunction build(FunctionNode fn) {
public IRFunction build(FunctionNode functionNode) {
// 创建 IRFunction 并包装到上下文
IRFunction irFunction = new IRFunction(fn.name());
IRFunction irFunction = new IRFunction(functionNode.name());
IRContext irContext = new IRContext(irFunction);
// 声明函数参数
for (ParameterNode parameterNode : fn.parameters()) {
for (ParameterNode parameterNode : functionNode.parameters()) {
// 在当前作用域中声明参数名称对应的虚拟寄存器
irContext.getScope().declare(parameterNode.name());
}
// 使用 StatementBuilder 遍历并生成函数体所有语句的 IR
StatementBuilder sb = new StatementBuilder(irContext);
for (StatementNode statementNode : fn.body()) {
sb.build(statementNode);
StatementBuilder statementBuilder = new StatementBuilder(irContext);
for (StatementNode statementNode : functionNode.body()) {
statementBuilder.build(statementNode);
}
// 返回构建好的 IRFunction

View File

@ -5,6 +5,7 @@ import org.jcnc.snow.compiler.parser.ast.AssignmentNode;
import org.jcnc.snow.compiler.parser.ast.DeclarationNode;
import org.jcnc.snow.compiler.parser.ast.ExpressionStatementNode;
import org.jcnc.snow.compiler.parser.ast.ReturnNode;
import org.jcnc.snow.compiler.parser.ast.base.ExpressionNode;
import org.jcnc.snow.compiler.parser.ast.base.StatementNode;
/**
@ -22,18 +23,18 @@ import org.jcnc.snow.compiler.parser.ast.base.StatementNode;
*/
public class StatementBuilder {
/** 当前的 IR 上下文,用于生成指令和管理作用域 */
private final IRContext ctx;
private final IRContext irContext;
/** 用于生成表达式对应的 IR 寄存器和指令的构建器 */
private final ExpressionBuilder exprBuilder;
/**
* 构造函数初始化 IR 上下文并创建 ExpressionBuilder 实例
*
* @param ctx IR 上下文对象包含当前函数的作用域指令列表等信息
* @param irContext IR 上下文对象包含当前函数的作用域指令列表等信息
*/
public StatementBuilder(IRContext ctx) {
this.ctx = ctx;
this.exprBuilder = new ExpressionBuilder(ctx);
public StatementBuilder(IRContext irContext) {
this.irContext = irContext;
this.exprBuilder = new ExpressionBuilder(irContext);
}
/**
@ -51,21 +52,21 @@ public class StatementBuilder {
public void build(StatementNode statementNode) {
// 表达式语句: 仅生成表达式对应的 IR 指令不关注结果
if (statementNode instanceof ExpressionStatementNode(
org.jcnc.snow.compiler.parser.ast.base.ExpressionNode expression)) {
ExpressionNode expression)) {
exprBuilder.build(expression);
return;
}
// 赋值语句: 先生成右侧表达式指令获取寄存器再声明或更新变量的值绑定
if (statementNode instanceof AssignmentNode(
String variable, org.jcnc.snow.compiler.parser.ast.base.ExpressionNode value
String variable, ExpressionNode value
)) {
IRVirtualRegister irVirtualRegister = exprBuilder.build(value);
if (ctx.getScope().lookup(variable) == null) {
if (irContext.getScope().lookup(variable) == null) {
// 首次赋值声明变量并绑定寄存器
ctx.getScope().declare(variable, irVirtualRegister);
irContext.getScope().declare(variable, irVirtualRegister);
} else {
// 变量已存在更新绑定的寄存器
ctx.getScope().put(variable, irVirtualRegister);
irContext.getScope().put(variable, irVirtualRegister);
}
return;
}
@ -73,19 +74,19 @@ public class StatementBuilder {
if (statementNode instanceof DeclarationNode declarationNode) {
if (declarationNode.getInitializer().isPresent()) {
IRVirtualRegister irVirtualRegister = exprBuilder.build(declarationNode.getInitializer().get());
ctx.getScope().declare(declarationNode.getName(), irVirtualRegister);
irContext.getScope().declare(declarationNode.getName(), irVirtualRegister);
} else {
ctx.getScope().declare(declarationNode.getName());
irContext.getScope().declare(declarationNode.getName());
}
return;
}
// 返回语句: 区分有返回值和无返回值两种情况
if (statementNode instanceof ReturnNode returnNode) {
if (returnNode.getExpression().isPresent()) {
IRVirtualRegister vr = exprBuilder.build(returnNode.getExpression().get());
InstructionFactory.ret(ctx, vr);
IRVirtualRegister irVirtualRegister = exprBuilder.build(returnNode.getExpression().get());
InstructionFactory.ret(irContext, irVirtualRegister);
} else {
InstructionFactory.retVoid(ctx);
InstructionFactory.retVoid(irContext);
}
return;
}

View File

@ -5,6 +5,7 @@ import org.jcnc.snow.compiler.ir.value.IRVirtualRegister;
import java.util.List;
/**
*
* IRInstruction 所有 IR 指令的抽象基类Abstract Base Class
* <p>
* 每一条 IR 指令都至少具备以下基本属性