private Value Stream(Value[] args) { var stream = new MemoryStream(); var writer = new BinaryWriter(stream); return new Value(writer); }
private Value Seek(Value[] args) { var writer = (BinaryWriter)args[0].Object; var value = args[1].Int; writer.Seek(value, SeekOrigin.Begin); return null; }
private Value CloseStream(Value[] args) { var writer = (BinaryWriter)args[0].Object; var stream = (MemoryStream)writer.BaseStream; mLastStream = stream.ToArray(); writer.Close(); return null; }
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(); }
Value ReadLine(Value[] args) { string read = Console.ReadLine(); return new Value(read); }
Value KeyAvailable(Value[] args) { return new Value(Console.KeyAvailable); }
Value Clear(Value[] args) { Console.Clear(); return null; }
private void Push(Value value) { mOperands.Push(value); }
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; }
public Value ForeignCall(int id, Value[] args) { var func = mFunctions[id].Value; return func(args); }
private IUnboundExpr ToExpr(Value value) { //### bob: implement me! return null; }
private Value WriteByte(Value[] args) { var writer = (BinaryWriter)args[0].Object; var value = args[1].Int; writer.Write((byte)value); return null; }
private Value StreamPosition(Value[] args) { var writer = (BinaryWriter)args[0].Object; return new Value((int)writer.BaseStream.Position); }
private Value ReadFile(Value[] args) { var path = args[0].String; return new Value(File.ReadAllText(path)); }
Value WriteLine(Value[] args) { Console.WriteLine(args[0]); return null; }
// (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); }
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; } }
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; }