Refactor evaluator into eval func

This commit is contained in:
Yash 2021-02-04 11:45:53 -06:00
parent 1fb0530a13
commit 83a8899ff3
No known key found for this signature in database
GPG Key ID: A794DA2529474BA5
2 changed files with 13 additions and 8 deletions

View File

@ -1 +1 @@
pH pe pl pl po p, pW po pr pl pd p!
pHello pWorld!

View File

@ -23,18 +23,23 @@ impl Program {
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) {
println!("{}", self);
while self.pc < self.data.len() {
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;
}