add pick & roll instructions
This commit is contained in:
parent
92fc7ff44b
commit
ca6fd92932
1 changed files with 19 additions and 2 deletions
19
src/vm.rs
19
src/vm.rs
|
@ -4,7 +4,7 @@ pub type VM_Word = i64;
|
|||
#[repr(u64)]
|
||||
pub enum VM_Instruction {
|
||||
Nop, Jmp, Call, Branch, Ret,
|
||||
Lit, Dup, Drop, Swap, Rot,
|
||||
Lit, Pick, Roll, Dup, Drop, Swap, Rot,
|
||||
Fetch, Store,
|
||||
Accept, Emit,
|
||||
Add, //Sub, Mul, Div, Rem,
|
||||
|
@ -103,6 +103,23 @@ impl VM {
|
|||
let val = char::from(self.data_stack.pop().expect("emit expects value") as u8);
|
||||
print!("{}", val);
|
||||
}
|
||||
VM_Instruction::Pick => {
|
||||
let n = self.data_stack.pop().expect("pick requires index") as usize;
|
||||
self.data_stack.push( *self.data_stack.get( self.data_stack.len() - n ).expect("pick invalid index") );
|
||||
}
|
||||
VM_Instruction::Roll => {
|
||||
let n = self.data_stack.pop().expect("pick requires index") as usize;
|
||||
let l = self.data_stack.len();
|
||||
if n <= l {
|
||||
if n > 1 {
|
||||
let val = self.data_stack.remove( l-n );
|
||||
self.data_stack.push( val );
|
||||
}
|
||||
} else {
|
||||
eprintln!("stack = {:?}", self.data_stack);
|
||||
eprintln!("roll invalid index");
|
||||
}
|
||||
}
|
||||
VM_Instruction::Dup => {
|
||||
self.data_stack.push( *self.data_stack.last().expect("dup expect val") );
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue