Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IInterpreter
- {
- int ProgramCounter { get; set; } // Gets or sets the next command's position in the byte code
- void InitScript(byte[] code); // Sets up the interpreter to run the specified script
- void NextCommand(); // Executes the next command
- string DecompileCommand(); // Returns a string representing the next command
- }
- public abstract class BaseInterpreter // Subclass this to implement an interpreter
- {
- public BaseInterpreter()
- {
- InitInsn();
- }
- protected BinaryReader DataBus { get; set; }
- public virtual int ProgramCounter
- {
- get { return (int) DataBus.BaseStream.Position; }
- set { DataBus.BaseStream.Position = value; }
- }
- abstract InitInsn(); // Initializes the instruction set
- // Instruction set:
- public Action[] Execute { get; protected set; }
- public Func<string>[] Decompile { get; protected set; }
- public virtual void InitScript(byte[] code)
- {
- if (DataBus != null) // If a BinaryReader was used before, dispose it
- DataBus.Dispose();
- DataBus = new BinaryReader(new MemoryStream(code, false));
- }
- public virtual void NextCommand()
- {
- byte op = DataBus.ReadByte();
- Execute[op]();
- }
- public virtual string DecompileCommand()
- {
- byte op = DataBus.ReadByte();
- return Decompile[op]();
- }
- }
- public abstract class InterpreterDecorator : IInterpreter
- {
- public InterpreterDecorator(IInterpreter interpreter)
- {
- this.interpreter = interpreter;
- }
- IInterpreter interpreter;
- int ProgramCounter
- {
- get { return interpreter.ProgramCounter; }
- set { interpreter.ProgramCounter = value; }
- }
- public virtual void InitScript(byte[] code)
- {
- interpreter.InitScript(code);
- }
- public virtual void NextCommand()
- {
- interpreter.NextCommand();
- }
- public virtual string DecompileCommand()
- {
- return interpreter.DecompileCommand();
- }
- }
- public class Debugger : InterpreterDecorator
- {
- public Debugger(IInterpreter interpreter, bool breakMode, params int[] breakPoints) : base(interpreter)
- {
- BreakMode = breakMode;
- BreakPoints = new SortedSet<int>(breakPoints);
- }
- public bool BreakMode { get; private set; } // True after break point
- public SortedSet<int> BreakPoints { get; private set; } // List of values of PC on which BreakMode should be entered.
- public override void NextCommand()
- {
- if (!BreakMode && BreakPoints.Contains(ProgramCounter))
- BreakMode = true;
- if (BreakMode)
- {
- int pc = ProgramCounter;
- Console.WriteLine(DecompileCommand());
- ProgramCounter = pc;
- string input = Console.ReadLine();
- switch (input) {
- case "continue":
- BreakMode = false;
- break;
- // Add more keywords as needed ...
- }
- }
- base.NextCommand();
- }
- }
- public class Decompiler : InterpreterDecorator
- {
- public Decompiler(IInterpreter interpreter) : base(interpreter)
- {
- }
- public override void NextCommand()
- {
- Console.Write("{0,8:X}: ", ProgramCounter); // Print formatted position
- Console.Write(DecompileCommand()); // Print decompiled code
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement