Add ability to add functions
This commit is contained in:
parent
520c7f9262
commit
1a811c650f
13
src/main.rs
13
src/main.rs
|
@ -10,6 +10,7 @@ struct Program {
|
|||
data: Vec<String>,
|
||||
pc: usize,
|
||||
vars: HashMap<char, String>,
|
||||
funcs: HashMap<char, String>
|
||||
}
|
||||
|
||||
impl Program {
|
||||
|
@ -28,6 +29,7 @@ impl Program {
|
|||
data: op_list,
|
||||
pc: 0,
|
||||
vars: HashMap::new(),
|
||||
funcs: HashMap::new()
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -78,6 +80,16 @@ impl Program {
|
|||
self.vars.insert(name, value);
|
||||
}
|
||||
|
||||
fn add_func(&mut self, arguments: &str) {
|
||||
let argument_vec: Vec<char> = arguments.chars().collect();
|
||||
let name = argument_vec[0];
|
||||
let body: String = argument_vec[1..].into_iter().collect();
|
||||
|
||||
self.funcs.insert(name, body);
|
||||
|
||||
println!("{:?}", self.funcs);
|
||||
}
|
||||
|
||||
fn parse(&mut self, instruction: &String) {
|
||||
// Opcode is the first character, arguments are everything after the first char
|
||||
let opcode = instruction.chars().collect::<Vec<char>>()[0];
|
||||
|
@ -90,6 +102,7 @@ impl Program {
|
|||
'm' => println!("{}", eval::do_math(self.args_or_vars(arguments), '*')),
|
||||
'd' => println!("{}", eval::do_math(self.args_or_vars(arguments), '/')),
|
||||
'l' => self.add_var(arguments),
|
||||
'f' => self.add_func(arguments),
|
||||
'#' => {} // Do nothing for comments
|
||||
_ => panic!("SyntaxError: No such opcode: {}", self.pc),
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue