修改标识符
This commit is contained in:
parent
670f0560bd
commit
de21808c2c
@ -21,24 +21,24 @@ public class FunctionBuilder {
|
|||||||
/**
|
/**
|
||||||
* 构建函数的 IR 表示
|
* 构建函数的 IR 表示
|
||||||
*
|
*
|
||||||
* @param fn 要构建的函数 AST 节点
|
* @param functionNode 要构建的函数 AST 节点
|
||||||
* @return 构建完成的 IRFunction 对象
|
* @return 构建完成的 IRFunction 对象
|
||||||
*/
|
*/
|
||||||
public IRFunction build(FunctionNode fn) {
|
public IRFunction build(FunctionNode functionNode) {
|
||||||
// 创建 IRFunction 并包装到上下文
|
// 创建 IRFunction 并包装到上下文
|
||||||
IRFunction irFunction = new IRFunction(fn.name());
|
IRFunction irFunction = new IRFunction(functionNode.name());
|
||||||
IRContext irContext = new IRContext(irFunction);
|
IRContext irContext = new IRContext(irFunction);
|
||||||
|
|
||||||
// 声明函数参数
|
// 声明函数参数
|
||||||
for (ParameterNode parameterNode : fn.parameters()) {
|
for (ParameterNode parameterNode : functionNode.parameters()) {
|
||||||
// 在当前作用域中声明参数名称对应的虚拟寄存器
|
// 在当前作用域中声明参数名称对应的虚拟寄存器
|
||||||
irContext.getScope().declare(parameterNode.name());
|
irContext.getScope().declare(parameterNode.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用 StatementBuilder 遍历并生成函数体所有语句的 IR
|
// 使用 StatementBuilder 遍历并生成函数体所有语句的 IR
|
||||||
StatementBuilder sb = new StatementBuilder(irContext);
|
StatementBuilder statementBuilder = new StatementBuilder(irContext);
|
||||||
for (StatementNode statementNode : fn.body()) {
|
for (StatementNode statementNode : functionNode.body()) {
|
||||||
sb.build(statementNode);
|
statementBuilder.build(statementNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回构建好的 IRFunction
|
// 返回构建好的 IRFunction
|
||||||
|
|||||||
@ -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.DeclarationNode;
|
||||||
import org.jcnc.snow.compiler.parser.ast.ExpressionStatementNode;
|
import org.jcnc.snow.compiler.parser.ast.ExpressionStatementNode;
|
||||||
import org.jcnc.snow.compiler.parser.ast.ReturnNode;
|
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;
|
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 {
|
public class StatementBuilder {
|
||||||
/** 当前的 IR 上下文,用于生成指令和管理作用域 */
|
/** 当前的 IR 上下文,用于生成指令和管理作用域 */
|
||||||
private final IRContext ctx;
|
private final IRContext irContext;
|
||||||
/** 用于生成表达式对应的 IR 寄存器和指令的构建器 */
|
/** 用于生成表达式对应的 IR 寄存器和指令的构建器 */
|
||||||
private final ExpressionBuilder exprBuilder;
|
private final ExpressionBuilder exprBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数,初始化 IR 上下文并创建 ExpressionBuilder 实例。
|
* 构造函数,初始化 IR 上下文并创建 ExpressionBuilder 实例。
|
||||||
*
|
*
|
||||||
* @param ctx IR 上下文对象,包含当前函数的作用域、指令列表等信息
|
* @param irContext IR 上下文对象,包含当前函数的作用域、指令列表等信息
|
||||||
*/
|
*/
|
||||||
public StatementBuilder(IRContext ctx) {
|
public StatementBuilder(IRContext irContext) {
|
||||||
this.ctx = ctx;
|
this.irContext = irContext;
|
||||||
this.exprBuilder = new ExpressionBuilder(ctx);
|
this.exprBuilder = new ExpressionBuilder(irContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,21 +52,21 @@ public class StatementBuilder {
|
|||||||
public void build(StatementNode statementNode) {
|
public void build(StatementNode statementNode) {
|
||||||
// 表达式语句: 仅生成表达式对应的 IR 指令,不关注结果
|
// 表达式语句: 仅生成表达式对应的 IR 指令,不关注结果
|
||||||
if (statementNode instanceof ExpressionStatementNode(
|
if (statementNode instanceof ExpressionStatementNode(
|
||||||
org.jcnc.snow.compiler.parser.ast.base.ExpressionNode expression)) {
|
ExpressionNode expression)) {
|
||||||
exprBuilder.build(expression);
|
exprBuilder.build(expression);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 赋值语句: 先生成右侧表达式指令,获取寄存器,再声明或更新变量的值绑定
|
// 赋值语句: 先生成右侧表达式指令,获取寄存器,再声明或更新变量的值绑定
|
||||||
if (statementNode instanceof AssignmentNode(
|
if (statementNode instanceof AssignmentNode(
|
||||||
String variable, org.jcnc.snow.compiler.parser.ast.base.ExpressionNode value
|
String variable, ExpressionNode value
|
||||||
)) {
|
)) {
|
||||||
IRVirtualRegister irVirtualRegister = exprBuilder.build(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 {
|
} else {
|
||||||
// 变量已存在:更新绑定的寄存器
|
// 变量已存在:更新绑定的寄存器
|
||||||
ctx.getScope().put(variable, irVirtualRegister);
|
irContext.getScope().put(variable, irVirtualRegister);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -73,19 +74,19 @@ public class StatementBuilder {
|
|||||||
if (statementNode instanceof DeclarationNode declarationNode) {
|
if (statementNode instanceof DeclarationNode declarationNode) {
|
||||||
if (declarationNode.getInitializer().isPresent()) {
|
if (declarationNode.getInitializer().isPresent()) {
|
||||||
IRVirtualRegister irVirtualRegister = exprBuilder.build(declarationNode.getInitializer().get());
|
IRVirtualRegister irVirtualRegister = exprBuilder.build(declarationNode.getInitializer().get());
|
||||||
ctx.getScope().declare(declarationNode.getName(), irVirtualRegister);
|
irContext.getScope().declare(declarationNode.getName(), irVirtualRegister);
|
||||||
} else {
|
} else {
|
||||||
ctx.getScope().declare(declarationNode.getName());
|
irContext.getScope().declare(declarationNode.getName());
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 返回语句: 区分有返回值和无返回值两种情况
|
// 返回语句: 区分有返回值和无返回值两种情况
|
||||||
if (statementNode instanceof ReturnNode returnNode) {
|
if (statementNode instanceof ReturnNode returnNode) {
|
||||||
if (returnNode.getExpression().isPresent()) {
|
if (returnNode.getExpression().isPresent()) {
|
||||||
IRVirtualRegister vr = exprBuilder.build(returnNode.getExpression().get());
|
IRVirtualRegister irVirtualRegister = exprBuilder.build(returnNode.getExpression().get());
|
||||||
InstructionFactory.ret(ctx, vr);
|
InstructionFactory.ret(irContext, irVirtualRegister);
|
||||||
} else {
|
} else {
|
||||||
InstructionFactory.retVoid(ctx);
|
InstructionFactory.retVoid(irContext);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import org.jcnc.snow.compiler.ir.value.IRVirtualRegister;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* IRInstruction —— 所有 IR 指令的抽象基类(Abstract Base Class)。
|
* IRInstruction —— 所有 IR 指令的抽象基类(Abstract Base Class)。
|
||||||
* <p>
|
* <p>
|
||||||
* 每一条 IR 指令都至少具备以下基本属性:
|
* 每一条 IR 指令都至少具备以下基本属性:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user