aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2019-02-24 08:52:18 +0000
committerNerdyPepper <[email protected]>2019-02-24 08:52:18 +0000
commit975f606aaa0657686c5e05912977f7fb683e17e3 (patch)
tree57e48df7f4d8ba0872338cb605655c43b225d2b2 /src
parenta532676b17af062f403a2eac4299e2ba295e51b7 (diff)
simplify emptying of NB and CB
Diffstat (limited to 'src')
-rw-r--r--src/main.rs56
1 files changed, 35 insertions, 21 deletions
diff --git a/src/main.rs b/src/main.rs
index 02473ae..7e2057c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,7 +7,7 @@ struct Token {
7} 7}
8 8
9fn main() { 9fn main() {
10 let input: &str = "2y + 11 ^ x"; 10 let input: &str = "2y + 11(1 + 2 + 3) + 12 / 4";
11 let input = input.replace(" ", ""); 11 let input = input.replace(" ", "");
12 println!("{}", input); 12 println!("{}", input);
13 13
@@ -22,41 +22,55 @@ fn main() {
22 } else if letter == '.' { 22 } else if letter == '.' {
23 num_vec.push(letter); 23 num_vec.push(letter);
24 } else if letter.is_alphabetic() { 24 } else if letter.is_alphabetic() {
25 let literal = buffer_to_token("Literal", &num_vec[..]); 25 drain_buffer("Literal", &num_vec[..], &mut result);
26 if let Some(x) = literal {
27 result.push(x);
28 }
29 char_vec.push(letter); 26 char_vec.push(letter);
30 num_vec.clear(); 27 num_vec.clear();
31 } else if is_operator(letter) { 28 } else if is_operator(letter) {
32 let literal = buffer_to_token("Literal", &num_vec[..]); 29 drain_buffer("Literal", &num_vec[..], &mut result);
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(); 30 num_vec.clear();
31 drain_buffer("Variable", &char_vec[..], &mut result);
41 char_vec.clear(); 32 char_vec.clear();
42 result.push(Token { kind: "Operator".into(), val: letter.to_string() }); 33 result.push(Token { kind: "Operator".into(), val: letter.to_string() });
34 } else if letter == '(' {
35 if char_vec.len() > 0 {
36 drain_buffer("Function", &char_vec[..], &mut result);
37 char_vec.clear();
38 } else if num_vec.len() > 0 {
39 drain_buffer("Literal", &num_vec[..], &mut result);
40 result.push( Token{kind: "Operator".into(), val: "*".to_string()} );
41 num_vec.clear();
42 }
43 result.push( Token{kind: "Left Paren".into(), val: letter.to_string()} );
44 } else if letter == ')' {
45 drain_buffer("Literal", &num_vec[..], &mut result);
46 num_vec.clear();
47 drain_buffer("Variable", &char_vec[..], &mut result);
48 char_vec.clear();
49 result.push(Token { kind: "Right Paren".into(), val: letter.to_string() });
50 } else if letter == ',' {
51 drain_buffer("Literal", &num_vec[..], &mut result);
52 num_vec.clear();
53 drain_buffer("Variable", &char_vec[..], &mut result);
54 char_vec.clear();
55 result.push(Token { kind: "Function Arg Separator".into(), val: letter.to_string() });
43 } 56 }
44 println!("{}", letter); 57 }
45 for token in &result { 58 drain_buffer("Literal", &num_vec[..], &mut result);
46 println!("{:?}", token); 59 num_vec.clear();
47 } 60 drain_buffer("Variable", &char_vec[..], &mut result);
61 char_vec.clear();
62 for token in &result {
63 println!("{} => {}", token.kind, token.val);
48 } 64 }
49} 65}
50 66
51fn buffer_to_token(k: &str, v: &str) -> Option<Token> { 67fn drain_buffer(k: &str, v: &str, result: &mut Vec<Token>) {
52 if v.len() > 0 { 68 if v.len() > 0 {
53 let token = Token { 69 let token = Token {
54 kind: k.into(), 70 kind: k.into(),
55 val: v.chars().collect::<String>() 71 val: v.chars().collect::<String>()
56 }; 72 };
57 Some(token) 73 result.push(token);
58 } else {
59 None
60 } 74 }
61} 75}
62 76