sandwich

A funny programming language written in Rust
git clone https://tilde.team/~karx/sandwich.git
Log | Files | Refs | README | LICENSE

commit 520c7f92626bb66d147a1a2fb363349731a016f7
parent 527274433025ac5846e14e8b1e96bba687997965
Author: ~karx <karx@tilde.team>
Date:   Mon,  8 Feb 2021 14:52:12 +0000

Run rustfmt formatter

Diffstat:
Msrc/eval.rs | 11+++++------
Msrc/main.rs | 34+++++++++++++++++-----------------
2 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/src/eval.rs b/src/eval.rs @@ -3,19 +3,19 @@ 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 { '+' => num1 + num2, '-' => num1 - num2, '*' => num1 * num2, '/' => num1 / num2, - _ => panic!("SyntaxError: Unknown operator {}", operator) + _ => panic!("SyntaxError: Unknown operator {}", operator), } -}- \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs @@ -1,21 +1,21 @@ -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 { fn from_string(program: String) -> Program { let mut op_list: Vec<String> = Vec::new(); - + for opcode in program.split("\n").collect::<Vec<&str>>() { 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 @@ -40,28 +44,26 @@ 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; } } - + 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), } } @@ -116,7 +118,7 @@ fn main() { // Grab args and a filename let args: Vec<String> = env::args().collect(); 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. panic!("You must provide a filename!"); } @@ -153,5 +155,4 @@ mod tests { fn test_undefined_variable() { make_program("p$v").run(); } - -}- \ No newline at end of file +}