我在玩我的 intcode 计算机实现(从代码 2019 的出现开始),发现当我实现开关(选择要执行的操作)时,它采用了错误的值。
下面的代码演示了这一点。
InstructionPointer有一个值2,opcode有值6意味着OpcodeJumpIfFalse将被使用。函数Jif()被调用得很好,它返回一个值,在我的例子中它返回0. Jif()还修改了 的值InstructionPointer,将其值更改为9。该InstructionPointer会的被增加0(返回值Jif()),我会想到它的价值是9,但它的价值会回去做2。
InstructionPointer += opcode switch
{
OpcodeAdd => Add(),
OpcodeMultiply => Mul(),
OpcodeInput => Inp(),
OpcodeOutput => Out(),
OpcodeJumpIfTrue => Jit(),
OpcodeJumpIfFalse => Jif(),
OpcodeLessThan => Let(),
OpcodeEquals => Equ(),
OpcodeAdjustRelativeBase => Arb(),
OpcodeHalt => End(),
_ => throw new ArgumentOutOfRangeException()
};
Run Code Online (Sandbox Code Playgroud)
显示相同行为的最小示例:
int …Run Code Online (Sandbox Code Playgroud)