Exemplo n.º 1
0
        private Instructions BuildInstructions(Stream source)
        {
            var instructions = new Instructions();
            var reader       = new CharReader(source);
            var jumpTable    = new Stack <int>();

            while (reader.HasCharacters())
            {
                if (IsSetToZero(reader))
                {
                    instructions.Add(new Instruction(InstructionType.SetZero));
                }

                var c = reader.GetChar();

                switch (c)
                {
                case '+':
                    instructions.AddRange(DeflateComboInstruction(GetComboInstruction(reader)));
                    break;

                case '-':
                    instructions.AddRange(DeflateComboInstruction(GetComboInstruction(reader)));
                    break;

                case '>':
                    instructions.AddRange(DeflateComboInstruction(GetComboInstruction(reader)));
                    break;

                case '<':
                    instructions.AddRange(DeflateComboInstruction(GetComboInstruction(reader)));
                    break;

                case '.':
                    instructions.Add(new Instruction(InstructionType.Print));
                    break;

                case ',':
                    instructions.Add(new Instruction(InstructionType.Read));
                    break;

                case '[':
                    instructions.Add(new Instruction(InstructionType.BeginLoop));
                    jumpTable.Push(instructions.Count - 1);
                    break;

                case ']':
                    var beginPosition    = jumpTable.Pop();
                    var beginInstruction = instructions[beginPosition];
                    instructions.Add(new Instruction(InstructionType.EndLoop, beginPosition));
                    beginInstruction.Parameter = instructions.Count - 1;
                    break;
                }

                reader.Forward();
            }

            return(instructions);
        }
Exemplo n.º 2
0
        public void Compile(CompilationModel model)
        {
            Instructions.AddRange(Block.GetInstructions(model));

            // add return instruction on end if it isnt already there
            if (Instructions.Count == 0 || (Instructions.Latest().Value != Compiler.Instructions.ReturnInstruction &&
                                            Instructions.Latest().Value != Compiler.Instructions.ReturnIntInstruction &&
                                            Instructions.Latest().Value != Compiler.Instructions.ReturnPointerInstruction))
            {
                Instructions.Add(new InstructionModel(Compiler.Instructions.ReturnInstruction));
                Instructions.Latest().Comment = "Added by compiler";
            }
        }
 public void AddInstructions(params ILOpCodeValues[] instructions)
 {
     Instructions.AddRange(instructions.Select(x => ILInstruction.Create(x)));
 }
 public void AddInstructions(params ILInstruction[] instructions)
 {
     Instructions.AddRange(instructions);
 }
Exemplo n.º 5
0
 public ILMethodBuilder AddRange(Instruction[] instructions)
 {
     Instructions.AddRange(instructions);
     return(this);
 }