Advertisement
tyler569

Rust VM

Nov 16th, 2015
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.58 KB | None | 0 0
  1.  
  2. enum Bytecode {
  3.     Nop,
  4.     Push(i64),
  5.     BinaryAdd,
  6.     Print,
  7. }
  8. use Bytecode::*;
  9.  
  10. static CODE: &'static [Bytecode] = &[
  11.    Push(9),
  12.    Push(5),
  13.    BinaryAdd,
  14.    Print,
  15. ];
  16.  
  17. fn main() {
  18.    let mut stack = Vec::new();
  19.  
  20.    for element in CODE {
  21.        match *element {
  22.            Nop => {},
  23.            Push(x) => { stack.push(x) },
  24.            BinaryAdd => {
  25.                let v = stack.pop().unwrap();
  26.                *stack.last_mut().unwrap() += v;
  27.            },
  28.            Print => { println!("{}", stack.pop().unwrap()); },
  29.        }
  30.    }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement