fix u8 error

This commit is contained in:
YannAhlgrim
2026-05-12 16:43:24 +02:00
parent a1fc32d7a3
commit 5fc4d355a8
3 changed files with 16 additions and 10 deletions
+13 -7
View File
@@ -3,12 +3,9 @@ use crate::token::lookup_ident;
use super::token::ASSIGN;
use super::token::COMMA;
use super::token::EOF;
use super::token::FUNCTION;
use super::token::IDENT;
use super::token::ILLEGAL;
use super::token::INT;
use super::token::LBRACE;
use super::token::LET;
use super::token::LPAREN;
use super::token::PLUS;
use super::token::RBRACE;
@@ -34,8 +31,8 @@ pub trait LexerTraits {
pub fn new(input_str: String) -> Lexer {
let mut l: Lexer = Lexer {
input: input_str,
position: Some(0),
read_pos: Some(1),
position: Some(-1),
read_pos: Some(0),
ch: Some(0),
};
l.read_char();
@@ -68,6 +65,7 @@ impl LexerTraits for Lexer {
let c = char::from(lit);
match c {
'\0' => tok = new_token(EOF, lit),
'=' => tok = new_token(ASSIGN, lit),
';' => tok = new_token(SEMICOLON, lit),
'(' => tok = new_token(LPAREN, lit),
@@ -80,10 +78,10 @@ impl LexerTraits for Lexer {
if c.is_alphabetic() {
let lit = self.read_identifier();
let tok_type = lookup_ident(&lit);
return new_token(tok_type, lit.as_bytes()[0]);
return new_token_from_str(tok_type, lit);
} else if c.is_ascii_digit() {
let lit = self.read_number();
return new_token(INT, lit.as_bytes()[0]);
return new_token_from_str(INT, lit);
} else {
tok = new_token(ILLEGAL, lit);
}
@@ -135,3 +133,11 @@ fn new_token(token_type: &str, ch: u8) -> Token {
literal: lit,
}
}
fn new_token_from_str(token_type: &str, lit: String) -> Token {
let token_type = String::from(token_type);
Token {
type_: token_type,
literal: lit,
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ fn main() {
let mut l = new(input.to_string());
for (i, tok) in (0..36).map(|_| l.next_token()).enumerate() {
for (_i, tok) in (0..37).map(|_| l.next_token()).enumerate() {
println!("{}: {:?}", tok.type_, tok.literal);
}
}
+2 -2
View File
@@ -21,10 +21,10 @@ pub const RBRACE: &str = "}";
pub const FUNCTION: &str = "FUNCTION";
pub const LET: &str = "LET";
pub fn lookup_ident(ident: &str) -> &str {
pub fn lookup_ident(ident: &str) -> &'static str {
match ident {
"fn" => FUNCTION,
"let" => LET,
_ => ILLEGAL,
_ => IDENT,
}
}