Compare commits
6 Commits
92027f414d
...
39f5acd6ab
Author | SHA1 | Date |
---|---|---|
~karx | 39f5acd6ab | |
~karx | e71914df66 | |
~karx | 804a678f69 | |
~karx | dab95136c4 | |
~karx | d11d5ce755 | |
~karx | 9e1f68801f |
109
src/main.rs
109
src/main.rs
|
@ -140,18 +140,54 @@ impl Program {
|
|||
builder
|
||||
}
|
||||
|
||||
fn run_external(&mut self, filename: String) {
|
||||
fn run_external(&mut self, arguments: String) {
|
||||
// Split arguments by -
|
||||
let argument_vec: Vec<&str> = arguments.split("-").collect();
|
||||
println!("{}", argument_vec.len());
|
||||
let filename = argument_vec[0];
|
||||
|
||||
// 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");
|
||||
let mut prog = Program::from_string(contents);
|
||||
prog.run();
|
||||
|
||||
for (key, value) in prog.vars.iter() {
|
||||
self.vars.insert(*key, value.to_string());
|
||||
}
|
||||
|
||||
for (key, value) in prog.funcs.iter() {
|
||||
self.funcs.insert(*key, value.to_string());
|
||||
if argument_vec.len() > 1 {
|
||||
// Start from the second element
|
||||
for name in argument_vec[1..].iter() {
|
||||
let kind = match name.chars().nth(0) {
|
||||
Some(content) => content,
|
||||
None => panic!("SyntaxError: {}: Invalid syntax", arguments)
|
||||
};
|
||||
let name_to_import = match name.chars().nth(1) {
|
||||
Some(content) => content,
|
||||
None => panic!("SyntaxError: {}: Invalid syntax", arguments)
|
||||
};
|
||||
if kind == 'v' {
|
||||
let key = prog.vars.get(&name_to_import);
|
||||
match key {
|
||||
Some(value) => self.vars.insert(name_to_import, value.to_string()),
|
||||
None => panic!("ValueError: variable {} has not been defined in {}!", name_to_import, filename)
|
||||
};
|
||||
} else if kind == 'f' {
|
||||
let key = prog.funcs.get(&name_to_import);
|
||||
match key {
|
||||
Some(value) => {self.funcs.insert(name_to_import, value.to_string());()},
|
||||
None => panic!("ValueError: function {} has not been defined in {}!", name_to_import, filename)
|
||||
}
|
||||
} else {
|
||||
// Skip unknown types
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// implied catch-all, might be unintuitive
|
||||
for (key, value) in prog.vars.iter() {
|
||||
self.vars.insert(*key, value.to_string());
|
||||
}
|
||||
|
||||
for (key, value) in prog.funcs.iter() {
|
||||
self.funcs.insert(*key, value.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -212,61 +248,4 @@ fn main() {
|
|||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn make_program(contents: &str) -> Program {
|
||||
Program::from_string(contents.to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_math() {
|
||||
assert_eq!(eval::do_math("2-2".to_string(), '+'), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_undefined_opcode() {
|
||||
make_program("Hello\nWorld!").run();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_undefined_variable() {
|
||||
make_program("p$v").run();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_undefined_function() {
|
||||
make_program("p*x").run();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_factory() {
|
||||
let prog = make_program("lhHello\nlwWorld\np$h $w");
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_args() {
|
||||
let mut prog = make_program("lhHello\nlwWorld\np$h $w");
|
||||
prog.run();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_funcs() {
|
||||
let mut prog = make_program("fxa10-10\nfys10-5\np*x *y");
|
||||
prog.run();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
mod tests;
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
use super::*;
|
||||
|
||||
fn make_program(contents: &str) -> Program {
|
||||
Program::from_string(contents.to_string())
|
||||
}
|
||||
#[test]
|
||||
fn test_math() {
|
||||
assert_eq!(eval::do_math("2-2".to_string(), '+'), 4);
|
||||
}
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_undefined_opcode() {
|
||||
make_program("Hello\nWorld!").run();
|
||||
}
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_undefined_variable() {
|
||||
make_program("p$v").run();
|
||||
}
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_undefined_function() {
|
||||
make_program("p*x").run();
|
||||
}
|
||||
#[test]
|
||||
fn test_factory() {
|
||||
let prog = make_program("lhHello\nlwWorld\np$h $w");
|
||||
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);
|
||||
}
|
||||
#[test]
|
||||
fn test_args() {
|
||||
let mut prog = make_program("lhHello\nlwWorld\np$h $w");
|
||||
prog.run();
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_funcs() {
|
||||
let mut prog = make_program("fxa10-10\nfys10-5\np*x *y");
|
||||
prog.run();
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue