Removed duplicate code, Fixed Clippy warnings
1. Removed usage of Token::new, Token::with_value with token! macro 2. Fixed issues reported by clippy 3. Reorganized imports a little bit
This commit is contained in:
parent
9e04c61310
commit
aa4e158aab
|
@ -141,7 +141,7 @@ impl TreeWalker {
|
||||||
TokenType::LessThan => Object::Boolean(l < r),
|
TokenType::LessThan => Object::Boolean(l < r),
|
||||||
_ => Object::Error(format!("unknown operator: {} {} {}", l, operator, r)),
|
_ => Object::Error(format!("unknown operator: {} {} {}", l, operator, r)),
|
||||||
},
|
},
|
||||||
(o1 @ _, o2 @ _) if o1.to_string() != o2.to_string() => {
|
(o1, o2) if o1.to_string() != o2.to_string() => {
|
||||||
Object::Error(format!("type mismatch: {} {} {}", o1, operator, o2))
|
Object::Error(format!("type mismatch: {} {} {}", o1, operator, o2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +165,7 @@ impl TreeWalker {
|
||||||
fn eval_minus_prefix_operator_expression(&self, expr: Object) -> Object {
|
fn eval_minus_prefix_operator_expression(&self, expr: Object) -> Object {
|
||||||
match expr {
|
match expr {
|
||||||
Object::Integer(v) => Object::Integer(-v),
|
Object::Integer(v) => Object::Integer(-v),
|
||||||
v @ _ => Object::Error(format!("unknown operator: -{}", v)),
|
v => Object::Error(format!("unknown operator: -{}", v)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
257
src/lexer/mod.rs
257
src/lexer/mod.rs
|
@ -6,15 +6,15 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref IDENTMAP: HashMap<&'static str, Token> = {
|
static ref IDENTMAP: HashMap<&'static str, TokenType> = {
|
||||||
let mut m = HashMap::new();
|
let mut m = HashMap::new();
|
||||||
m.insert("fn", Token::new(TokenType::Function));
|
m.insert("fn", TokenType::Function);
|
||||||
m.insert("let", Token::new(TokenType::Let));
|
m.insert("let", TokenType::Let);
|
||||||
m.insert("true", Token::new(TokenType::True));
|
m.insert("true", TokenType::True);
|
||||||
m.insert("false", Token::new(TokenType::False));
|
m.insert("false", TokenType::False);
|
||||||
m.insert("return", Token::new(TokenType::Return));
|
m.insert("return", TokenType::Return);
|
||||||
m.insert("if", Token::new(TokenType::If));
|
m.insert("if", TokenType::If);
|
||||||
m.insert("else", Token::new(TokenType::Else));
|
m.insert("else", TokenType::Else);
|
||||||
m
|
m
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ pub struct Token {
|
||||||
|
|
||||||
impl Token {
|
impl Token {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(name: TokenType) -> Self {
|
pub const fn new(name: TokenType) -> Self {
|
||||||
Token {
|
Token {
|
||||||
name,
|
name,
|
||||||
literal: None,
|
literal: None,
|
||||||
|
@ -124,6 +124,15 @@ impl Display for Token {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! token {
|
||||||
|
($token_name:expr) => {
|
||||||
|
Token::new($token_name)
|
||||||
|
};
|
||||||
|
($token_name:expr, $value:expr) => {
|
||||||
|
Token::with_value($token_name, $value)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Lexer<'a> {
|
pub struct Lexer<'a> {
|
||||||
input: Peekable<Chars<'a>>,
|
input: Peekable<Chars<'a>>,
|
||||||
|
@ -206,21 +215,21 @@ impl<'a> Iterator for Lexer<'a> {
|
||||||
};
|
};
|
||||||
if is_e {
|
if is_e {
|
||||||
self.read_char();
|
self.read_char();
|
||||||
Some(Token::new(TokenType::Equals))
|
Some(token!(TokenType::Equals))
|
||||||
} else {
|
} else {
|
||||||
Some(Token::new(TokenType::Assign))
|
Some(token!(TokenType::Assign))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some('+') => Some(Token::new(TokenType::Plus)),
|
Some('+') => Some(token!(TokenType::Plus)),
|
||||||
Some('*') => Some(Token::new(TokenType::Asterisk)),
|
Some('*') => Some(token!(TokenType::Asterisk)),
|
||||||
Some('/') => Some(Token::new(TokenType::Slash)),
|
Some('/') => Some(token!(TokenType::Slash)),
|
||||||
Some('-') => Some(Token::new(TokenType::Minus)),
|
Some('-') => Some(token!(TokenType::Minus)),
|
||||||
Some(',') => Some(Token::new(TokenType::Comma)),
|
Some(',') => Some(token!(TokenType::Comma)),
|
||||||
Some(';') => Some(Token::new(TokenType::Semicolon)),
|
Some(';') => Some(token!(TokenType::Semicolon)),
|
||||||
Some('(') => Some(Token::new(TokenType::LParen)),
|
Some('(') => Some(token!(TokenType::LParen)),
|
||||||
Some(')') => Some(Token::new(TokenType::RParen)),
|
Some(')') => Some(token!(TokenType::RParen)),
|
||||||
Some('{') => Some(Token::new(TokenType::LBrace)),
|
Some('{') => Some(token!(TokenType::LBrace)),
|
||||||
Some('}') => Some(Token::new(TokenType::RBrace)),
|
Some('}') => Some(token!(TokenType::RBrace)),
|
||||||
Some('!') => {
|
Some('!') => {
|
||||||
let is_ne = match self.input.peek() {
|
let is_ne = match self.input.peek() {
|
||||||
Some(v) if *v == '=' => true,
|
Some(v) if *v == '=' => true,
|
||||||
|
@ -228,27 +237,27 @@ impl<'a> Iterator for Lexer<'a> {
|
||||||
};
|
};
|
||||||
if is_ne {
|
if is_ne {
|
||||||
self.read_char();
|
self.read_char();
|
||||||
Some(Token::new(TokenType::NotEquals))
|
Some(token!(TokenType::NotEquals))
|
||||||
} else {
|
} else {
|
||||||
Some(Token::new(TokenType::Bang))
|
Some(token!(TokenType::Bang))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some('>') => Some(Token::new(TokenType::GreaterThan)),
|
Some('>') => Some(token!(TokenType::GreaterThan)),
|
||||||
Some('<') => Some(Token::new(TokenType::LessThan)),
|
Some('<') => Some(token!(TokenType::LessThan)),
|
||||||
Some(ch) if is_letter(ch) => {
|
Some(ch) if is_letter(ch) => {
|
||||||
let ident = self.read_identifier(ch);
|
let ident = self.read_identifier(ch);
|
||||||
Some(lookup_ident(&ident))
|
Some(lookup_ident(&ident))
|
||||||
}
|
}
|
||||||
Some(ch) if ch.is_ascii_digit() => {
|
Some(ch) if ch.is_ascii_digit() => {
|
||||||
let number = self.read_number(ch);
|
let number = self.read_number(ch);
|
||||||
Some(Token::with_value(TokenType::Int, &number))
|
Some(token!(TokenType::Int, &number))
|
||||||
}
|
}
|
||||||
None if !self.eof_sent => {
|
None if !self.eof_sent => {
|
||||||
self.eof_sent = true;
|
self.eof_sent = true;
|
||||||
Some(Token::new(TokenType::EOF))
|
Some(token!(TokenType::EOF))
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
_ => Some(Token::new(TokenType::Illegal)),
|
_ => Some(token!(TokenType::Illegal)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -260,8 +269,8 @@ fn is_letter(c: char) -> bool {
|
||||||
|
|
||||||
fn lookup_ident(ident: &str) -> Token {
|
fn lookup_ident(ident: &str) -> Token {
|
||||||
match IDENTMAP.get(&ident) {
|
match IDENTMAP.get(&ident) {
|
||||||
Some(v) => v.clone(),
|
Some(v) => token!(*v),
|
||||||
None => Token::with_value(TokenType::Ident, ident),
|
None => token!(TokenType::Ident, ident),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,15 +283,15 @@ mod tests {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Lexer::new("=+(){},;").collect::<Vec<Token>>(),
|
Lexer::new("=+(){},;").collect::<Vec<Token>>(),
|
||||||
vec![
|
vec![
|
||||||
Token::new(TokenType::Assign),
|
token!(TokenType::Assign),
|
||||||
Token::new(TokenType::Plus),
|
token!(TokenType::Plus),
|
||||||
Token::new(TokenType::LParen),
|
token!(TokenType::LParen),
|
||||||
Token::new(TokenType::RParen),
|
token!(TokenType::RParen),
|
||||||
Token::new(TokenType::LBrace),
|
token!(TokenType::LBrace),
|
||||||
Token::new(TokenType::RBrace),
|
token!(TokenType::RBrace),
|
||||||
Token::new(TokenType::Comma),
|
token!(TokenType::Comma),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::EOF),
|
token!(TokenType::EOF),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -299,43 +308,43 @@ mod tests {
|
||||||
)
|
)
|
||||||
.collect::<Vec<Token>>(),
|
.collect::<Vec<Token>>(),
|
||||||
vec![
|
vec![
|
||||||
Token::new(TokenType::Let),
|
token!(TokenType::Let),
|
||||||
Token::with_value(TokenType::Ident, "five"),
|
token!(TokenType::Ident, "five"),
|
||||||
Token::new(TokenType::Assign),
|
token!(TokenType::Assign),
|
||||||
Token::with_value(TokenType::Int, "5"),
|
token!(TokenType::Int, "5"),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::Let),
|
token!(TokenType::Let),
|
||||||
Token::with_value(TokenType::Ident, "ten"),
|
token!(TokenType::Ident, "ten"),
|
||||||
Token::new(TokenType::Assign),
|
token!(TokenType::Assign),
|
||||||
Token::with_value(TokenType::Int, "10"),
|
token!(TokenType::Int, "10"),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::Let),
|
token!(TokenType::Let),
|
||||||
Token::with_value(TokenType::Ident, "add"),
|
token!(TokenType::Ident, "add"),
|
||||||
Token::new(TokenType::Assign),
|
token!(TokenType::Assign),
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
Token::new(TokenType::LParen),
|
token!(TokenType::LParen),
|
||||||
Token::with_value(TokenType::Ident, "x"),
|
token!(TokenType::Ident, "x"),
|
||||||
Token::new(TokenType::Comma),
|
token!(TokenType::Comma),
|
||||||
Token::with_value(TokenType::Ident, "y"),
|
token!(TokenType::Ident, "y"),
|
||||||
Token::new(TokenType::RParen),
|
token!(TokenType::RParen),
|
||||||
Token::new(TokenType::LBrace),
|
token!(TokenType::LBrace),
|
||||||
Token::with_value(TokenType::Ident, "x"),
|
token!(TokenType::Ident, "x"),
|
||||||
Token::new(TokenType::Plus),
|
token!(TokenType::Plus),
|
||||||
Token::with_value(TokenType::Ident, "y"),
|
token!(TokenType::Ident, "y"),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::RBrace),
|
token!(TokenType::RBrace),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::Let),
|
token!(TokenType::Let),
|
||||||
Token::with_value(TokenType::Ident, "result"),
|
token!(TokenType::Ident, "result"),
|
||||||
Token::new(TokenType::Assign),
|
token!(TokenType::Assign),
|
||||||
Token::with_value(TokenType::Ident, "add"),
|
token!(TokenType::Ident, "add"),
|
||||||
Token::new(TokenType::LParen),
|
token!(TokenType::LParen),
|
||||||
Token::with_value(TokenType::Ident, "five"),
|
token!(TokenType::Ident, "five"),
|
||||||
Token::new(TokenType::Comma),
|
token!(TokenType::Comma),
|
||||||
Token::with_value(TokenType::Ident, "ten"),
|
token!(TokenType::Ident, "ten"),
|
||||||
Token::new(TokenType::RParen),
|
token!(TokenType::RParen),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::EOF),
|
token!(TokenType::EOF),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -358,54 +367,54 @@ mod tests {
|
||||||
)
|
)
|
||||||
.collect::<Vec<Token>>(),
|
.collect::<Vec<Token>>(),
|
||||||
vec![
|
vec![
|
||||||
Token::new(TokenType::Let),
|
token!(TokenType::Let),
|
||||||
Token::with_value(TokenType::Ident, "result"),
|
token!(TokenType::Ident, "result"),
|
||||||
Token::new(TokenType::Assign),
|
token!(TokenType::Assign),
|
||||||
Token::with_value(TokenType::Ident, "add"),
|
token!(TokenType::Ident, "add"),
|
||||||
Token::new(TokenType::LParen),
|
token!(TokenType::LParen),
|
||||||
Token::with_value(TokenType::Ident, "five"),
|
token!(TokenType::Ident, "five"),
|
||||||
Token::new(TokenType::Comma),
|
token!(TokenType::Comma),
|
||||||
Token::with_value(TokenType::Ident, "ten"),
|
token!(TokenType::Ident, "ten"),
|
||||||
Token::new(TokenType::RParen),
|
token!(TokenType::RParen),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::Bang),
|
token!(TokenType::Bang),
|
||||||
Token::new(TokenType::Minus),
|
token!(TokenType::Minus),
|
||||||
Token::new(TokenType::Slash),
|
token!(TokenType::Slash),
|
||||||
Token::new(TokenType::Asterisk),
|
token!(TokenType::Asterisk),
|
||||||
Token::with_value(TokenType::Int, "5"),
|
token!(TokenType::Int, "5"),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::with_value(TokenType::Int, "5"),
|
token!(TokenType::Int, "5"),
|
||||||
Token::new(TokenType::LessThan),
|
token!(TokenType::LessThan),
|
||||||
Token::with_value(TokenType::Int, "10"),
|
token!(TokenType::Int, "10"),
|
||||||
Token::new(TokenType::GreaterThan),
|
token!(TokenType::GreaterThan),
|
||||||
Token::with_value(TokenType::Int, "5"),
|
token!(TokenType::Int, "5"),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::If),
|
token!(TokenType::If),
|
||||||
Token::new(TokenType::LParen),
|
token!(TokenType::LParen),
|
||||||
Token::with_value(TokenType::Int, "5"),
|
token!(TokenType::Int, "5"),
|
||||||
Token::new(TokenType::LessThan),
|
token!(TokenType::LessThan),
|
||||||
Token::with_value(TokenType::Int, "10"),
|
token!(TokenType::Int, "10"),
|
||||||
Token::new(TokenType::RParen),
|
token!(TokenType::RParen),
|
||||||
Token::new(TokenType::LBrace),
|
token!(TokenType::LBrace),
|
||||||
Token::new(TokenType::Return),
|
token!(TokenType::Return),
|
||||||
Token::new(TokenType::True),
|
token!(TokenType::True),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::RBrace),
|
token!(TokenType::RBrace),
|
||||||
Token::new(TokenType::Else),
|
token!(TokenType::Else),
|
||||||
Token::new(TokenType::LBrace),
|
token!(TokenType::LBrace),
|
||||||
Token::new(TokenType::Return),
|
token!(TokenType::Return),
|
||||||
Token::new(TokenType::False),
|
token!(TokenType::False),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::RBrace),
|
token!(TokenType::RBrace),
|
||||||
Token::with_value(TokenType::Int, "10"),
|
token!(TokenType::Int, "10"),
|
||||||
Token::new(TokenType::Equals),
|
token!(TokenType::Equals),
|
||||||
Token::with_value(TokenType::Int, "10"),
|
token!(TokenType::Int, "10"),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::with_value(TokenType::Int, "9"),
|
token!(TokenType::Int, "9"),
|
||||||
Token::new(TokenType::NotEquals),
|
token!(TokenType::NotEquals),
|
||||||
Token::with_value(TokenType::Int, "10"),
|
token!(TokenType::Int, "10"),
|
||||||
Token::new(TokenType::Semicolon),
|
token!(TokenType::Semicolon),
|
||||||
Token::new(TokenType::EOF),
|
token!(TokenType::EOF),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
|
|
||||||
|
#[allow(clippy::new_ret_no_self)]
|
||||||
mod evaluator;
|
mod evaluator;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
mod lexer;
|
mod lexer;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod repl;
|
mod repl;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
|
|
||||||
use {
|
use {
|
||||||
crate::{
|
crate::{
|
||||||
lexer::{Lexer, Token, TokenType},
|
lexer::{Lexer, Token, TokenType},
|
||||||
|
@ -6,7 +7,7 @@ use {
|
||||||
},
|
},
|
||||||
std::{
|
std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fmt::{Display, Error as FmtError, Formatter},
|
fmt::{Display, Formatter, Result as FmtResult},
|
||||||
iter::Peekable,
|
iter::Peekable,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -158,7 +159,7 @@ pub struct Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
impl Display for Error {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
|
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
||||||
f.write_str(&self.reason)
|
f.write_str(&self.reason)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -263,7 +264,7 @@ mod tests {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
program.statements,
|
program.statements,
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "foobar"),
|
token!(TokenType::Ident, "foobar"),
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "foobar")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "foobar")),
|
||||||
))]
|
))]
|
||||||
);
|
);
|
||||||
|
@ -281,7 +282,7 @@ mod tests {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
program.unwrap().statements,
|
program.unwrap().statements,
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "5"),
|
token!(TokenType::Int, "5"),
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5))
|
Expression::IntegerLiteral(IntegerLiteral::new(5))
|
||||||
))]
|
))]
|
||||||
);
|
);
|
||||||
|
@ -293,7 +294,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"!5",
|
"!5",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Bang),
|
token!(TokenType::Bang),
|
||||||
Expression::PrefixExpression(PrefixExpression::new(
|
Expression::PrefixExpression(PrefixExpression::new(
|
||||||
TokenType::Bang,
|
TokenType::Bang,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
|
@ -303,7 +304,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"-15;",
|
"-15;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Minus),
|
token!(TokenType::Minus),
|
||||||
Expression::PrefixExpression(PrefixExpression::new(
|
Expression::PrefixExpression(PrefixExpression::new(
|
||||||
TokenType::Minus,
|
TokenType::Minus,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
||||||
|
@ -313,7 +314,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"!foobar;",
|
"!foobar;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Bang),
|
token!(TokenType::Bang),
|
||||||
Expression::PrefixExpression(PrefixExpression::new(
|
Expression::PrefixExpression(PrefixExpression::new(
|
||||||
TokenType::Bang,
|
TokenType::Bang,
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "foobar")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "foobar")),
|
||||||
|
@ -323,7 +324,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"!true;",
|
"!true;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Bang),
|
token!(TokenType::Bang),
|
||||||
Expression::PrefixExpression(PrefixExpression::new(
|
Expression::PrefixExpression(PrefixExpression::new(
|
||||||
TokenType::Bang,
|
TokenType::Bang,
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
||||||
|
@ -333,7 +334,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"!false;",
|
"!false;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Bang),
|
token!(TokenType::Bang),
|
||||||
Expression::PrefixExpression(PrefixExpression::new(
|
Expression::PrefixExpression(PrefixExpression::new(
|
||||||
TokenType::Bang,
|
TokenType::Bang,
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
||||||
|
@ -343,7 +344,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"!isGreaterThanZero( 2);",
|
"!isGreaterThanZero( 2);",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Bang),
|
token!(TokenType::Bang),
|
||||||
Expression::PrefixExpression(PrefixExpression::new(
|
Expression::PrefixExpression(PrefixExpression::new(
|
||||||
TokenType::Bang,
|
TokenType::Bang,
|
||||||
Expression::CallExpression(CallExpression::new(
|
Expression::CallExpression(CallExpression::new(
|
||||||
|
@ -367,7 +368,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"5 + 10;",
|
"5 + 10;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "5"),
|
token!(TokenType::Int, "5"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
TokenType::Plus,
|
TokenType::Plus,
|
||||||
|
@ -378,7 +379,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"5 - 10;",
|
"5 - 10;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "5"),
|
token!(TokenType::Int, "5"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
TokenType::Minus,
|
TokenType::Minus,
|
||||||
|
@ -389,7 +390,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"5 * 15;",
|
"5 * 15;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "5"),
|
token!(TokenType::Int, "5"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
TokenType::Asterisk,
|
TokenType::Asterisk,
|
||||||
|
@ -400,7 +401,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"15 / 3;",
|
"15 / 3;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "15"),
|
token!(TokenType::Int, "15"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
||||||
TokenType::Slash,
|
TokenType::Slash,
|
||||||
|
@ -411,7 +412,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"5 > 15;",
|
"5 > 15;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "5"),
|
token!(TokenType::Int, "5"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
TokenType::GreaterThan,
|
TokenType::GreaterThan,
|
||||||
|
@ -422,7 +423,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"a + b + c;",
|
"a + b + c;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "a"),
|
token!(TokenType::Ident, "a"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
|
||||||
|
@ -437,7 +438,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"true == true",
|
"true == true",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::True),
|
token!(TokenType::True),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
||||||
TokenType::Equals,
|
TokenType::Equals,
|
||||||
|
@ -448,7 +449,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"true != false",
|
"true != false",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::True),
|
token!(TokenType::True),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
||||||
TokenType::NotEquals,
|
TokenType::NotEquals,
|
||||||
|
@ -459,7 +460,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"false == false",
|
"false == false",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::False),
|
token!(TokenType::False),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
||||||
TokenType::Equals,
|
TokenType::Equals,
|
||||||
|
@ -521,14 +522,14 @@ mod tests {
|
||||||
(
|
(
|
||||||
"true;",
|
"true;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::True),
|
token!(TokenType::True),
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
||||||
))],
|
))],
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"false;",
|
"false;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::False),
|
token!(TokenType::False),
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
||||||
))],
|
))],
|
||||||
),
|
),
|
||||||
|
@ -547,7 +548,7 @@ mod tests {
|
||||||
let test_cases = [(
|
let test_cases = [(
|
||||||
"if (x > y) { x };",
|
"if (x > y) { x };",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::If),
|
token!(TokenType::If),
|
||||||
Expression::IfExpression(IfExpression::new(
|
Expression::IfExpression(IfExpression::new(
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
||||||
|
@ -556,7 +557,7 @@ mod tests {
|
||||||
)),
|
)),
|
||||||
BlockStatement::new(vec![Statement::ExpressionStatement(
|
BlockStatement::new(vec![Statement::ExpressionStatement(
|
||||||
ExpressionStatement::new(
|
ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "x"),
|
token!(TokenType::Ident, "x"),
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
||||||
),
|
),
|
||||||
)]),
|
)]),
|
||||||
|
@ -572,7 +573,7 @@ mod tests {
|
||||||
let test_cases = [(
|
let test_cases = [(
|
||||||
"if (x > y) { x } else { y };",
|
"if (x > y) { x } else { y };",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::If),
|
token!(TokenType::If),
|
||||||
Expression::IfExpression(IfExpression::new(
|
Expression::IfExpression(IfExpression::new(
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
||||||
|
@ -581,13 +582,13 @@ mod tests {
|
||||||
)),
|
)),
|
||||||
BlockStatement::new(vec![Statement::ExpressionStatement(
|
BlockStatement::new(vec![Statement::ExpressionStatement(
|
||||||
ExpressionStatement::new(
|
ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "x"),
|
token!(TokenType::Ident, "x"),
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
||||||
),
|
),
|
||||||
)]),
|
)]),
|
||||||
Some(BlockStatement::new(vec![Statement::ExpressionStatement(
|
Some(BlockStatement::new(vec![Statement::ExpressionStatement(
|
||||||
ExpressionStatement::new(
|
ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "y"),
|
token!(TokenType::Ident, "y"),
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
|
||||||
),
|
),
|
||||||
)])),
|
)])),
|
||||||
|
@ -603,16 +604,16 @@ mod tests {
|
||||||
(
|
(
|
||||||
"fn(a,b) {x + y;}",
|
"fn(a,b) {x + y;}",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
Expression::FunctionExpression(FunctionLiteral::new(
|
Expression::FunctionExpression(FunctionLiteral::new(
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
vec![
|
vec![
|
||||||
Identifier::new(TokenType::Ident, "a"),
|
Identifier::new(TokenType::Ident, "a"),
|
||||||
Identifier::new(TokenType::Ident, "b"),
|
Identifier::new(TokenType::Ident, "b"),
|
||||||
],
|
],
|
||||||
BlockStatement::new(vec![Statement::ExpressionStatement(
|
BlockStatement::new(vec![Statement::ExpressionStatement(
|
||||||
ExpressionStatement::new(
|
ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "x"),
|
token!(TokenType::Ident, "x"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
||||||
TokenType::Plus,
|
TokenType::Plus,
|
||||||
|
@ -626,9 +627,9 @@ mod tests {
|
||||||
(
|
(
|
||||||
"fn() {}",
|
"fn() {}",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
Expression::FunctionExpression(FunctionLiteral::new(
|
Expression::FunctionExpression(FunctionLiteral::new(
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
vec![],
|
vec![],
|
||||||
BlockStatement::new(vec![]),
|
BlockStatement::new(vec![]),
|
||||||
)),
|
)),
|
||||||
|
@ -637,9 +638,9 @@ mod tests {
|
||||||
(
|
(
|
||||||
"fn(x,ya,z) {}",
|
"fn(x,ya,z) {}",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
Expression::FunctionExpression(FunctionLiteral::new(
|
Expression::FunctionExpression(FunctionLiteral::new(
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
vec![
|
vec![
|
||||||
Identifier::new(TokenType::Ident, "x"),
|
Identifier::new(TokenType::Ident, "x"),
|
||||||
Identifier::new(TokenType::Ident, "ya"),
|
Identifier::new(TokenType::Ident, "ya"),
|
||||||
|
@ -652,9 +653,9 @@ mod tests {
|
||||||
(
|
(
|
||||||
"fn(a) {}",
|
"fn(a) {}",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
Expression::FunctionExpression(FunctionLiteral::new(
|
Expression::FunctionExpression(FunctionLiteral::new(
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
vec![Identifier::new(TokenType::Ident, "a")],
|
vec![Identifier::new(TokenType::Ident, "a")],
|
||||||
BlockStatement::new(vec![]),
|
BlockStatement::new(vec![]),
|
||||||
)),
|
)),
|
||||||
|
@ -663,9 +664,9 @@ mod tests {
|
||||||
(
|
(
|
||||||
"fn(a,b,abc) {(x + y) * (a -b);}",
|
"fn(a,b,abc) {(x + y) * (a -b);}",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
Expression::FunctionExpression(FunctionLiteral::new(
|
Expression::FunctionExpression(FunctionLiteral::new(
|
||||||
Token::new(TokenType::Function),
|
token!(TokenType::Function),
|
||||||
vec![
|
vec![
|
||||||
Identifier::new(TokenType::Ident, "a"),
|
Identifier::new(TokenType::Ident, "a"),
|
||||||
Identifier::new(TokenType::Ident, "b"),
|
Identifier::new(TokenType::Ident, "b"),
|
||||||
|
@ -673,7 +674,7 @@ mod tests {
|
||||||
],
|
],
|
||||||
BlockStatement::new(vec![Statement::ExpressionStatement(
|
BlockStatement::new(vec![Statement::ExpressionStatement(
|
||||||
ExpressionStatement::new(
|
ExpressionStatement::new(
|
||||||
Token::new(TokenType::LParen),
|
token!(TokenType::LParen),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::Identifier(Identifier::new(
|
Expression::Identifier(Identifier::new(
|
||||||
|
@ -714,7 +715,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"add(1, 2 * 3, 4 + 5);",
|
"add(1, 2 * 3, 4 + 5);",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "add"),
|
token!(TokenType::Ident, "add"),
|
||||||
Expression::CallExpression(CallExpression::new(
|
Expression::CallExpression(CallExpression::new(
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
|
||||||
vec![
|
vec![
|
||||||
|
@ -736,7 +737,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"a + add(1, 2 * 3, 4 + 5) + d;",
|
"a + add(1, 2 * 3, 4 + 5) + d;",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "a"),
|
token!(TokenType::Ident, "a"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
|
||||||
|
@ -766,7 +767,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"add(a,b,1, 2 * 3, 4 + 5, add(6,7 *8));",
|
"add(a,b,1, 2 * 3, 4 + 5, add(6,7 *8));",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "add"),
|
token!(TokenType::Ident, "add"),
|
||||||
Expression::CallExpression(CallExpression::new(
|
Expression::CallExpression(CallExpression::new(
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
|
||||||
vec![
|
vec![
|
||||||
|
@ -801,7 +802,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"add(a + b + c * d / f + g);",
|
"add(a + b + c * d / f + g);",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "add"),
|
token!(TokenType::Ident, "add"),
|
||||||
Expression::CallExpression(CallExpression::new(
|
Expression::CallExpression(CallExpression::new(
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
|
||||||
vec![Expression::InfixExpression(InfixExpression::new(
|
vec![Expression::InfixExpression(InfixExpression::new(
|
||||||
|
@ -837,7 +838,7 @@ mod tests {
|
||||||
(
|
(
|
||||||
"add();",
|
"add();",
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "add"),
|
token!(TokenType::Ident, "add"),
|
||||||
Expression::CallExpression(CallExpression::new(
|
Expression::CallExpression(CallExpression::new(
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
|
||||||
vec![],
|
vec![],
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
use crate::{
|
use {
|
||||||
|
crate::{
|
||||||
evaluator::{tree_walker::TreeWalker, Evaluator},
|
evaluator::{tree_walker::TreeWalker, Evaluator},
|
||||||
lexer::Lexer,
|
lexer::Lexer,
|
||||||
parser::{ast::Node, Error as ParserError, Parser},
|
parser::{ast::Node, Error as ParserError, Parser},
|
||||||
|
},
|
||||||
|
std::io::{self, BufRead, Result as IoResult, Write},
|
||||||
};
|
};
|
||||||
use std::io::{self, BufRead, Result as IoResult, Write};
|
|
||||||
|
|
||||||
const PROMPT: &[u8] = b">> ";
|
const PROMPT: &[u8] = b">> ";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user