From 719c5fad0249911dd4e68b96dab0969abeea515b Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Sat, 16 Mar 2019 16:16:53 +0530 Subject: use matches, enums --- src/main.rs | 112 +++++++++++++++++++++++------------------------------------- 1 file changed, 43 insertions(+), 69 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7e2057c..c9d02f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,82 +1,56 @@ -use std::str; - #[derive(Debug)] -struct Token { - kind: String, - val: String, +enum Token { + Operator(char), + Num(f64), + Paren(char) } fn main() { - let input: &str = "2y + 11(1 + 2 + 3) + 12 / 4"; + let input = "11(12)"; let input = input.replace(" ", ""); - println!("{}", input); - let mut num_vec: String = String::new(); - let mut char_vec: String = String::new(); + let lexed = lexer(&input); - let mut result: Vec = vec![]; + println!("{:?}", lexed); +} +fn lexer(input: &str) -> Result, String> { + let mut num_vec: String = String::new(); + let mut result: Vec = 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(); + match letter { + '0'...'9' | '.' => { + num_vec.push(letter); + }, + '+' | '-' | '/' | '*' | '^' => { + let parse_num = num_vec.parse::().ok(); + if let Some(x) = parse_num { + result.push(Token::Num(x)); + num_vec.clear(); + } + result.push(Token::Paren(letter)); + }, + '(' => { + let parse_num = num_vec.parse::().ok(); + if let Some(x) = parse_num { + result.push(Token::Num(x)); + result.push(Token::Operator('*')); + num_vec.clear(); + } + result.push(Token::Paren(letter)); + }, + ')' => { + let parse_num = num_vec.parse::().ok(); + if let Some(x) = parse_num { + result.push(Token::Num(x)); + num_vec.clear(); + } + result.push(Token::Paren(letter)); + } + _ => { + return Err(format!("Unexpected character: {}", letter)) } - 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) { - if v.len() > 0 { - let token = Token { - kind: k.into(), - val: v.chars().collect::() - }; - result.push(token); - } -} - -fn is_operator(x: char) -> bool { - match x { - '+' | '-' | '/' | '*' | '^' => true, - _ => false - } + Ok(result) } -- cgit v1.2.3