Guest User

Untitled

a guest
Jan 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #![feature(nll)]
  2. #[derive(Debug)]
  3. pub enum Command {
  4. Add(i32),
  5. }
  6.  
  7. #[derive(Debug)]
  8. pub struct Cpu {
  9. commands: Vec<Command>,
  10. pc: usize,
  11. acc: i32,
  12. }
  13.  
  14. #[derive(Debug)]
  15. pub struct Block {
  16. commands: Vec<Command>,
  17. pc: usize,
  18. acc: i32,
  19. }
  20.  
  21. impl Block {
  22. pub fn new<T>(iter: T) -> Block
  23. where
  24. T: IntoIterator<Item = Command>,
  25. {
  26. Block {
  27. commands: iter.into_iter().collect(),
  28. pc: 0,
  29. acc: 0,
  30. }
  31. }
  32.  
  33. pub fn step(&mut self) {
  34. match &self.commands[self.pc] {
  35. Command::Add(i) => self.add(i),
  36. };
  37. }
  38.  
  39. fn add(&mut self, op: &i32) {
  40. self.acc += op;
  41. }
  42. }
  43.  
  44. fn main() {
  45. let mut block = Block::new(vec![Command::Add(1)]);
  46. block.step();
  47. println!("{:#?}", block);
  48. }
Add Comment
Please, Sign In to add comment