Create macro for constructing a program
This commit is contained in:
parent
dcef0a3943
commit
690fc10791
13
src/main.rs
13
src/main.rs
|
@ -13,6 +13,13 @@ struct Program {
|
||||||
funcs: HashMap<char, String>
|
funcs: HashMap<char, String>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! create_program {
|
||||||
|
($filename:expr, $prog_name:ident) => {
|
||||||
|
let contents = fs::read_to_string($filename).expect("Something went wrong reading the file");
|
||||||
|
let mut $prog_name = Program::from_string(contents);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
impl Program {
|
impl Program {
|
||||||
fn from_string(program: String) -> Program {
|
fn from_string(program: String) -> Program {
|
||||||
let mut op_list: Vec<String> = Vec::new();
|
let mut op_list: Vec<String> = Vec::new();
|
||||||
|
@ -148,8 +155,7 @@ impl Program {
|
||||||
let filename = argument_vec[0];
|
let filename = argument_vec[0];
|
||||||
|
|
||||||
// Read contents of the provided file and construct a symbolic Program from it
|
// Read contents of the provided file and construct a symbolic Program from it
|
||||||
let contents = fs::read_to_string(filename).expect("Something went wrong reading the file");
|
create_program!(filename, prog);
|
||||||
let mut prog = Program::from_string(contents);
|
|
||||||
prog.run();
|
prog.run();
|
||||||
|
|
||||||
if argument_vec.len() > 1 {
|
if argument_vec.len() > 1 {
|
||||||
|
@ -243,8 +249,7 @@ fn main() {
|
||||||
let filename = &args[1];
|
let filename = &args[1];
|
||||||
|
|
||||||
// Read contents of the provided file and construct a symbolic Program from it
|
// Read contents of the provided file and construct a symbolic Program from it
|
||||||
let contents = fs::read_to_string(filename).expect("Something went wrong reading the file");
|
create_program!(filename, prog);
|
||||||
let mut prog = Program::from_string(contents);
|
|
||||||
prog.run();
|
prog.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
23
src/tests.rs
23
src/tests.rs
|
@ -1,8 +1,11 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn make_program(contents: &str) -> Program {
|
macro_rules! create_program_with_contents {
|
||||||
Program::from_string(contents.to_string())
|
($contents:expr, $prog_name:ident) => {
|
||||||
|
let mut $prog_name = Program::from_string($contents.to_string());
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_math() {
|
fn test_math() {
|
||||||
assert_eq!(eval::do_math("2-2".to_string(), '+'), 4);
|
assert_eq!(eval::do_math("2-2".to_string(), '+'), 4);
|
||||||
|
@ -10,27 +13,31 @@ fn test_math() {
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_undefined_opcode() {
|
fn test_undefined_opcode() {
|
||||||
make_program("Hello\nWorld!").run();
|
create_program_with_contents!("Hello\nWorld!", prog);
|
||||||
|
prog.run()
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_undefined_variable() {
|
fn test_undefined_variable() {
|
||||||
make_program("p$v").run();
|
create_program_with_contents!("p$v", prog);
|
||||||
|
prog.run()
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_undefined_function() {
|
fn test_undefined_function() {
|
||||||
make_program("p*x").run();
|
create_program_with_contents!("p*x", prog);
|
||||||
|
prog.run()
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_factory() {
|
fn test_factory() {
|
||||||
let prog = make_program("lhHello\nlwWorld\np$h $w");
|
create_program_with_contents!("lhHello\nlwWorld\np$h $w", prog);
|
||||||
|
prog.run();
|
||||||
let vec_to_check: Vec<String> = vec!["lhHello", "lwWorld", "p$h $w"].into_iter().map(|s| s.to_string()).collect();
|
let vec_to_check: Vec<String> = vec!["lhHello", "lwWorld", "p$h $w"].into_iter().map(|s| s.to_string()).collect();
|
||||||
assert_eq!(prog.data, vec_to_check);
|
assert_eq!(prog.data, vec_to_check);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_args() {
|
fn test_args() {
|
||||||
let mut prog = make_program("lhHello\nlwWorld\np$h $w");
|
create_program_with_contents!("lhHello\nlwWorld\np$h $w", prog);
|
||||||
prog.run();
|
prog.run();
|
||||||
let args_to_check: HashMap<char, String> = [('h', String::from("Hello")), ('w', String::from("World"))].iter().cloned().collect();
|
let args_to_check: HashMap<char, String> = [('h', String::from("Hello")), ('w', String::from("World"))].iter().cloned().collect();
|
||||||
assert_eq!(prog.vars, args_to_check);
|
assert_eq!(prog.vars, args_to_check);
|
||||||
|
@ -38,7 +45,7 @@ fn test_args() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_funcs() {
|
fn test_funcs() {
|
||||||
let mut prog = make_program("fxa10-10\nfys10-5\np*x *y");
|
create_program_with_contents!("fxa10-10\nfys10-5\np*x *y", prog);
|
||||||
prog.run();
|
prog.run();
|
||||||
let funcs_to_check: HashMap<char, String> = [('x', String::from("a10-10")), ('y', String::from("s10-5"))].iter().cloned().collect();
|
let funcs_to_check: HashMap<char, String> = [('x', String::from("a10-10")), ('y', String::from("s10-5"))].iter().cloned().collect();
|
||||||
assert_eq!(prog.funcs, funcs_to_check);
|
assert_eq!(prog.funcs, funcs_to_check);
|
||||||
|
|
Loading…
Reference in New Issue