This commit is contained in:
YannAhlgrim
2026-05-09 17:42:21 +02:00
parent 68bf2f4288
commit 64e9f9c8c5
2 changed files with 50 additions and 12 deletions
+47 -9
View File
@@ -1,23 +1,61 @@
use super::token;
use super::token::Token;
struct Lexer {
input: String,
position: Option<i32>,
read_pos: Option<i32>,
ch: Option<i8>,
ch: Option<u8>,
}
trait LexerTraits {
fn read_char(&mut self);
fn next_token(&self) -> Token;
}
fn new(input_str: String) -> Lexer {
let l: Lexer = Lexer {
let mut l: Lexer = Lexer {
input: input_str,
position: None,
read_pos: None,
ch: None,
position: Some(0),
read_pos: Some(1),
ch: Some(0),
};
l.read_char();
l
}
fn read_char(l: Lexer) {
let read_pos = usize::try_from(l.read_pos.unwrap());
if read_pos >= l.input.len() {
l.ch = 0;
impl LexerTraits for Lexer {
fn read_char(&mut self) {
let read_pos = usize::try_from(self.read_pos.unwrap());
if read_pos.unwrap() >= self.input.len() {
self.ch = Some(0);
} else {
let bytes = self.input.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if i == read_pos.unwrap() {
self.ch = Some(item);
}
}
}
self.position = self.read_pos;
let new_read_pos = self.read_pos.unwrap() + 1;
self.read_pos = Some(new_read_pos);
}
fn next_token(&self) -> Token {
let tok = Token {
type_: String::from(""),
literal:
}
match self.ch {}
}
}
fn new_token(token_type: String, ch: &u8) {
let lit = String::from_utf8_lossy(ch);
let token = Token {
type_: token_type,
literal: lit
};
}