Refactor evaluator into eval func
This commit is contained in:
parent
1fb0530a13
commit
83a8899ff3
19
src/main.rs
19
src/main.rs
|
@ -23,18 +23,23 @@ impl Program {
|
||||||
return Program{ data: op_list, pc: 0 };
|
return Program{ data: op_list, pc: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn eval(&self, instruction: &String) {
|
||||||
|
let instruction_vec: Vec<char> = instruction.chars().collect();
|
||||||
|
let opcode = instruction_vec[0];
|
||||||
|
let arguments = &instruction[1..];
|
||||||
|
|
||||||
|
match opcode {
|
||||||
|
'p' => println!("{}", arguments),
|
||||||
|
_ => panic!("SyntaxError at opcode {}!", self.pc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn run(&mut self) {
|
fn run(&mut self) {
|
||||||
println!("{}", self);
|
println!("{}", self);
|
||||||
while self.pc < self.data.len() {
|
while self.pc < self.data.len() {
|
||||||
let instruction = &self.data[self.pc];
|
let instruction = &self.data[self.pc];
|
||||||
let instruction_vec: Vec<char> = instruction.chars().collect();
|
|
||||||
let opcode = instruction_vec[0];
|
|
||||||
|
|
||||||
match opcode {
|
|
||||||
'p' => print!("{}", instruction_vec[1]),
|
|
||||||
_ => panic!("SyntaxError at opcode {}!", self.pc)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
self.eval(instruction);
|
||||||
|
|
||||||
self.pc = self.pc + 1;
|
self.pc = self.pc + 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue