Beispiel #1
0
        internal static int ReadVar(InterpreterStorage storage)
        {
            var blob   = storage.module.codeSection.Blob;
            var type   = (VariableType)blob[storage.cursor++];
            var result = 0;

            switch (type)
            {
            case VariableType.Register:
                result = storage.registers[blob[storage.cursor++]];
                break;

            case VariableType.Offset:
                var address = BitConverter.ToInt32(blob, storage.cursor);
                storage.cursor += 4;
                result          = BitConverter.ToInt32(blob, address);
                break;

            case VariableType.Const:
                result          = BitConverter.ToInt32(blob, storage.cursor);
                storage.cursor += 4;
                break;

            case VariableType.Array:
                address         = BitConverter.ToInt32(blob, storage.cursor);
                storage.cursor += 4;
                result          = BitConverter.ToInt32(blob, address + ReadVar(storage) * 4);
                break;

            default:
                throw new NotImplementedException();
            }

            return(result);
        }
Beispiel #2
0
        public void Run(InterpreterStorage storage)
        {
            Console.Write("Enter int value: ");
            int input = 0;

            while (!int.TryParse(Console.ReadLine(), out input))
            {
                Console.Write("Wrong value. Try again: ");
            }

            InterpreterUtils.SaveValue(storage, input);
        }
Beispiel #3
0
        internal static void SaveValue(InterpreterStorage storage, int input, Func <int, int, int> func = null)
        {
            var type = (VariableType)storage.module.codeSection.Blob[storage.cursor++];

            switch (type)
            {
            case VariableType.Register:
                int reg = storage.module.codeSection.Blob[storage.cursor++];
                storage.registers[reg] = func?.Invoke(storage.registers[reg], input) ?? input;
                break;

            case VariableType.Offset:
                int address = BitConverter.ToInt32(storage.module.codeSection.Blob, storage.cursor);
                storage.cursor += 4;

                if (func != null)
                {
                    int old = BitConverter.ToInt32(storage.module.codeSection.Blob, address);
                    input = func(old, input);
                }

                var bytes = BitConverter.GetBytes(input);
                storage.module.codeSection.Blob[address]     = bytes[0];
                storage.module.codeSection.Blob[address + 1] = bytes[1];
                storage.module.codeSection.Blob[address + 2] = bytes[2];
                storage.module.codeSection.Blob[address + 3] = bytes[3];

                break;

            case VariableType.Array:
                address         = BitConverter.ToInt32(storage.module.codeSection.Blob, storage.cursor);
                storage.cursor += 4;
                address        += ReadVar(storage) * 4;

                if (func != null)
                {
                    int old = BitConverter.ToInt32(storage.module.codeSection.Blob, address);
                    input = func(old, input);
                }

                bytes = BitConverter.GetBytes(input);
                storage.module.codeSection.Blob[address]     = bytes[0];
                storage.module.codeSection.Blob[address + 1] = bytes[1];
                storage.module.codeSection.Blob[address + 2] = bytes[2];
                storage.module.codeSection.Blob[address + 3] = bytes[3];

                break;

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #4
0
        internal static void RunJumpCondition(InterpreterStorage storage, Func <int, int, bool> predicate)
        {
            var offset = BitConverter.ToInt32(storage.module.codeSection.Blob, storage.cursor);

            storage.cursor += 4;

            var a = ReadVar(storage);
            var b = ReadVar(storage);

            if (predicate(a, b))
            {
                storage.cursor = offset;
            }
        }
Beispiel #5
0
        private static void Main(string[] args)
        {
            var input  = File.OpenRead(path: args[0]);
            var reader = new ExtBinaryReader(input);

            var moduleLayout = new BinaryModuleLayout();

            moduleLayout.Read(reader);

            input.Close();

            var storage = new InterpreterStorage()
            {
                instructions = new List <IInstruction>
                {
                    new NopInstruction(),
                    new PrintInstruction(),
                    new MovInstruction(),
                    new AddInstruction(),
                    new JumpLessInstruction(),
                    new HaltInstruction(),
                    new InputInstruction(),
                    new JumpGreaterInstruction(),
                    new JumpEqualsInstruction(),
                    new JumpInstruction(),
                    new PushInstruction(),
                    new PopInstruction(),
                },
                stack     = new Stack <int>(),
                module    = new BinaryModule(moduleLayout),
                cursor    = 0,
                registers = new int[4]
            };

            var interpreter = new Interpreter(storage);

            interpreter.Run();
        }
Beispiel #6
0
        public void Run(InterpreterStorage storage)
        {
            int offset = BitConverter.ToInt32(storage.module.codeSection.Blob, storage.cursor);

            storage.cursor = offset;
        }
Beispiel #7
0
 public void Run(InterpreterStorage storage)
 {
     InterpreterUtils.SaveValue(storage, storage.stack.Pop());
 }
Beispiel #8
0
 public void Run(InterpreterStorage storage)
 {
     InterpreterUtils.RunJumpCondition(storage, (left, right) => left == right);
 }
Beispiel #9
0
 public Interpreter(InterpreterStorage storage)
 {
     this.storage = storage;
 }
Beispiel #10
0
 public void Run(InterpreterStorage storage)
 {
     storage.stack.Push(InterpreterUtils.ReadVar(storage));
 }
Beispiel #11
0
        internal static void RunDestSrc(InterpreterStorage storage, Func <int, int, int> func)
        {
            var val = ReadVar(storage);

            SaveValue(storage, val, func);
        }
Beispiel #12
0
 public void Run(InterpreterStorage storage)
 {
     Console.ReadLine();
     storage.cursor = int.MaxValue;
 }
Beispiel #13
0
 public void Run(InterpreterStorage storage)
 {
     Console.WriteLine(InterpreterUtils.ReadVar(storage));
 }
Beispiel #14
0
 public void Run(InterpreterStorage storage)
 {
     InterpreterUtils.RunDestSrc(storage, (dest, src) => dest + src);
 }