aboutsummaryrefslogtreecommitdiff
path: root/src/lex/mod.rs
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2019-03-22 13:56:47 +0000
committerNerdyPepper <[email protected]>2019-03-22 13:56:47 +0000
commita5238794efce5767139d9c4dbdf056bb859921d1 (patch)
treeeb49cc23220ef52dd003d5335287122c94c18f4d /src/lex/mod.rs
parent62c29360c8e7d0a56599ea4c42f72fadee0eec14 (diff)
support unary operators (minus and plus)
Diffstat (limited to 'src/lex/mod.rs')
-rw-r--r--src/lex/mod.rs22
1 files changed, 19 insertions, 3 deletions
diff --git a/src/lex/mod.rs b/src/lex/mod.rs
index 7fd5e33..47e35b0 100644
--- a/src/lex/mod.rs
+++ b/src/lex/mod.rs
@@ -71,15 +71,31 @@ pub fn lexer(input: &str) -> Result<Vec<Token>, String> {
71 } 71 }
72 char_vec.push(letter); 72 char_vec.push(letter);
73 }, 73 },
74 '+' | '-' | '/' | '*' | '^' => { 74 '+' | '-' => {
75 let op_token = match letter {
76 '+' => Operator::token_from_op('+', |x, y| x + y, 2, true),
77 '-' => Operator::token_from_op('-', |x, y| x - y, 2, true),
78 _ => unreachable!()
79 };
80 let parse_num = num_vec.parse::<f64>().ok();
81 if let Some(x) = parse_num {
82 result.push(Token::Num(x));
83 num_vec.clear();
84 result.push(op_token);
85 } else {
86 result.push(Token::LParen);
87 result.push(Token::Num((letter.to_string() + "1").parse::<f64>().unwrap()));
88 result.push(Token::RParen);
89 result.push(Operator::token_from_op('*', |x, y| x * y, 2, true));
90 }
91 },
92 '/' | '*' | '^' => {
75 let parse_num = num_vec.parse::<f64>().ok(); 93 let parse_num = num_vec.parse::<f64>().ok();
76 if let Some(x) = parse_num { 94 if let Some(x) = parse_num {
77 result.push(Token::Num(x)); 95 result.push(Token::Num(x));
78 num_vec.clear(); 96 num_vec.clear();
79 } 97 }
80 let operator_token: Token = match letter { 98 let operator_token: Token = match letter {
81 '+' => Operator::token_from_op('+', |x, y| x + y, 2, true),
82 '-' => Operator::token_from_op('-', |x, y| x - y, 2, true),
83 '/' => Operator::token_from_op('/', |x, y| x / y, 3, true), 99 '/' => Operator::token_from_op('/', |x, y| x / y, 3, true),
84 '*' => Operator::token_from_op('*', |x, y| x * y, 3, true), 100 '*' => Operator::token_from_op('*', |x, y| x * y, 3, true),
85 '^' => Operator::token_from_op('^', |x, y| x.powf(y), 4, false), 101 '^' => Operator::token_from_op('^', |x, y| x.powf(y), 4, false),