summary refs log tree commit diff
path: root/src/main.rs
blob: bd5053e232ef4a03504f244c28f430676cf5ab9f (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
120
121
122
123
124
125
126
127
128
129
130
131
// use std::env; // TODO: Get args from here
use std::collections::HashMap;
use rust_decimal::prelude::*;
use rust_decimal_macros::dec;

#[derive(Debug)]
enum Value {
        Str(String),
        Num(Decimal),
}

enum Mode {
    Integer,
    Decimal,
    Str,
    Command
}


fn main() {
    let mut state = RPState {
        registers: HashMap::new(),
        stack: Vec::new(),
        mode: Mode::Command,
        wip_str: String::from(""),
        num: dec!(0.0),
        decimal_offset: dec!(1),
    };

    let result = eval(String::from("123 456 7.89+"), &mut state);
    match result {
        Ok(()) => println!("{:?}", state.stack),
        Err(msg) => println!("Error: {}", msg),
    }
}

const RADIX: u32 = 10;
const INTEGER_RADIX: Decimal = dec!(10);

fn to_decimal(input: u32) -> Decimal {
    <u32 as Into<Decimal>>::into(input)
}


struct RPState {
    registers: HashMap<String, Value>,
    stack: Vec<Value>,
    mode: Mode,
    wip_str: String,
    num: Decimal,
    decimal_offset: Decimal
}

fn command(c: char, state: &mut RPState) -> Result<(), String> { 
    if c.is_digit(RADIX) {
        state.mode = Mode::Integer;
        state.num = dec!(0);
        state.num += to_decimal(c.to_digit(10).ok_or_else(|| format!("{} isn't a digit", c))?);
        Ok(())
    } else if c == ' ' {
        // do nothing
        Ok(())
    } else if c == '+' {
        
        let a = state.stack.pop().ok_or_else(|| "Data underflow!")?;
        let b = state.stack.pop().ok_or_else(|| "Data underflow!")?;

        match (a, b) {
            (Value::Num(c), Value::Num(d)) => { 
                state.stack.push(Value::Num(c + d));
            }

            _ => return Err("Invalid operation + on non-numbers!".to_string())
        }
        Ok(())
    } else {
        panic!("{} isn't implemented yet!", c)
    }
}

fn integer(c: char, state: &mut RPState) -> Result<(), String> {
    if c.is_digit(RADIX) {
        state.num *= INTEGER_RADIX;
        state.num += to_decimal(c.to_digit(10).unwrap());
    } else if c == '.' {
        state.decimal_offset = dec!(1);
        state.mode = Mode::Decimal;
    }
    else {
        state.stack.push(Value::Num(state.num));
        state.mode = Mode::Command;
        return command(c, state);
    }
    return Ok(());
}

fn decimal(c: char, state: &mut RPState) -> Result<(), String> {
    if c.is_digit(RADIX) {
        state.decimal_offset *= dec!(0.1);
        state.num += to_decimal(c.to_digit(10).unwrap()) * state.decimal_offset;
    } else {
        state.stack.push(Value::Num(state.num));
        state.mode = Mode::Command;
        return command(c, state);
    }
    return Ok(());
}

fn eval(input: String, state: &mut RPState) -> Result<(), String> {
    for (_cpos, c) in input.char_indices() {
        let res = match state.mode {
            Mode::Command => command(c, state),
            Mode::Integer => integer(c, state),
            Mode::Decimal => decimal(c, state),
            Mode::Str => {
                panic!("Strings not implemented yet");
            }
        };
        if res.is_err() {
            return res
        }
    }
    match state.mode {
        Mode::Integer | Mode::Decimal => { 
            state.stack.push(Value::Num(state.num));
            state.mode = Mode::Command;
        },
        _ => {}
    };
    return Ok(());
}