Advertisement
Treyzania

Untitled

Jul 29th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.17 KB | None | 0 0
  1. pub fn vm_exec(p: &mut Process, isns: u64) -> Result<u64, (u64, &str)> {
  2.  
  3.     // FIXME Ugly nested matches can be made into a more functional thing.
  4.  
  5.     let mut execed = 0;
  6.  
  7.     while execed < isns {
  8.  
  9.         let top = p.call_stack.top();
  10.         match top {
  11.             Some(frame) => {
  12.                 let ti = frame.get_target_isn();
  13.                 match ti {
  14.                     Some(i) => {
  15.                         let ii = i.clone();
  16.                         match ii.execute(p) {
  17.                             Ok(segue) => {
  18.                                 execed += 1;
  19.                                 match segue {
  20.                                     IsnSegue::Next => {
  21.                                         let mut sf = p.call_stack.pop().unwrap();
  22.                                         sf.next_isn += 1;
  23.                                         p.call_stack.push(sf);
  24.                                     },
  25.                                     IsnSegue::RelJump(diff) => {
  26.                                         let mut sf = p.call_stack.pop().unwrap();
  27.                                         sf.next_isn += diff as u64;
  28.                                         p.call_stack.push(sf)
  29.                                     },
  30.                                     IsnSegue::Return => {
  31.                                         p.call_stack.pop();
  32.                                     },
  33.                                     IsnSegue::Detach => {
  34.                                         let mut sf = p.call_stack.pop().unwrap();
  35.                                         sf.next_isn += 1;
  36.                                         p.call_stack.push(sf);
  37.                                         return Ok(execed + 1);
  38.                                     }
  39.                                 }
  40.                             },
  41.                             Err(r) => return Err((execed + 1, r))
  42.                         }
  43.                     },
  44.                     None => {} // Don't push it back onto the stack and don't increment execed.
  45.                 }
  46.             },
  47.             None => return Err((execed, "process exited"))
  48.         }
  49.  
  50.     }
  51.  
  52.     return Ok(execed);
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement