diff --git a/src/lex/tok.rs b/src/lex/tok.rs index 5db9a8f..6758e93 100644 --- a/src/lex/tok.rs +++ b/src/lex/tok.rs @@ -1,4 +1,5 @@ #![allow(unused)] +use std::string::String; use logos::Logos; use logos::Lexer; use core::iter::Peekable; @@ -84,11 +85,11 @@ pub enum Token { // LITERALS #[regex(r#"("[^"]*")|('[^']*')"#)] - String, // A string literal. + StringLiteral, // A string literal. #[regex("[0-9]+", |lex| lex.slice().parse().ok())] - Number(u64), // An integer. - #[regex(r#"[^[0-9]^"^-^[ \t\n\f]^\.^=^(^)^{^}.^,^;^[+-/*%]]+[^"^-^=^\..^[ \t\n\f]^(^)^{^}^,^;^[+-/*%]]*"#)] - Identifier, // An identifier. + Number(u64), // An integer. + #[regex(r#"[^[0-9]^"^-^[ \t\n\f]^\.^=^(^)^{^}.^,^;^[+-/*%]]+[^"^-^=^\..^[ \t\n\f]^(^)^{^}^,^;^[+-/*%]]*"#, |lex| lex.slice().to_owned())] + Identifier(String), // An identifier. #[token("true")] True, // true #[token("false")] @@ -147,7 +148,7 @@ impl std::fmt::Display for Token { GreaterEqual => write!(f, ">="), String => write!(f, "[string]"), Number(n) => write!(f, "{}", n), - Identifier => write!(f, "identifier"), + Identifier(i) => write!(f, "{}", i), True => write!(f, "true"), False => write!(f, "false"), Null => write!(f, "none"), diff --git a/src/parse/parse.rs b/src/parse/parse.rs index eb68050..302f33d 100644 --- a/src/parse/parse.rs +++ b/src/parse/parse.rs @@ -10,38 +10,34 @@ macro_rules! unwrap { } } -pub fn parse_math(mut tokens: Lexer) -> Option { - if let Some(left) = parse_math(tokens) { - if let Some(op) = match_operator(tokens) { - if let Some(Ok(Number(right))) = tokens.next() { - return Some(Expr::MathExpr(Math {left: Rc::new(left), right: Rc::new(Expr::Number(right)), operator: op})); - } - } else { - +pub fn expect_number(n: Option>) -> Expr<'static> { + match n { + Some(Ok(Number(res))) => Expr::Number(res), + Some(Ok(tok)) => { + println!("Expected a number, got: {}", tok); + std::process::exit(1); + } + _ => { + println!("Expected a number, got EOF"); + std::process::exit(1); } } - - None } -pub fn parse_global_declaration(mut tokens: Lexer) -> Option { - let mut tok = None; - if unwrap!(tokens) == Let && unwrap!(tokens) == Identifier { - let name = tokens.slice(); - - println!("{:?}", name); - if unwrap!(tokens) == Equal { - let temp_token = unwrap!(tokens); - if let Number(n) = temp_token { - tok = Some(Expr::GlobalDefinition(Rc::new(VarDefinition {name, value: n}))); - } - } - } - tok +pub fn parse_math(mut tokens: Lexer) -> Option { + let mut left = expect_number(tokens.next()); + + loop { + let op = tokens.next(); + if !match_operator(&op).is_some() { break } + let right = expect_number(tokens.next()); + left = Expr::MathExpr(Math {left: Rc::new(left), right: Rc::new(right), operator: match_operator(&op)?}); + } + Some(left) } -pub fn match_operator(mut tokens: Lexer) -> Option { - if let Some(Ok(token)) = tokens.next() { +pub fn match_operator(token: &Option>) -> Option { + if let Some(Ok(token)) = token { return match token { Plus => Some(MathOperator::OP_ADD), Minus => Some(MathOperator::OP_SUB),