Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(nll)]
- #[derive(Debug)]
- pub enum Command {
- Add(i32),
- }
- #[derive(Debug)]
- pub struct Cpu {
- commands: Vec<Command>,
- pc: usize,
- acc: i32,
- }
- #[derive(Debug)]
- pub struct Block {
- commands: Vec<Command>,
- pc: usize,
- acc: i32,
- }
- impl Block {
- pub fn new<T>(iter: T) -> Block
- where
- T: IntoIterator<Item = Command>,
- {
- Block {
- commands: iter.into_iter().collect(),
- pc: 0,
- acc: 0,
- }
- }
- pub fn step(&mut self) {
- match &self.commands[self.pc] {
- Command::Add(i) => self.add(i),
- };
- }
- fn add(&mut self, op: &i32) {
- self.acc += op;
- }
- }
- fn main() {
- let mut block = Block::new(vec![Command::Add(1)]);
- block.step();
- println!("{:#?}", block);
- }
Add Comment
Please, Sign In to add comment