diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs index 0a13e3e..02473ae 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,17 +1,18 @@ | |||
1 | use std::str; | 1 | use std::str; |
2 | 2 | ||
3 | #[derive(Debug)] | ||
3 | struct Token { | 4 | struct Token { |
4 | kind: String, | 5 | kind: String, |
5 | val: String, | 6 | val: String, |
6 | } | 7 | } |
7 | 8 | ||
8 | fn main() { | 9 | fn main() { |
9 | let input: &str = "2y + 11 + sin(5)"; | 10 | let input: &str = "2y + 11 ^ x"; |
10 | let input = input.replace(" ", ""); | 11 | let input = input.replace(" ", ""); |
11 | println!("{}", input); | 12 | println!("{}", input); |
12 | 13 | ||
13 | let mut num_vec: Vec<char> = vec![]; | 14 | let mut num_vec: String = String::new(); |
14 | let mut char_vec: Vec<char> = vec![]; | 15 | let mut char_vec: String = String::new(); |
15 | 16 | ||
16 | let mut result: Vec<Token> = vec![]; | 17 | let mut result: Vec<Token> = vec![]; |
17 | 18 | ||
@@ -21,19 +22,42 @@ fn main() { | |||
21 | } else if letter == '.' { | 22 | } else if letter == '.' { |
22 | num_vec.push(letter); | 23 | num_vec.push(letter); |
23 | } else if letter.is_alphabetic() { | 24 | } else if letter.is_alphabetic() { |
24 | if num_vec.len() > 0 { | 25 | let literal = buffer_to_token("Literal", &num_vec[..]); |
25 | let token = Token { | 26 | if let Some(x) = literal { |
26 | kind: "Literal".into(), | 27 | result.push(x); |
27 | val: num_vec.iter().collect::<String>() | ||
28 | }; | ||
29 | result.push(token); | ||
30 | char_vec.push(letter); | ||
31 | } | 28 | } |
29 | char_vec.push(letter); | ||
30 | num_vec.clear(); | ||
32 | } else if is_operator(letter) { | 31 | } else if is_operator(letter) { |
33 | 32 | let literal = buffer_to_token("Literal", &num_vec[..]); | |
33 | let variable = buffer_to_token("Variable", &char_vec[..]); | ||
34 | if let Some(x) = literal { | ||
35 | result.push(x); | ||
36 | } | ||
37 | if let Some(x) = variable { | ||
38 | result.push(x); | ||
39 | } | ||
40 | num_vec.clear(); | ||
41 | char_vec.clear(); | ||
42 | result.push(Token { kind: "Operator".into(), val: letter.to_string() }); | ||
43 | } | ||
44 | println!("{}", letter); | ||
45 | for token in &result { | ||
46 | println!("{:?}", token); | ||
34 | } | 47 | } |
35 | } | 48 | } |
49 | } | ||
36 | 50 | ||
51 | fn buffer_to_token(k: &str, v: &str) -> Option<Token> { | ||
52 | if v.len() > 0 { | ||
53 | let token = Token { | ||
54 | kind: k.into(), | ||
55 | val: v.chars().collect::<String>() | ||
56 | }; | ||
57 | Some(token) | ||
58 | } else { | ||
59 | None | ||
60 | } | ||
37 | } | 61 | } |
38 | 62 | ||
39 | fn is_operator(x: char) -> bool { | 63 | fn is_operator(x: char) -> bool { |