예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        public void Instruction2()
        {
            var instruction = new ConditionalInstructionNode("Y", "A", 0)
            {
                Label = new Label("A")
            };

            AssertEqualCodified(instruction, 29);
        }
예제 #4
0
        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);
        }
예제 #5
0
        public void Instruction3()
        {
            var instruction = new ConditionalInstructionNode("X", "A", 0);

            AssertEqualCodified(instruction, 46);
        }