diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 3210b0d..0db092b 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -14,19 +14,20 @@ use super::token::RPAREN; use super::token::SEMICOLON; use super::token::Token; -struct Lexer { +pub struct Lexer { input: String, position: Option, read_pos: Option, ch: Option, } -trait LexerTraits { +pub trait LexerTraits { fn read_char(&mut self); fn next_token(&mut self) -> Token; + fn read_identifier(&mut self) -> String; } -fn new(input_str: String) -> Lexer { +pub fn new(input_str: String) -> Lexer { let mut l: Lexer = Lexer { input: input_str, position: Some(0), @@ -71,15 +72,29 @@ impl LexerTraits for Lexer { '{' => tok = new_token(LBRACE, lit), '}' => tok = new_token(RBRACE, lit), _ => { - tok = Token { - type_: String::from(EOF), - literal: String::from(""), + if c.is_alphabetic() { + let _lit_ = self.read_identifier(); + return tok; + } else { + tok = new_token(ILLEGAL, lit); } } } self.read_char(); tok } + + fn read_identifier(&mut self) -> String { + let position = self.position.unwrap(); + while self.ch.unwrap().is_ascii_alphabetic() { + self.read_char(); + } + let read_pos = self.position.unwrap() as usize; + let position = position as usize; + let res = &self.input; + let res = &res[position..read_pos]; + String::from(res) + } } fn new_token(token_type: &str, ch: u8) -> Token { diff --git a/src/main.rs b/src/main.rs index 4021de5..79c572f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,20 @@ mod lexer; mod token; +use crate::lexer::LexerTraits; +use lexer::new; + fn main() { - println!("Hello, world!"); + let input = "let five = 5; + let ten = 10; + let add = fn(x, y) { + x + y; + }; + let result = add(five, ten);"; + + let mut l = new(input.to_string()); + + for (i, tok) in (0..10).map(|_| l.next_token()).enumerate() { + println!("{}: {:?}", i, tok.type_); + } }