public void InstructionContainer_IncVal_DecVal()
 {
     var instruction = new InstructionContainer(new []
                 {
                     new IncrementCurrentValue(1), new IncrementCurrentValue(-1)
                 });
     var m = new MockGenerator();
     instruction.Emit(m);
     Assert.AreEqual(1, m.ValueDecrements);
     Assert.AreEqual(1, m.ValueIncrements);
 }
Exemplo n.º 2
0
        private static void BrainmessCompiler(string outputPath, string program)
        {
            var generator = new BrainmessIlGenerator(outputPath,program.Count(x=>x=='['),5000);
            var containerStack = new Stack<List<IInstruction>>();
            containerStack.Push(new List<IInstruction>());
            foreach(var instruction in program)
            {
                switch(instruction)
                {
                case '>':
                    containerStack.Peek().Add(new MoveTape(1));
                    break;
                case '<':
                    containerStack.Peek().Add(new MoveTape(-1));
                    break;
                case '+':
                    containerStack.Peek().Add(new IncrementCurrentValue(1));
                    break;
                case '-':
                    containerStack.Peek().Add(new IncrementCurrentValue(-1));
                    break;
                case '.':
                    containerStack.Peek().Add(new WriteOutCurrentValue());
                    break;
                case ',':
                    containerStack.Peek().Add(new ReadAndStoreChar());
                    break;
                case '[':
                    containerStack.Push(new List<IInstruction>());
                    break;
                case ']':
                    var instructions = containerStack.Pop();
                    containerStack.Peek().Add(new WhileLoop(new InstructionContainer(instructions)));
                    break;
                }
            }
            var lexedProgram =new InstructionContainer(containerStack.Pop());

            if(containerStack.Count >0)
            {
                throw new ArgumentException("Invalid program");
            }

            lexedProgram.Emit(generator);
            generator.FinalizeProgram();
        }
Exemplo n.º 3
0
 public WhileLoop(InstructionContainer instructions)
 {
     _instructions = instructions;
 }