summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2025-01-16 22:06:48 +0200
committerZhongheng Liu <z.liu@outlook.com.gr>2025-01-16 22:06:48 +0200
commit8b68f77786394b8f2021373e7ac1e4a563b9f14d (patch)
tree18df43a02bc9425966f7370e93f3b16d95dcc7fc
parentb462df2f3a294711bd47a26b2bc93397318d738a (diff)
downloadrpn-parse-rs-8b68f77786394b8f2021373e7ac1e4a563b9f14d.tar.gz
rpn-parse-rs-8b68f77786394b8f2021373e7ac1e4a563b9f14d.tar.bz2
rpn-parse-rs-8b68f77786394b8f2021373e7ac1e4a563b9f14d.zip
feat(rpntree): create module file
Created module file for the RPN tree with test functions
-rw-r--r--src/rpntree.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/rpntree.rs b/src/rpntree.rs
new file mode 100644
index 0000000..5b74bfd
--- /dev/null
+++ b/src/rpntree.rs
@@ -0,0 +1,41 @@
+use std::collections::HashMap;
+mod types;
+use types::tree::Tree;
+
+fn reducer_string(opt_left: Option<String>, center: char, opt_right: Option<String>) -> String {
+ format!(
+ "{}{}{}",
+ opt_left.unwrap_or_else(|| { "".to_string() }),
+ opt_right.unwrap_or_else(|| { "".to_string() }),
+ center,
+ )
+}
+
+pub fn test() {
+ println!("adding characters to the binary tree");
+ // let PRIO_MAP: HashMap<char, usize> = HashMap::new();
+ let mut tree = Tree::new('+');
+ let _ = &tree.root_node.insert_left('a');
+ let _ = &tree.root_node.insert_right('b');
+ println!("{}", &tree);
+ let result = tree.reduce::<String>(reducer_string);
+ println!("{}", result);
+}
+pub fn eval_rpn(rpnstr: String) -> Result<String, String> {
+ let mut priority_map = HashMap::<char, usize>::new();
+ priority_map.insert('+', 100);
+ priority_map.insert('-', 100);
+ priority_map.insert('*', 200);
+ priority_map.insert('/', 200);
+ println!("Priority map: {:?}", priority_map);
+ let waiting_stack: Vec<char> = vec![];
+ for c in rpnstr.chars() {
+ if priority_map.contains_key(&c) {
+ let priority = match priority_map.get(&c) {
+ Some(c_ptr) => c_ptr.to_owned(),
+ None => return Err("Internal error".to_string()),
+ };
+ }
+ }
+ Ok("Hello".to_string())
+}