update
This commit is contained in:
+47
-9
@@ -1,23 +1,61 @@
|
|||||||
|
use super::token;
|
||||||
|
use super::token::Token;
|
||||||
|
|
||||||
struct Lexer {
|
struct Lexer {
|
||||||
input: String,
|
input: String,
|
||||||
position: Option<i32>,
|
position: Option<i32>,
|
||||||
read_pos: 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 {
|
fn new(input_str: String) -> Lexer {
|
||||||
let l: Lexer = Lexer {
|
let mut l: Lexer = Lexer {
|
||||||
input: input_str,
|
input: input_str,
|
||||||
position: None,
|
position: Some(0),
|
||||||
read_pos: None,
|
read_pos: Some(1),
|
||||||
ch: None,
|
ch: Some(0),
|
||||||
};
|
};
|
||||||
|
l.read_char();
|
||||||
l
|
l
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_char(l: Lexer) {
|
impl LexerTraits for Lexer {
|
||||||
let read_pos = usize::try_from(l.read_pos.unwrap());
|
fn read_char(&mut self) {
|
||||||
if read_pos >= l.input.len() {
|
let read_pos = usize::try_from(self.read_pos.unwrap());
|
||||||
l.ch = 0;
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
struct Token {
|
pub struct Token {
|
||||||
type_: String,
|
pub type_: String,
|
||||||
literal: String,
|
pub literal: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
const ILLEGAL: &str = "ILLEGAL";
|
const ILLEGAL: &str = "ILLEGAL";
|
||||||
|
|||||||
Reference in New Issue
Block a user