Advertisement
Guest User

Script Interpreter

a guest
Oct 22nd, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. public interface IInterpreter
  2. {
  3.     int ProgramCounter { get; set; }    // Gets or sets the next command's position in the byte code
  4.    
  5.     void InitScript(byte[] code);       // Sets up the interpreter to run the specified script
  6.  
  7.     void NextCommand();         // Executes the next command
  8.     string DecompileCommand();      // Returns a string representing the next command
  9. }
  10.  
  11. public abstract class BaseInterpreter       // Subclass this to implement an interpreter
  12. {
  13.     public BaseInterpreter()
  14.     {
  15.         InitInsn();
  16.     }
  17.  
  18.     protected BinaryReader DataBus { get; set; }
  19.     public virtual int ProgramCounter
  20.     {
  21.         get { return (int) DataBus.BaseStream.Position; }
  22.         set { DataBus.BaseStream.Position = value; }
  23.     }
  24.    
  25.     abstract InitInsn();            // Initializes the instruction set
  26.    
  27.     // Instruction set:
  28.     public Action[] Execute { get; protected set; }
  29.     public Func<string>[] Decompile { get; protected set; }
  30.    
  31.     public virtual void InitScript(byte[] code)
  32.     {
  33.         if (DataBus != null)        // If a BinaryReader was used before, dispose it
  34.             DataBus.Dispose();
  35.        
  36.         DataBus = new BinaryReader(new MemoryStream(code, false));
  37.     }
  38.    
  39.     public virtual void NextCommand()
  40.     {
  41.         byte op = DataBus.ReadByte();
  42.         Execute[op]();
  43.     }
  44.    
  45.     public virtual string DecompileCommand()
  46.     {
  47.         byte op = DataBus.ReadByte();
  48.         return Decompile[op]();
  49.     }
  50. }
  51.  
  52. public abstract class InterpreterDecorator : IInterpreter
  53. {
  54.     public InterpreterDecorator(IInterpreter interpreter)
  55.     {
  56.         this.interpreter = interpreter;
  57.     }
  58.    
  59.     IInterpreter interpreter;
  60.    
  61.     int ProgramCounter
  62.     {
  63.         get { return interpreter.ProgramCounter; }
  64.         set { interpreter.ProgramCounter = value; }
  65.     }
  66.    
  67.     public virtual void InitScript(byte[] code)
  68.     {
  69.         interpreter.InitScript(code);
  70.     }
  71.  
  72.     public virtual void NextCommand()
  73.     {
  74.         interpreter.NextCommand();
  75.     }
  76.    
  77.     public virtual string DecompileCommand()
  78.     {
  79.         return interpreter.DecompileCommand();
  80.     }
  81. }
  82.  
  83. public class Debugger : InterpreterDecorator
  84. {
  85.     public Debugger(IInterpreter interpreter, bool breakMode, params int[] breakPoints) : base(interpreter)
  86.     {
  87.         BreakMode = breakMode;
  88.         BreakPoints = new SortedSet<int>(breakPoints);
  89.     }
  90.  
  91.     public bool BreakMode { get; private set; }         // True after break point
  92.     public SortedSet<int> BreakPoints { get; private set; }     // List of values of PC on which BreakMode should be entered.
  93.    
  94.     public override void NextCommand()
  95.     {
  96.         if (!BreakMode && BreakPoints.Contains(ProgramCounter))
  97.             BreakMode = true;
  98.         if (BreakMode)
  99.         {
  100.             int pc = ProgramCounter;
  101.             Console.WriteLine(DecompileCommand());
  102.             ProgramCounter = pc;
  103.            
  104.             string input = Console.ReadLine();
  105.             switch (input) {
  106.             case "continue":
  107.                 BreakMode = false;
  108.                 break;
  109.             // Add more keywords as needed ...
  110.             }
  111.         }
  112.        
  113.         base.NextCommand();
  114.     }
  115. }
  116.  
  117. public class Decompiler : InterpreterDecorator
  118. {
  119.     public Decompiler(IInterpreter interpreter) : base(interpreter)
  120.     {
  121.        
  122.     }
  123.  
  124.     public override void NextCommand()
  125.     {
  126.         Console.Write("{0,8:X}:   ", ProgramCounter);   // Print formatted position
  127.         Console.Write(DecompileCommand());      // Print decompiled code
  128.         Console.WriteLine();
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement