Some refactoring
All checks were successful
/ build (push) Successful in 1m34s
/ clippy (push) Successful in 1m10s

This commit is contained in:
xqtc 2024-07-25 20:01:35 +02:00
parent 1561375d38
commit 513f2f86f2
5 changed files with 23 additions and 11 deletions

View file

@ -15,10 +15,10 @@ jobs:
with: with:
command: build command: build
args: --release args: --release
# - uses: https://github.com/actions-rs/cargo@v1 - uses: https://github.com/actions-rs/cargo@v1
# with: with:
# command: test command: test
# args: --lib args: --lib
clippy: clippy:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View file

@ -37,7 +37,7 @@ impl Lexer {
if c.is_numeric() { if c.is_numeric() {
number.push(c); number.push(c);
self.advance(); self.advance();
} else if c == '.' { } else if c == '.' || c == 'e' {
is_float = true; is_float = true;
number.push(c); number.push(c);
self.advance() self.advance()

View file

@ -1,9 +1,9 @@
use crate::lex::util::populate_symbols; use crate::lex::util::make_symbols;
use crate::tokens::Token; use crate::tokens::Token;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use super::util::populate_keywords; use super::util::make_keywords;
pub struct Lexer { pub struct Lexer {
/// GLSL source /// GLSL source
@ -29,8 +29,8 @@ impl Lexer {
input: input.chars().collect(), input: input.chars().collect(),
position: 0, position: 0,
current_char: None, current_char: None,
keywords: populate_keywords(), keywords: make_keywords(),
symbols: populate_symbols(), symbols: make_symbols(),
}; };
dbg!("{}", &lexer.keywords); dbg!("{}", &lexer.keywords);
lexer.current_char = if lexer.position < lexer.input.len() { lexer.current_char = if lexer.position < lexer.input.len() {

View file

@ -2,7 +2,7 @@ use std::collections::HashMap;
use crate::tokens::{Image, Material, Sampler, Token, Vector}; use crate::tokens::{Image, Material, Sampler, Token, Vector};
pub fn populate_symbols() -> HashMap<char, Token> { pub fn make_symbols() -> HashMap<char, Token> {
let mut symbols = HashMap::new(); let mut symbols = HashMap::new();
symbols.insert('{', Token::LEFT_BRACE); symbols.insert('{', Token::LEFT_BRACE);
symbols.insert('}', Token::RIGHT_BRACE); symbols.insert('}', Token::RIGHT_BRACE);
@ -17,7 +17,7 @@ pub fn populate_symbols() -> HashMap<char, Token> {
symbols symbols
} }
pub fn populate_keywords() -> HashMap<&'static str, Token> { pub fn make_keywords() -> HashMap<&'static str, Token> {
let mut keywords = HashMap::new(); let mut keywords = HashMap::new();
keywords.insert("const", Token::CONST); keywords.insert("const", Token::CONST);
keywords.insert("uniform", Token::UNIFORM); keywords.insert("uniform", Token::UNIFORM);

View file

@ -112,6 +112,18 @@ mod tests {
vec![Token::FLOATCONSTANT(".4504".to_string()), Token::EOF].into() vec![Token::FLOATCONSTANT(".4504".to_string()), Token::EOF].into()
); );
} }
#[test]
fn float_with_e_strogen() {
let source_code = "1e38";
let mut lexer = Lexer::new(source_code);
let tokens = lexer.get_tokens();
assert_eq!(
tokens,
vec![Token::FLOATCONSTANT("1e38".to_string()), Token::EOF].into()
);
}
#[test] #[test]
fn integer_literal() { fn integer_literal() {
let source_code = "12345"; let source_code = "12345";