public static InstructionNode UncodifyInstructionFormat( int number, Var var) { if (number < 0) { throw new InvalidOperationException(); } InstructionNode fakeInstruction; if (number == 0) { fakeInstruction = new UnaryExpressionInstructionNode( var.ToString(), -1); } else if (number == 1 || number == 2) { fakeInstruction = new BinaryExpressionInstructionNode( var.ToString(), number == 1 ? "+" : "-"); } else { var codifiedLabel = number - 2; var label = UncodifyLabel(codifiedLabel); fakeInstruction = new ConditionalInstructionNode( var.ToString(), label.ToString(), -1); } return(fakeInstruction); }
private static int StepConditional( ConditionalInstructionNode conditional, int instructionPointer, IEnumerable <InstructionNode> instructions) { int newInstructionPointer; var targetVar = GetTargetVar(conditional.Var.Type); int index = GetNormalizedIndex(conditional); if (targetVar[index] != 0) { var goToInstruction = instructions.FirstOrDefault(item => item.Label == conditional.TargetLabel); if (goToInstruction != null) { // TODO avoid array conversion newInstructionPointer = Array.IndexOf(instructions.ToArray(), goToInstruction); } else { // We suppose it's the exit label newInstructionPointer = instructions.Count(); } } else { newInstructionPointer = instructionPointer + 1; } return(newInstructionPointer); }
public void Instruction2() { var instruction = new ConditionalInstructionNode("Y", "A", 0) { Label = new Label("A") }; AssertEqualCodified(instruction, 29); }
public void ConditionalInstructionFormat() { const string rawLabel = "A"; var instruction = new ConditionalInstructionNode("X", rawLabel, 0); var label = new Label(rawLabel); var codifiedLabel = Codifier.Codify(label); AssertEqualCodifiedFormat(instruction, codifiedLabel + 2); }
public void Instruction3() { var instruction = new ConditionalInstructionNode("X", "A", 0); AssertEqualCodified(instruction, 46); }