Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 3.02 KB | None | 0 0
  1. enum OpT {INC, MOVE, PRINT, LOOP}
  2. namespace Test
  3. {
  4.     struct Op {
  5.         public OpT op;
  6.         public int v;
  7.         public Op[] loop;
  8.  
  9.         public Op(OpT _op, int _v) { op = _op; v = _v; loop = null; }
  10.         public Op.vnull(OpT _op, Op[] _l) { op = _op; loop = _l; v = 0; }
  11.     }
  12.     [Compact]
  13.     public class Tape
  14.     {
  15.         public int pos;
  16.         public int[] tape;
  17.  
  18.         public Tape()
  19.         {
  20.             pos = 0;
  21.             tape = new int[1];
  22.            
  23.         }
  24.  
  25.         public int Get() { return tape[pos]; }
  26.         public void Inc(int x) { tape[pos] += x; }
  27.         public void Move(int x) { pos += x; while (pos >= tape.length) tape.resize(tape.length*2);}
  28.     }
  29.     [Compact]
  30.     class Program
  31.     {
  32.         public string code;
  33.         public  int pos;
  34.         public  Op[] ops;
  35.  
  36.         Program(string text)
  37.         {
  38.             code = text;
  39.             pos = 0;
  40.             ops = parse();
  41.         }
  42.  
  43.         private Op[] parse()
  44.         {
  45.             //  ArrayList<Op> res = new ArrayList<Op>();
  46.             Op[] res = {};
  47.             while (pos < code.length) {
  48.                 char c = code[pos];
  49.                 pos++;
  50.                 switch (c)
  51.                 {
  52.                     case '+': res += (Op(OpT.INC, 1)); break;
  53.                     case '-': res += (Op(OpT.INC, -1)); break;
  54.                     case '>': res += (Op(OpT.MOVE, 1)); break;
  55.                     case '<': res += (Op(OpT.MOVE, -1)); break;
  56.                     case '.': res += (Op(OpT.PRINT, 0)); break;
  57.                     case '[': res += (Op.vnull(OpT.LOOP, parse())); break;
  58.                     case ']': return res;
  59.                 }
  60.             }
  61.             return res;
  62.         }
  63.  
  64.         public void run() {
  65.             _run(ops, new Tape());
  66.         }
  67.  
  68.         private void _run(Op[] program, Tape tape) {
  69.             foreach (Op op in program) {
  70.                 switch (op.op) {
  71.                     case OpT.INC: tape.Inc(op.v); break;
  72.                     case OpT.MOVE: tape.Move(op.v); break;
  73.                     case OpT.LOOP: while (tape.Get() > 0) _run(op.loop, tape); break;
  74.                     case OpT.PRINT: stdout.printf("%c",(char)tape.Get()); break;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         static void main(string[] args)
  80.         {
  81.             string text;
  82.             FileUtils.get_contents(args[1],out text);
  83.            
  84.             message("JIT warming up");
  85.             Timer timer = new Timer ();
  86.             new Program(">++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++[>++++++++<-]>[-]<<>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++").run();
  87.             timer.stop();
  88.             message("time: " + timer.elapsed().to_string() + " s");
  89.  
  90.             message("run");
  91.             timer.reset();
  92.             var p = new Program(text);
  93.             p.run();
  94.             timer.stop();
  95.             message("time: " + timer.elapsed().to_string() + " s");
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement