Compare commits

...

3 Commits
main ... andres

Author SHA1 Message Date
Alpha Chen 3707f7be91
mu
2 years ago
Alpha Chen 3dc84e9f28
mu
2 years ago
Alpha Chen e8f5059c9b
mu
2 years ago

@ -1,4 +1,4 @@
use crate::scanner::{Scanner, TokenKind};
use crate::scanner::Scanner;
use color_eyre::eyre::Result;
pub fn compile(source: &str) -> Result<()> {

@ -4,6 +4,10 @@ pub struct Scanner<'a> {
line: usize,
}
fn is_alpha(c: char) -> bool {
c.is_ascii_alphabetic() || c == '_'
}
impl<'a> Scanner<'a> {
pub fn new(source: &'a str) -> Self {
Self {
@ -13,22 +17,6 @@ impl<'a> Scanner<'a> {
}
}
fn make_token(&self, kind: TokenKind) -> Token<'a> {
Token {
kind,
value: std::str::from_utf8(&self.source[..self.current]).unwrap(),
line: self.line,
}
}
fn error_token(&self, message: &'static str) -> Token<'a> {
Token {
kind: TokenKind::Error,
value: message,
line: self.line,
}
}
fn advance(&mut self) -> char {
self.current += 1;
self.source[self.current - 1] as char
@ -75,7 +63,7 @@ impl<'a> Scanner<'a> {
self.source.get(self.current).map(|&x| x as char)
}
fn peek_next(&mut self) -> Option<char> {
fn peek_next(&self) -> Option<char> {
self.source.get(self.current + 1).map(|&x| x as char)
}
@ -89,10 +77,11 @@ impl<'a> Scanner<'a> {
if self.peek().is_none() {
// Maybe this should return an Err?
return self.error_token("Unterminated string.");
return Token::error(&self, "Unterminated string.");
}
self.advance();
// Token::new(&self, TokenKind::String)
self.make_token(TokenKind::String)
}
@ -109,6 +98,7 @@ impl<'a> Scanner<'a> {
}
}
// Token::new(&self, TokenKind::Number)
self.make_token(TokenKind::Number)
}
@ -117,6 +107,7 @@ impl<'a> Scanner<'a> {
self.advance();
}
// Token::new(&self, self.identifier_type())
self.make_token(self.identifier_type())
}
@ -147,6 +138,14 @@ impl<'a> Scanner<'a> {
_ => TokenKind::Identifier,
}
}
fn make_token(&self, kind: TokenKind) -> Token<'a> {
Token {
kind: TokenKind::And,
value: std::str::from_utf8(&self.source[..self.current]).unwrap(),
line: self.line,
}
}
}
impl<'a> Iterator for Scanner<'a> {
@ -159,61 +158,76 @@ impl<'a> Iterator for Scanner<'a> {
Some(match self.advance() {
c if is_alpha(c) => self.identifier(),
c if c.is_ascii_digit() => self.number(),
'(' => self.make_token(TokenKind::LeftParen),
')' => self.make_token(TokenKind::RightParen),
'{' => self.make_token(TokenKind::LeftBrace),
'}' => self.make_token(TokenKind::RightBrace),
';' => self.make_token(TokenKind::Semicolon),
',' => self.make_token(TokenKind::Comma),
'.' => self.make_token(TokenKind::Dot),
'-' => self.make_token(TokenKind::Minus),
'+' => self.make_token(TokenKind::Plus),
'/' => self.make_token(TokenKind::Slash),
'*' => self.make_token(TokenKind::Star),
'!' => {
if self.is_match('=') {
self.make_token(TokenKind::BangEqual)
} else {
self.make_token(TokenKind::Bang)
}
}
'=' => {
if self.is_match('=') {
self.make_token(TokenKind::EqualEqual)
} else {
self.make_token(TokenKind::Equal)
}
}
'<' => {
if self.is_match('=') {
self.make_token(TokenKind::LessEqual)
} else {
self.make_token(TokenKind::Less)
}
}
'>' => {
if self.is_match('=') {
self.make_token(TokenKind::GreaterEqual)
} else {
self.make_token(TokenKind::Greater)
}
}
'"' => self.string(),
_ => self.error_token("Unexpected character."),
'(' => Token::new(self, TokenKind::LeftParen),
// '(' => self.make_token(TokenKind::LeftParen),
// ')' => self.make_token(TokenKind::RightParen),
// '{' => self.make_token(TokenKind::LeftBrace),
// '}' => self.make_token(TokenKind::RightBrace),
// ';' => self.make_token(TokenKind::Semicolon),
// ',' => self.make_token(TokenKind::Comma),
// '.' => self.make_token(TokenKind::Dot),
// '-' => self.make_token(TokenKind::Minus),
// '+' => self.make_token(TokenKind::Plus),
// '/' => self.make_token(TokenKind::Slash),
// '*' => self.make_token(TokenKind::Star),
// '!' => {
// if self.is_match('=') {
// self.make_token(TokenKind::BangEqual)
// } else {
// self.make_token(TokenKind::Bang)
// }
// }
// '=' => {
// if self.is_match('=') {
// self.make_token(TokenKind::EqualEqual)
// } else {
// self.make_token(TokenKind::Equal)
// }
// }
// '<' => {
// if self.is_match('=') {
// self.make_token(TokenKind::LessEqual)
// } else {
// self.make_token(TokenKind::Less)
// }
// }
// '>' => {
// if self.is_match('=') {
// self.make_token(TokenKind::GreaterEqual)
// } else {
// self.make_token(TokenKind::Greater)
// }
// }
// '"' => self.string(),
_ => Token::error(self, "Unexpected character."),
})
}
}
fn is_alpha(c: char) -> bool {
c.is_ascii_alphabetic() || c == '_'
}
pub struct Token<'a> {
pub kind: TokenKind,
pub value: &'a str,
pub line: usize,
}
impl<'a> Token<'a> {
fn new(scanner: &Scanner<'a>, kind: TokenKind) -> Self {
Token {
kind,
value: std::str::from_utf8(&scanner.source[..scanner.current]).unwrap(),
line: scanner.line,
}
}
fn error(scanner: &Scanner, message: &'static str) -> Self {
Token {
kind: TokenKind::Error,
value: message,
line: scanner.line,
}
}
}
#[derive(Debug, PartialEq)]
pub enum TokenKind {
// Single-character tokens.

Loading…
Cancel
Save