daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest Aug 16th, 2018 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Main()
  2. {
  3.     long[] literals = new long[]
  4.     {
  5.         1,
  6.         2
  7.     };
  8.    
  9.     byte[] instructions = new byte[]
  10.     {
  11.         PUSH, 0,
  12.         PUSH, 1,
  13.         ADD,
  14.         RET
  15.     };
  16.    
  17.     Compile(literals, instructions).Dump();
  18. }
  19.  
  20. const byte PUSH = 0;
  21. const byte ADD = 1;
  22. const byte RET = 2;
  23.  
  24. static long Compile(long[] literals, byte[] instructions)
  25. {
  26.     Stack<long> stack = new Stack<long>();
  27.     int instructionPointer = 0;
  28.     while (true)
  29.     {
  30.     switch (instructions[instructionPointer])
  31.         {
  32.         case PUSH:
  33.             ++instructionPointer;
  34.             stack.Push(literals[instructions[instructionPointer]]);
  35.             break;
  36.         case ADD:
  37.             stack.Push(stack.Pop() + stack.Pop());
  38.             break;
  39.         case RET:
  40.             return stack.Pop();
  41.         }
  42.         instructionPointer++;
  43.     }
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top