tyler569 icon

Rust VM

tyler569 | PRO | 11/16/15 01:05:35 PM UTC | 0 ⭐ | 939 👁️ | Never ⏰ | []
Rust |

580 B

|

None

|

0 👍

/

0 👎

 
enum Bytecode {
    Nop,
    Push(i64),
    BinaryAdd,
    Print,
}
use Bytecode::*;
 
static CODE: &'static [Bytecode] = &[
    Push(9),
    Push(5),
    BinaryAdd,
    Print,
];
 
fn main() {
    let mut stack = Vec::new();
 
    for element in CODE {
        match *element {
            Nop => {},
            Push(x) => { stack.push(x) },
            BinaryAdd => {
                let v = stack.pop().unwrap();
                *stack.last_mut().unwrap() += v;
            },
            Print => { println!("{}", stack.pop().unwrap()); },
        }
    }
}

Comments