Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. fn main() {
  2. let program_data = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
  3. let mut program = Program::from_chars(program_data);
  4. program.step();
  5. println!("OUTPUT:");
  6. println!(" pointer: {}", program.pointer);
  7. println!(" data: {:?}", program.data);
  8. }
  9.  
  10. #[derive(Debug)]
  11. pub enum Instruction {
  12. // > : Move pointer forward
  13. Forward,
  14.  
  15. // < : Move pointer backward
  16. Backward,
  17.  
  18. // + : Increment current value by 1
  19. Increment,
  20.  
  21. // - : Decrement current value by 1
  22. Decrement,
  23.  
  24. // . : Output current value
  25. Output,
  26.  
  27. // , : Input value, store at current pointer
  28. Input,
  29.  
  30. // [ : Begin loop
  31. Begin,
  32.  
  33. // ] : End loop; if current value is not zero, jump back to ]
  34. End
  35. }
  36.  
  37. impl Instruction {
  38.  
  39. fn new(c: char) -> Instruction {
  40. match c {
  41. '>' => Instruction::Forward,
  42. '<' => Instruction::Backward,
  43. '+' => Instruction::Increment,
  44. '-' => Instruction::Decrement,
  45. '.' => Instruction::Output,
  46. ',' => Instruction::Input,
  47. '[' => Instruction::Begin,
  48. ']' => Instruction::End,
  49. _ => panic!("Invalid character: {}", c)
  50. }
  51. }
  52.  
  53. }
  54.  
  55. struct Program {
  56. instructions: Vec<Instruction>,
  57. step: usize,
  58. pointer: usize,
  59. begin_stack: Vec<usize>,
  60. data: Vec<i32>
  61. }
  62.  
  63. impl Program {
  64. fn from_chars(characters: &str) -> Program {
  65. let instructions = characters.chars().map(Instruction::new).collect();
  66. Program {
  67. instructions: instructions,
  68. step: 0 as usize,
  69. pointer: 0 as usize,
  70. begin_stack: Vec::new(),
  71. data: vec![0] // reserve
  72. }
  73. }
  74.  
  75. fn step(&mut self) {
  76. let step = self.step;
  77. self.step += 1;
  78. if step < self.instructions.len() {
  79. match self.instructions[step] {
  80. Instruction::Forward => self.forward(),
  81. Instruction::Backward => self.backward(),
  82. Instruction::Increment => self.increment(),
  83. Instruction::Decrement => self.decrement(),
  84. Instruction::Input => self.input(),
  85. Instruction::Output => self.output(),
  86. Instruction::Begin => self.begin(),
  87. Instruction::End => self.end()
  88. }
  89. }
  90. }
  91.  
  92. fn forward(&mut self) {
  93. println!("forward");
  94. self.pointer += 1;
  95. println!("pointer: {}", self.pointer);
  96. if self.pointer >= self.data.len() {
  97. self.data.push(0);
  98. }
  99. println!("data: {:?}", self.data);
  100. self.step();
  101. }
  102.  
  103. fn backward(&mut self) {
  104. println!("backward");
  105. self.pointer -= 1;
  106. println!("pointer: {}", self.pointer);
  107. println!("data: {:?}", self.data);
  108. self.step();
  109. }
  110.  
  111. fn increment(&mut self) {
  112. println!("increment");
  113. self.data[self.pointer] += 1;
  114. println!("{:?}", self.pointer);
  115. println!("{:?}", self.data);
  116. self.step();
  117. }
  118.  
  119. fn decrement(&mut self) {
  120. println!("decrement");
  121. self.data[self.pointer] -= 1;
  122. self.step();
  123. }
  124.  
  125. fn input(&mut self) {
  126. println!("input");
  127. self.step();
  128. }
  129.  
  130. fn output(&mut self) {
  131. println!("output");
  132. println!("{}", self.data[self.pointer]);
  133. self.step();
  134. }
  135.  
  136. fn begin(&mut self) {
  137. println!("begin");
  138. self.begin_stack.push(self.step);
  139. self.step();
  140. }
  141.  
  142. fn end(&mut self) {
  143. println!("end");
  144. println!("{:?}", self.pointer);
  145. if (self.data[self.pointer] == 0) {
  146. self.begin_stack.pop();
  147. self.step();
  148. } else {
  149. self.step = match self.begin_stack.last() {
  150. Some(v) => *v,
  151. None => panic!("Unmatched loop bracket")
  152. };
  153. self.step();
  154. }
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement