Run rustfmt formatter

This commit is contained in:
~karx 2021-02-08 14:52:12 +00:00
parent 5272744330
commit 520c7f9262
2 changed files with 22 additions and 21 deletions

View File

@ -3,12 +3,12 @@ pub fn do_math(arguments: String, operator: char) -> u32 {
let num1: u32 = match split_args[0].parse() {
Ok(num) => num,
Err(_e) => panic!("ArgumentError: Not a number: {}", split_args[0])
Err(_e) => panic!("ArgumentError: Not a number: {}", split_args[0]),
};
let num2: u32 = match split_args[1].parse() {
Ok(num) => num,
Err(_e) => panic!("ArgumentError: Not a number: {}", split_args[1])
Err(_e) => panic!("ArgumentError: Not a number: {}", split_args[1]),
};
match operator {
@ -16,6 +16,6 @@ pub fn do_math(arguments: String, operator: char) -> u32 {
'-' => num1 - num2,
'*' => num1 * num2,
'/' => num1 / num2,
_ => panic!("SyntaxError: Unknown operator {}", operator)
_ => panic!("SyntaxError: Unknown operator {}", operator),
}
}

View File

@ -1,15 +1,15 @@
use std::fs;
use std::collections::HashMap;
use std::env;
use std::fmt;
use std::fs;
use std::usize;
use std::collections::HashMap;
mod eval;
struct Program {
data: Vec<String>,
pc: usize,
vars: HashMap<char, String>
vars: HashMap<char, String>,
}
impl Program {
@ -24,7 +24,11 @@ impl Program {
}
}
return Program{ data: op_list, pc: 0, vars: HashMap::new() };
return Program {
data: op_list,
pc: 0,
vars: HashMap::new(),
};
}
// Reads the arguments passed to an opcode, and inserts variables where necessary
@ -40,7 +44,7 @@ impl Program {
if index > 0 {
// Only test for the dollar sign if it's not the first character
// This is because there can't be anything before the first character, otherwise it's not the first
if argument_vec[index-1] == '$' {
if argument_vec[index - 1] == '$' {
// If the previous character is a dollar sign, we can skip this iteration because we know the variable has already been handled
continue;
}
@ -48,20 +52,18 @@ impl Program {
if current_char == '$' {
// If the current char is a $, we know that the next char should be a variable
let variable = argument_vec[index+1];
let variable = argument_vec[index + 1];
let key = self.vars.get(&variable);
match key {
Some(value) => str_to_push = value.to_string(),
None => panic!("NotFoundError: Variable {} has not been defined", variable)
None => panic!("NotFoundError: Variable {} has not been defined", variable),
}
} else {
// If there's no variable, then just push the char that was already there
str_to_push = current_char.to_string();
}
builder.push_str(&str_to_push);
}
@ -88,8 +90,8 @@ 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),
'#' => {}, // Do nothing for comments
_ => panic!("SyntaxError: No such opcode: {}", self.pc)
'#' => {} // Do nothing for comments
_ => panic!("SyntaxError: No such opcode: {}", self.pc),
}
}
@ -153,5 +155,4 @@ mod tests {
fn test_undefined_variable() {
make_program("p$v").run();
}
}