Esempio n. 1
0
        private Value Stream(Value[] args)
        {
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);

            return new Value(writer);
        }
Esempio n. 2
0
        private Value Seek(Value[] args)
        {
            var writer = (BinaryWriter)args[0].Object;
            var value = args[1].Int;

            writer.Seek(value, SeekOrigin.Begin);
            return null;
        }
Esempio n. 3
0
        private Value CloseStream(Value[] args)
        {
            var writer = (BinaryWriter)args[0].Object;

            var stream = (MemoryStream)writer.BaseStream;
            mLastStream = stream.ToArray();

            writer.Close();

            return null;
        }
Esempio n. 4
0
        public Value Interpret(BytecodeFile file, DebugInfo debug, string function, Value argument)
        {
            mFile = file;
            mDebug = debug;

            int functionOffset = file.FindFunction(function);

            if (argument != null)
            {
                // push the argument
                Push(argument);

                Push(functionOffset);
                Call(1, false);
            }
            else
            {
                Push(functionOffset);
                Call(0, false);
            }

            return Interpret();
        }
Esempio n. 5
0
 Value ReadLine(Value[] args)
 {
     string read = Console.ReadLine();
     return new Value(read);
 }
Esempio n. 6
0
 Value KeyAvailable(Value[] args)
 {
     return new Value(Console.KeyAvailable);
 }
Esempio n. 7
0
 Value Clear(Value[] args)
 {
     Console.Clear();
     return null;
 }
Esempio n. 8
0
 private void Push(Value value)
 {
     mOperands.Push(value);
 }
Esempio n. 9
0
        private Structure MakeCallFrame(int numLocals, Structure parentFrame, int instruction)
        {
            var frame = new Structure(numLocals + 2);
            frame[numLocals] = new Value(parentFrame);
            frame[numLocals + 1] = new Value(instruction);

            return frame;
        }
Esempio n. 10
0
 public Value ForeignCall(int id, Value[] args)
 {
     var func = mFunctions[id].Value;
     return func(args);
 }
Esempio n. 11
0
 private IUnboundExpr ToExpr(Value value)
 {
     //### bob: implement me!
     return null;
 }
Esempio n. 12
0
        private Value WriteByte(Value[] args)
        {
            var writer = (BinaryWriter)args[0].Object;
            var value = args[1].Int;

            writer.Write((byte)value);
            return null;
        }
Esempio n. 13
0
        private Value StreamPosition(Value[] args)
        {
            var writer = (BinaryWriter)args[0].Object;

            return new Value((int)writer.BaseStream.Position);
        }
Esempio n. 14
0
        private Value ReadFile(Value[] args)
        {
            var path = args[0].String;

            return new Value(File.ReadAllText(path));
        }
Esempio n. 15
0
 Value WriteLine(Value[] args)
 {
     Console.WriteLine(args[0]);
     return null;
 }
Esempio n. 16
0
        // (int id, Value[] args)
        private void ForeignCall(int paramType)
        {
            int id = ReadInt();

            Value[] args;
            switch (paramType)
            {
                case 0: args = new Value[0]; break;
                case 1: args = new Value[] { Pop() }; break;
                case 2: args = PopStructure().Fields.ToArray(); break;
                default: throw new ArgumentException("Unknown parameter type.");
            }

            Value result = mForeignInterface.ForeignCall(id, args);
            if (result != null) Push(result);
        }
Esempio n. 17
0
        public Value Run(string functionName, Value argument)
        {
            if (!EnsureCompiled()) return null;

            // interpret the resulting bytecode
            var machine = new Machine(mForeign);
            machine.Printed += Machine_Printed;
            machine.MaxStackDepth = MaxStackDepth;

            try
            {
                return machine.Interpret(mBytecode, mDebug, functionName, argument);
            }
            catch (InterpreterException ex)
            {
                // do nothing
                //### bob: should report runtime errors
                Console.WriteLine(ex.ToString());

                return null;
            }
            finally
            {
                machine.Printed -= Machine_Printed;
            }
        }
Esempio n. 18
0
        private Value WriteString(Value[] args)
        {
            var writer = (BinaryWriter)args[0].Object;
            var value = args[1].String;

            writer.Write(Encoding.UTF8.GetBytes(value));
            writer.Write(0);
            return null;
        }