commit ac30bfdfa513bf53b0d25414ebcb0c0798eace05 Author: Erin Nova Date: Wed Sep 29 15:13:51 2021 -0400 Add basic tokenization diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6ab487f --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "calc" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7073ca8 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "calc" +description = "A simple terminal calculator" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d6477b7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,104 @@ +use::std::io; + +// Enum of different mathematical operations +#[derive(Debug)] +enum Operators { + Add, + Subtract, + Multiply, + Divide, + Power, +} + +// Enum of different separators +#[derive(Debug)] +enum Separators { + Open, + Close, + Decimal, +} + +// Enum for types of one 'block" in an eqation +#[derive(Debug)] +enum Blocks { + Operator(Operators), // A mathematical operator + Separator(Separators), // A separator, eg. '(', ')' + Integer(i32), // An integer + Float(f64), // A float + Error, +} + +// Determine what type a 'block' is +fn determine_type(block: &str) -> Blocks { + let block_type = match block { + // Check if it's an operator + "+" => Blocks::Operator(Operators::Add), + "-" => Blocks::Operator(Operators::Subtract), + "*" => Blocks::Operator(Operators::Multiply), + "/" => Blocks::Operator(Operators::Divide), + "^" => Blocks::Operator(Operators::Power), + // Check if it's a separator + "(" => Blocks::Separator(Separators::Open), + ")" => Blocks::Separator(Separators::Close), + "." => Blocks::Separator(Separators::Decimal), + + _ => { + match block.parse::() { + Ok(integer) => Blocks::Integer(integer), + Err(_e) => { + match block.parse::() { + Ok(float) => Blocks::Float(float), + Err(_e) => Blocks::Error, + } + } + } + } + }; + return block_type; +} +// cut the input string into different pieces +fn separate(input: String) { + // Vector for the whole equation + let mut equation: Vec = Vec::new(); + // Vector for collecting a single number + let mut number: Vec = Vec::new(); + + // Loop through each character in the equation + for i in input.chars() { + match i.to_digit(10) { + Some(..) => { + number.push(i); + }, + None => { + if i == '.' { + number.push(i); + } else { + let full_number: String = number.iter().collect::(); + println!("Full number found: {}", full_number); + number = Vec::new(); // Any found number has finished, so clear the vector + equation.push(determine_type(&full_number)); + } + equation.push(determine_type(&i.to_string())); // Determine the type and append it to the equation vector + } + } + println!("{}", i); + } + let full_number: String = number.iter().collect::(); + equation.push(determine_type(&full_number)); + println!("{:?}", equation); + // 5+6-2/45*(6-2)^5 +} + +fn main() { + println!("RUNK (Rust Universal Number Calculator)"); + + let mut input = String::new(); + + io::stdin() + .read_line(&mut input) + .expect("Failed to read input"); + input = input.split_whitespace().collect(); + + println!("Your input was {}", input); + separate(input); +}