Add logic for Operators
All checks were successful
/ build (push) Successful in 1m39s

This commit is contained in:
xqtc161 2024-07-11 17:59:11 +02:00
parent 52828a7f0b
commit a74b520231
2 changed files with 44 additions and 4 deletions

View file

@ -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"
}
},

View file

@ -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('('),