summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2025-01-24 22:45:08 +0200
committerZhongheng Liu <z.liu@outlook.com.gr>2025-01-24 22:45:08 +0200
commit7e8352e697081bbb73ff8d2fe2828eb5588e5f0a (patch)
treea89233b90a3e96aef66ac18ba10b540252c16f16
parent92617b26a26b3363b38367857a54fa0e4896699f (diff)
downloadrpn-parse-rs-7e8352e697081bbb73ff8d2fe2828eb5588e5f0a.tar.gz
rpn-parse-rs-7e8352e697081bbb73ff8d2fe2828eb5588e5f0a.tar.bz2
rpn-parse-rs-7e8352e697081bbb73ff8d2fe2828eb5588e5f0a.zip
chore: mv binary tree mod out of tree
-rw-r--r--Cargo.lock9
-rw-r--r--Cargo.toml3
-rw-r--r--src/lib.rs17
-rw-r--r--src/rpn.rs2
-rw-r--r--src/rpntree.rs8
-rw-r--r--src/rpntree/types.rs1
-rw-r--r--src/rpntree/types/tree.rs80
7 files changed, 25 insertions, 95 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 9a056f7..c583e7e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4,4 +4,13 @@ version = 3
[[package]]
name = "rpn"
+version = "0.0.2"
+dependencies = [
+ "tree",
+]
+
+[[package]]
+name = "tree"
version = "0.0.1"
+source = "sparse+http://kellnr.homelab.local/api/v1/crates/"
+checksum = "7154e93293f847b9951c2f864b48772c68800f4ff02c92084b589fefbfb6e6dc"
diff --git a/Cargo.toml b/Cargo.toml
index c666f07..d42045a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rpn"
-version = "0.0.1"
+version = "0.0.2"
edition = "2021"
author = ["Zhongheng Liu <z.liu@outlook.com.gr>"]
description = "Reverse Polish Notation parsing support"
@@ -8,3 +8,4 @@ description = "Reverse Polish Notation parsing support"
name = "rpn"
path = "src/lib.rs"
[dependencies]
+tree = { version = "0.0.1", registry = "kellnr" }
diff --git a/src/lib.rs b/src/lib.rs
index 8e36c01..14eba78 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -8,7 +8,7 @@
//! m.insert('a', 1.0);
//! m.insert('b', 2.0);
//! m.insert('c', 3.0);
-//! println!("{}", match eval("ab-c+c*".to_string(), m) {
+//! println!("{}", match eval("ab-c+c*", m) {
//! Ok(n) => n.to_string(),
//! Err(s) => s,
//! })
@@ -19,19 +19,20 @@
mod rpn;
mod rpntree;
-
use std::collections::HashMap;
pub use rpn::{eval, RpnOperation};
-pub use rpntree::eval_rpn;
-
+pub use rpntree::eval_infix;
pub fn test() {
let mut m: HashMap<char, f32> = HashMap::new();
m.insert('a', 1.0);
m.insert('b', 2.0);
m.insert('c', 3.0);
- println!("{}", match eval("ab-c+c*".to_string(), m) {
- Ok(n) => n.to_string(),
- Err(s) => s,
- })
+ println!(
+ "{}",
+ match eval("ab-c+c*", m) {
+ Ok(n) => n.to_string(),
+ Err(s) => s,
+ }
+ )
}
diff --git a/src/rpn.rs b/src/rpn.rs
index 6ac0dec..dbed3db 100644
--- a/src/rpn.rs
+++ b/src/rpn.rs
@@ -29,7 +29,7 @@ fn rpn_match_op(c: char) -> Option<RpnOperation> {
///
/// Returns `f32` arithmetic result if the expression can be parsed
/// Returns `Err` with a message if the RPN expression is malformed
-pub fn eval(rpn_str: String, var_map: HashMap<char, f32>) -> Result<f32, String> {
+pub fn eval(rpn_str: &str, var_map: HashMap<char, f32>) -> Result<f32, String> {
let mut stack: Vec<f32> = vec![];
for (_i, ch) in rpn_str.chars().enumerate() {
// println!("Parsing: {}, Stack: {:?}", ch, stack);
diff --git a/src/rpntree.rs b/src/rpntree.rs
index 5b74bfd..5ac8b04 100644
--- a/src/rpntree.rs
+++ b/src/rpntree.rs
@@ -1,6 +1,6 @@
use std::collections::HashMap;
-mod types;
-use types::tree::Tree;
+
+use tree::Tree;
fn reducer_string(opt_left: Option<String>, center: char, opt_right: Option<String>) -> String {
format!(
@@ -21,7 +21,7 @@ pub fn test() {
let result = tree.reduce::<String>(reducer_string);
println!("{}", result);
}
-pub fn eval_rpn(rpnstr: String) -> Result<String, String> {
+pub fn eval_infix(infixstr: &str) -> Result<String, String> {
let mut priority_map = HashMap::<char, usize>::new();
priority_map.insert('+', 100);
priority_map.insert('-', 100);
@@ -29,7 +29,7 @@ pub fn eval_rpn(rpnstr: String) -> Result<String, String> {
priority_map.insert('/', 200);
println!("Priority map: {:?}", priority_map);
let waiting_stack: Vec<char> = vec![];
- for c in rpnstr.chars() {
+ for c in infixstr.chars() {
if priority_map.contains_key(&c) {
let priority = match priority_map.get(&c) {
Some(c_ptr) => c_ptr.to_owned(),
diff --git a/src/rpntree/types.rs b/src/rpntree/types.rs
deleted file mode 100644
index 87821cc..0000000
--- a/src/rpntree/types.rs
+++ /dev/null
@@ -1 +0,0 @@
-pub mod tree;
diff --git a/src/rpntree/types/tree.rs b/src/rpntree/types/tree.rs
deleted file mode 100644
index 0181389..0000000
--- a/src/rpntree/types/tree.rs
+++ /dev/null
@@ -1,80 +0,0 @@
-use std::fmt::Display;
-/// A binary tree structure used to parse and evaluate an expression into RPN.
-pub struct Tree<T: Copy> {
- pub root_node: TreeNode<T>,
-}
-impl<T: Copy> Tree<T> {
- pub fn reduce<R>(&self, reducer: fn(Option<R>, T, Option<R>) -> R) -> R {
- self.root_node.reduce(reducer)
- }
- pub fn new(root_val: T) -> Tree<T> {
- Tree::<T> {
- root_node: TreeNode::<T> {
- value: root_val,
- left: None,
- right: None,
- },
- }
- }
-}
-impl<T: Display + Copy> Display for Tree<T> {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}", self.root_node)
- }
-}
-pub struct TreeNode<T: Copy> {
- pub value: T,
- pub left: Option<Box<TreeNode<T>>>,
- pub right: Option<Box<TreeNode<T>>>,
-}
-impl<T: Copy + Display> Display for TreeNode<T> {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(
- f,
- "{}{}{}",
- match &self.left {
- Some(v) => format!("({})<-", v.to_string()),
- None => "".to_string(),
- },
- format!("({})", self.value),
- match &self.right {
- Some(v) => format!("->({})", v.to_string()),
- None => "".to_string(),
- }
- )
- }
-}
-impl<T: Copy> TreeNode<T> {
- pub fn new(v: T) -> TreeNode<T> {
- TreeNode {
- value: v,
- left: None,
- right: None,
- }
- }
- pub fn reduce<R>(&self, reducer: fn(Option<R>, T, Option<R>) -> R) -> R {
- let left_val = match &self.left {
- Some(node) => Some(node.reduce(reducer)),
- None => None,
- };
- let right_val = match &self.right {
- Some(node) => Some(node.reduce(reducer)),
- None => None,
- };
- reducer(left_val, self.value, right_val)
- }
- pub fn insert_left(&mut self, value: T) {
- self.left = Some(Box::new(TreeNode {
- value,
- left: None,
- right: None,
- }));
- }
- pub fn insert_right(&mut self, value: T) {
- self.right = Some(Box::new(TreeNode {
- value,
- left: None,
- right: None,
- }));
- }
-}