Implement program class
This commit is contained in:
parent
6bebbac9cd
commit
dc4efa3aa6
37
src/main.rs
37
src/main.rs
|
@ -1,5 +1,37 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
struct Program {
|
||||||
|
data: Vec<String>,
|
||||||
|
pc: u32
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Program {
|
||||||
|
fn from_string(program: String) -> Program {
|
||||||
|
let mut op_list: Vec<String> = Vec::new();
|
||||||
|
|
||||||
|
for opcode in program.split(" ").collect::<Vec<&str>>() {
|
||||||
|
let mut new_op = opcode.to_owned();
|
||||||
|
new_op = new_op.replace("\n", "");
|
||||||
|
|
||||||
|
if new_op.len() != 0 {
|
||||||
|
op_list.push(new_op.to_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Program{ data: op_list, pc: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self) {
|
||||||
|
println!("{}", self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Program {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "Program ({:?})", self.data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
|
@ -9,7 +41,8 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
let filename = &args[1];
|
let filename = &args[1];
|
||||||
|
|
||||||
let contents = fs::read_to_string(filename).expect("Something went wrong reading the file");
|
let contents = fs::read_to_string(filename).expect("Something went wrong reading the file");
|
||||||
println!("{}", contents);
|
let prog = Program::from_string(contents);
|
||||||
|
prog.run();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue