Run rustfmt formatter
This commit is contained in:
parent
5272744330
commit
520c7f9262
10
src/eval.rs
10
src/eval.rs
|
@ -3,19 +3,19 @@ pub fn do_math(arguments: String, operator: char) -> u32 {
|
||||||
|
|
||||||
let num1: u32 = match split_args[0].parse() {
|
let num1: u32 = match split_args[0].parse() {
|
||||||
Ok(num) => num,
|
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() {
|
let num2: u32 = match split_args[1].parse() {
|
||||||
Ok(num) => num,
|
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 {
|
match operator {
|
||||||
'+' => num1 + num2,
|
'+' => num1 + num2,
|
||||||
'-' => num1 - num2,
|
'-' => num1 - num2,
|
||||||
'*' => num1 * num2,
|
'*' => num1 * num2,
|
||||||
'/' => num1 / num2,
|
'/' => num1 / num2,
|
||||||
_ => panic!("SyntaxError: Unknown operator {}", operator)
|
_ => panic!("SyntaxError: Unknown operator {}", operator),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -1,21 +1,21 @@
|
||||||
use std::fs;
|
use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::fs;
|
||||||
use std::usize;
|
use std::usize;
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
mod eval;
|
mod eval;
|
||||||
|
|
||||||
struct Program {
|
struct Program {
|
||||||
data: Vec<String>,
|
data: Vec<String>,
|
||||||
pc: usize,
|
pc: usize,
|
||||||
vars: HashMap<char, String>
|
vars: HashMap<char, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
for opcode in program.split("\n").collect::<Vec<&str>>() {
|
for opcode in program.split("\n").collect::<Vec<&str>>() {
|
||||||
let new_op = opcode.to_owned();
|
let new_op = opcode.to_owned();
|
||||||
|
|
||||||
|
@ -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
|
// Reads the arguments passed to an opcode, and inserts variables where necessary
|
||||||
|
@ -40,28 +44,26 @@ impl Program {
|
||||||
if index > 0 {
|
if index > 0 {
|
||||||
// Only test for the dollar sign if it's not the first character
|
// 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
|
// 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
|
// If the previous character is a dollar sign, we can skip this iteration because we know the variable has already been handled
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if current_char == '$' {
|
if current_char == '$' {
|
||||||
// If the current char is a $, we know that the next char should be a variable
|
// 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);
|
let key = self.vars.get(&variable);
|
||||||
match key {
|
match key {
|
||||||
Some(value) => str_to_push = value.to_string(),
|
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 {
|
} else {
|
||||||
// If there's no variable, then just push the char that was already there
|
// If there's no variable, then just push the char that was already there
|
||||||
str_to_push = current_char.to_string();
|
str_to_push = current_char.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
builder.push_str(&str_to_push);
|
builder.push_str(&str_to_push);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,8 +90,8 @@ impl Program {
|
||||||
'm' => println!("{}", eval::do_math(self.args_or_vars(arguments), '*')),
|
'm' => println!("{}", eval::do_math(self.args_or_vars(arguments), '*')),
|
||||||
'd' => println!("{}", eval::do_math(self.args_or_vars(arguments), '/')),
|
'd' => println!("{}", eval::do_math(self.args_or_vars(arguments), '/')),
|
||||||
'l' => self.add_var(arguments),
|
'l' => self.add_var(arguments),
|
||||||
'#' => {}, // Do nothing for comments
|
'#' => {} // Do nothing for comments
|
||||||
_ => panic!("SyntaxError: No such opcode: {}", self.pc)
|
_ => panic!("SyntaxError: No such opcode: {}", self.pc),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +118,7 @@ fn main() {
|
||||||
// Grab args and a filename
|
// Grab args and a filename
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
if args.len() == 1 {
|
if args.len() == 1 {
|
||||||
// Args will always have at least 1 argument, which is the name of the executable.
|
// Args will always have at least 1 argument, which is the name of the executable.
|
||||||
// That's why we're checking index 1, not index 0.
|
// That's why we're checking index 1, not index 0.
|
||||||
panic!("You must provide a filename!");
|
panic!("You must provide a filename!");
|
||||||
}
|
}
|
||||||
|
@ -153,5 +155,4 @@ mod tests {
|
||||||
fn test_undefined_variable() {
|
fn test_undefined_variable() {
|
||||||
make_program("p$v").run();
|
make_program("p$v").run();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue