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