This commit is contained in:
Yannick Reiß 2025-07-28 18:16:13 +02:00
commit 4a5488dff7
4 changed files with 81 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "micro"
version = "0.1.0"
edition = "2024"
[dependencies]

58
src/collector.rs Normal file
View File

@ -0,0 +1,58 @@
// Collector
// Collector that stores a complete set of data structures.
pub struct Collector {
pub definitions: Vec<(String, String)>,
pub arguments: Vec<(String, String)>,
}
// Implementation of Collector
// Functions to read data into the collector.
impl Collector {
// @name eval
// @brief Evaluate a given term
// @param &mut self, term: &str
pub fn eval(&mut self, term: String) -> String {
let editing_string: String = term.clone();
// Turn into words
// Rule 0: Replace definitions
// Rule 1: Solve brackets
let mut collect_bracket: bool = false;
let mut bracket_term: String = String::from("");
let mut resolved_bracket_term: String = String::from("");
for character in editing_string.chars() {
if character == '(' {
collect_bracket = true;
continue;
}
if character == ')' {
if !collect_bracket {
panic!("Closing bracket without matching opening bracket!");
} else {
resolved_bracket_term += self.eval(bracket_term).as_str();
bracket_term = String::from("");
collect_bracket = false;
}
continue;
}
if collect_bracket {
bracket_term += character.to_string().as_str();
} else {
resolved_bracket_term += character.to_string().as_str();
}
}
// Rule 2: Mul/Div
// Rule 3: Add/Sub
// Rule 4: Clean up from left to right
println!("{}", resolved_bracket_term);
return resolved_bracket_term;
}
}

16
src/main.rs Normal file
View File

@ -0,0 +1,16 @@
mod collector;
use collector::Collector;
fn main() {
let mut _collector: Collector = Collector {
definitions: vec![(String::from(""), String::from(""))],
arguments: vec![(String::from(""), String::from(""))],
};
let test_string: String = String::from("(1 + 2) * 3");
let echo_string: String = _collector.eval(test_string);
println!("Result: {}", echo_string);
}