我正在为一个学校项目用 C# 做一个编译器,我不禁想知道我将如何在 Haskell 中做到这一点。
例如:
我为 While 循环生成的代码是:
public override void generateCode(Compiler compiler)
{
int jumpToTheBeginningInstructionIndex = compiler.getIndexOfNextActionInCurrentFunction();
MachineInstructions.JMP jumpTotheBeginning = new MachineInstructions.JMP(jumpToTheBeginningInstructionIndex);
MachineInstructions.JMPF jumpToTheEnd = new MachineInstructions.JMPF();
booleanExpression.generateCode(compiler);
//I insert the jump to the end here:
compiler.addAction(jumpToTheEnd);
foreach(IAction action in this.thenActions)
{
action.generateCode(compiler);
}
compiler.addAction(jumpTotheBeginning);
//...But is here where I know where should it jump to:
jumpToTheEnd.whereToJump = compiler.getIndexOfNextActionInCurrentFunction();
}
您可以看到我如何在方法的中间插入 jumpToTheEnd 的代码,但直到我知道跳转所在行的末尾才插入。幸运的是,我保留了指向该跳转的指针,并且可以轻松地在方法的最后设置其 whereToJump 属性。
你将如何在 Haskell 中做到这一点!有什么推荐的教程吗?