aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs68
1 files changed, 36 insertions, 32 deletions
diff --git a/src/main.rs b/src/main.rs
index 6a97d1c..c91b662 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,44 +1,41 @@
1#[derive(Debug, Copy, Clone, PartialEq)]
2pub struct Operator {
3 token: char,
4 operation: fn(f64, f64) -> f64,
5 precedence: u8,
6 is_left_associative: bool,
7}
8
1#[derive(Debug)] 9#[derive(Debug)]
2pub enum Token { 10pub enum Token {
3 Operator(char), 11 Operator(Operator),
4 Num(f64), 12 Num(f64),
5 LParen, 13 LParen,
6 RParen 14 RParen
7} 15}
8 16
9#[derive(Debug)] 17impl Operator {
10pub struct Node { 18 fn token_from_op(token: char, operation: fn(f64, f64) -> f64, precedence: u8, is_left_associative: bool) -> Token {
11 value: Option<Token>, 19 Token::Operator(
12 left: Box<Option<Node>>, 20 Operator {
13 right: Box<Option<Node>> 21 token,
14} 22 operation,
15 23 precedence,
16impl Node { 24 is_left_associative
17 fn new() -> Node { 25 }
18 Node { 26 )
19 value: None,
20 left: Box::new(None),
21 right: Box::new(None)
22 }
23 }
24 fn set_val(&mut self, val: Token) {
25 self.value = Some(val);
26 }
27 fn set_left(&mut self, val: Node) {
28 self.left = Box::new(Some(val));
29 } 27 }
30 fn set_right(&mut self, val: Node) { 28 fn operate(self, x: f64, y: f64) -> f64 {
31 self.right = Box::new(Some(val)); 29 (self.operation)(x, y)
32 } 30 }
33} 31}
34 32
35fn main() { 33fn main() {
36 let input = "11 + (1 + 2(3))"; 34 let input = "1(2)";
37 let input = input.replace(" ", ""); 35 let input = input.replace(" ", "");
38
39 let lexed = lexer(&input); 36 let lexed = lexer(&input);
40
41 println!("{:?}", lexed); 37 println!("{:?}", lexed);
38 println!("{}", lexed.unwrap().len());
42} 39}
43 40
44fn lexer(input: &str) -> Result<Vec<Token>, String> { 41fn lexer(input: &str) -> Result<Vec<Token>, String> {
@@ -57,18 +54,26 @@ fn lexer(input: &str) -> Result<Vec<Token>, String> {
57 num_vec.clear(); 54 num_vec.clear();
58 } 55 }
59 // finish 56 // finish
60 result.push(Token::Operator(letter)); 57 let operator_token: Token = match letter {
58 '+' => Operator::token_from_op('+', |x, y| x + y, 2, true),
59 '-' => Operator::token_from_op('-', |x, y| x - y, 2, true),
60 '/' => Operator::token_from_op('/', |x, y| x / y, 3, true),
61 '*' => Operator::token_from_op('*', |x, y| x * y, 3, true),
62 '^' => Operator::token_from_op('^', |x, y| x.powf(y), 4, false),
63 _ => panic!("unexpected op whuuu"),
64 };
65 result.push(operator_token);
61 }, 66 },
62 '(' => { 67 '(' => {
63 // parse num buf 68 // parse num buf
64 let parse_num = num_vec.parse::<f64>().ok(); 69 let parse_num = num_vec.parse::<f64>().ok();
65 if let Some(x) = parse_num { 70 if let Some(x) = parse_num {
66 result.push(Token::Num(x)); 71 result.push(Token::Num(x));
67 result.push(Token::Operator('*')); 72 result.push(Operator::token_from_op('*', |x, y| x * y, 3, true));
68 num_vec.clear(); 73 num_vec.clear();
69 } 74 }
70 // finish 75 // finish
71 result.push(Token::RParen); 76 result.push(Token::LParen);
72 }, 77 },
73 ')' => { 78 ')' => {
74 // parse num buf 79 // parse num buf
@@ -78,8 +83,9 @@ fn lexer(input: &str) -> Result<Vec<Token>, String> {
78 num_vec.clear(); 83 num_vec.clear();
79 } 84 }
80 // finish 85 // finish
81 result.push(Token::LParen); 86 result.push(Token::RParen);
82 } 87 }
88 ' ' => {}
83 _ => { 89 _ => {
84 return Err(format!("Unexpected character: {}", letter)) 90 return Err(format!("Unexpected character: {}", letter))
85 } 91 }
@@ -87,5 +93,3 @@ fn lexer(input: &str) -> Result<Vec<Token>, String> {
87 } 93 }
88 Ok(result) 94 Ok(result)
89} 95}
90
91