aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 7e2057ce07e59e062dceeb887978318b7f4a3c39 (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
use std::str;

#[derive(Debug)]
struct Token {
    kind: String,
    val: String,
}

fn main() {
    let input: &str = "2y + 11(1 + 2 + 3) + 12 /    4";
    let input = input.replace(" ", "");
    println!("{}", input);

    let mut num_vec: String = String::new();
    let mut char_vec: String = String::new();

    let mut result: Vec<Token> = vec![];

    for letter in input.chars() {
        if letter.is_digit(10) {
            num_vec.push(letter);
        } else if letter == '.' {
            num_vec.push(letter);
        } else if letter.is_alphabetic() {
            drain_buffer("Literal", &num_vec[..], &mut result);
            char_vec.push(letter);
            num_vec.clear();
        } else if is_operator(letter) {
            drain_buffer("Literal", &num_vec[..], &mut result);
            num_vec.clear();
            drain_buffer("Variable", &char_vec[..], &mut result);
            char_vec.clear();
            result.push(Token { kind: "Operator".into(), val: letter.to_string() });
        } else if letter == '(' {
            if char_vec.len() > 0 {
                drain_buffer("Function", &char_vec[..], &mut result);
                char_vec.clear();
            } else if num_vec.len() > 0 {
                drain_buffer("Literal", &num_vec[..], &mut result);
                result.push( Token{kind: "Operator".into(), val: "*".to_string()} );
                num_vec.clear();
            }
            result.push( Token{kind: "Left Paren".into(), val: letter.to_string()} );
        } else if letter == ')' {
            drain_buffer("Literal", &num_vec[..], &mut result);
            num_vec.clear();
            drain_buffer("Variable", &char_vec[..], &mut result);
            char_vec.clear();
            result.push(Token { kind: "Right Paren".into(), val: letter.to_string() });
        } else if letter == ',' {
            drain_buffer("Literal", &num_vec[..], &mut result);
            num_vec.clear();
            drain_buffer("Variable", &char_vec[..], &mut result);
            char_vec.clear();
            result.push(Token { kind: "Function Arg Separator".into(), val: letter.to_string() });
        }
    }
    drain_buffer("Literal", &num_vec[..], &mut result);
    num_vec.clear();
    drain_buffer("Variable", &char_vec[..], &mut result);
    char_vec.clear();
    for token in &result {
        println!("{} => {}", token.kind, token.val);
    }
}

fn drain_buffer(k: &str, v: &str, result: &mut Vec<Token>) {
    if v.len() > 0 {
        let token = Token {
            kind: k.into(),
            val: v.chars().collect::<String>()
        };
        result.push(token);
    }
}

fn is_operator(x: char) -> bool {
    match x {
        '+' | '-' | '/' | '*' | '^' => true,
        _ => false
    }
}