summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 4578e30ccd2420f2b411ad503cbbd38536a72606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
mod rpn;
mod rpntree;
use std::{collections::HashMap, io::stdin};
/*struct TextSettings {}
struct Component {
    text: String,
    setting: TextSettings,
}
struct Article {
    title: String,
    components: Vec<Component>,
}
impl Display for Component {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.text)
    }
}
impl Component {
    fn new(text: String, setting: Option<TextSettings>) -> Component {
        match setting {
            Some(s) => {
                return Component {
                    text,
                    setting: s,
                }
            },
            None => {
                return Component {
                    text,
                    setting: TextSettings {}
                }
            }
        }
    }
}
impl Article {
    fn new(lines: Vec<String>) -> Article {
        let mut components = vec![];
        for (i, l) in lines.iter().enumerate() {
            components.insert(i, Component::new(l.to_string(), None));
        }
        Article {
            title: "article title".to_string(),
            components,
        }
    }
}
impl Display for Article {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut builder = String::from("");
        for c in &self.components {
            builder += &format!("{}\n", c);
        }
        write!(f, "{}", builder)
    }
}*/
fn main() -> Result<(), String> {
    rpntree::test();
    static RETURN_CHAR: &str = "*";
    let mut vars: HashMap<char, f32> = HashMap::new();
    println!("Input your variable mappings below, in K:char,V:f32 format, * to break;");
    let term = stdin();
    loop {
        let mut s = String::new();
        let _ = term.read_line(&mut s);
        if s.trim() == RETURN_CHAR {
            break;
        }
        let vals = s.trim().split_once(',');
        if vals.is_none() {
            println!("Format invalid!");
            return Err("Format invalid".to_string());
        }
        let (k_s, v_s) = vals.unwrap();
        println!("Stored: '{}': '{}'", k_s, v_s);
        if k_s.chars().count() != 1 {
            println!("Identifier must only be one character!");
            return Err("Identified length exceeded".to_string());
        }
        let k = k_s.chars().next();
        if k.is_none() {
            return Err("No identifier name found.".to_string());
        }
        let v = v_s.parse::<f32>();
        match v {
            Ok(val) => vars.insert(k.unwrap(), val),
            Err(e) => return Err(e.to_string()),
        };
    }
    println!("Great! Now put your RPN expression below: ");
    let mut rpn_str = String::new();
    let _ = term.read_line(&mut rpn_str);
    let result = rpn::eval(rpn_str.trim().to_string(), vars);
    match result {
        Ok(number) => {
            println!("Result: {}", number);
            return Ok(());
        }
        Err(reason) => {
            return Err(format!("In RPN evaluation: {}", reason));
        }
    }
}
/*fn _main() -> Result<(), Box<dyn Error>> {
    println!("running main function");
    let mut cont = true;
    let mut lines: Vec<String> = vec![];
    while true {
        let mut s = String::new();
        let input = stdin();
        let _size = input.read_line(&mut s);
        println!("{}", s);
        if s.eq("stop") { break; }
        lines.push(s);
    }
    let a = Article::new(lines);
    println!("{}", a);
    Ok(())
}*/