From a74b52023134f90f98f2b38b627cee43d3631927 Mon Sep 17 00:00:00 2001 From: xqtc161 Date: Thu, 11 Jul 2024 17:59:11 +0200 Subject: [PATCH] Add logic for Operators --- flake.lock | 4 ++-- src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/flake.lock b/flake.lock index b894095..3a35f50 100644 --- a/flake.lock +++ b/flake.lock @@ -81,11 +81,11 @@ "locked": { "lastModified": 1, "narHash": "sha256-8wkkYGr1dPSnX9oVMX8D6dTOROXKOYpBTKfriA0sEBI=", - "path": "/nix/store/7rlwm9hr4df367974cszjpbmjbd36jvn-source/flake.systems.nix", + "path": "/nix/store/7v3i06afpf7zfq4cwingjhfhmbc6vbqi-source/flake.systems.nix", "type": "path" }, "original": { - "path": "/nix/store/7rlwm9hr4df367974cszjpbmjbd36jvn-source/flake.systems.nix", + "path": "/nix/store/7v3i06afpf7zfq4cwingjhfhmbc6vbqi-source/flake.systems.nix", "type": "path" } }, diff --git a/src/lib.rs b/src/lib.rs index 322af8d..c742476 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,7 +94,14 @@ impl Lexer { } else if c == '/' && self.peek() == Some('/') { tokens.push(self.consume_comment()); } else { - tokens.push(self.consume_symbol()); + match c { + '+' | '-' | '*' | '/' | '%' | '&' | '|' | '^' | '!' | '=' | '<' | '>' | '?' => { + tokens.push(self.consume_operator()); + } + _ => { + tokens.push(self.consume_symbol()); + } + } } } tokens.push(Token::EOF); @@ -176,6 +183,11 @@ impl Lexer { self.advance(); Token::Symbol(symbol) } + fn consume_operator(&mut self) -> Token { + let operator = self.current_char.unwrap(); + self.advance(); + Token::Operator(operator.to_string()) + } fn error(&self, message: &str) -> ! { panic!("Lexer error at position {}: {}", self.position, message); @@ -266,6 +278,34 @@ mod tests { assert_eq!(tokens, vec![Token::FloatLiteral(123.45), Token::EOF]); } + #[test] + fn test_operator() { + init(); + let source = "+-*/%&|^!=<>?"; + let mut lexer = Lexer::new(source); + let tokens = lexer.get_tokens(); + info!("[Operator] Tokens: {:#?}", tokens); + assert_eq!( + tokens, + vec![ + Token::Operator("+".to_string()), + Token::Operator("-".to_string()), + Token::Operator("*".to_string()), + Token::Operator("/".to_string()), + Token::Operator("%".to_string()), + Token::Operator("&".to_string()), + Token::Operator("|".to_string()), + Token::Operator("^".to_string()), + Token::Operator("!".to_string()), + Token::Operator("=".to_string()), + Token::Operator("<".to_string()), + Token::Operator(">".to_string()), + Token::Operator("?".to_string()), + Token::EOF, + ] + ); + } + #[test] fn test_single_line_comment() { init(); @@ -317,7 +357,7 @@ mod tests { Token::Whitespace, Token::Identifier("gl_FragColor".to_string()), Token::Whitespace, - Token::Symbol('='), + Token::Operator('='.to_string()), Token::Whitespace, Token::Identifier("vec4".to_string()), Token::Symbol('('),