- computedGoto编译指示
computedGoto编译指示
calculateGoto 编译指示可用于告诉编译器如何编译在 while true 语句中中的Nim case 语句。 从语法上讲,它必须在循环中用作语句:
- type
- MyEnum = enum
- enumA, enumB, enumC, enumD, enumE
- proc vm() =
- var instructions: array[0..100, MyEnum]
- instructions[2] = enumC
- instructions[3] = enumD
- instructions[4] = enumA
- instructions[5] = enumD
- instructions[6] = enumC
- instructions[7] = enumA
- instructions[8] = enumB
- instructions[12] = enumE
- var pc = 0
- while true:
- {.computedGoto.}
- let instr = instructions[pc]
- case instr
- of enumA:
- echo "yeah A"
- of enumC, enumD:
- echo "yeah CD"
- of enumB:
- echo "yeah B"
- of enumE:
- break
- inc(pc)
- vm()
如示例所示, computedGoto 对解释器最有用。 如果底层后端(C编译器)不支持计算的goto扩展,则简单地忽略编译指示。