Fixed all warnings reported by clippy
This commit is contained in:
parent
f3f50eed47
commit
b0cbe5d4f7
|
@ -84,22 +84,18 @@ impl LetStatement {
|
||||||
let ntoken = parser.lexer.next()?;
|
let ntoken = parser.lexer.next()?;
|
||||||
let expr = Expression::parse(parser, ntoken, ExpressionPriority::Lowest);
|
let expr = Expression::parse(parser, ntoken, ExpressionPriority::Lowest);
|
||||||
|
|
||||||
parser.expect_peek(TokenType::Semicolon);
|
parser.expect_peek(TokenType::Semicolon)?;
|
||||||
|
|
||||||
Some(Self {
|
Some(Self {
|
||||||
name: Identifier::new(TokenType::Let, &ident.literal?),
|
name: Identifier::new(TokenType::Let, &ident.literal?),
|
||||||
value: expr,
|
value: expr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const fn token_literal() -> &'static str {
|
|
||||||
"let"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for LetStatement {
|
impl Display for LetStatement {
|
||||||
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
||||||
let mut out = format!("{} {} = ", Self::token_literal(), self.name.value);
|
let mut out = format!("{} {} = ", TokenType::Let.to_string(), self.name.value);
|
||||||
|
|
||||||
if let Some(v) = &self.value {
|
if let Some(v) = &self.value {
|
||||||
let a: String = v.into();
|
let a: String = v.into();
|
||||||
|
@ -125,18 +121,14 @@ impl ReturnStatement {
|
||||||
fn parse(parser: &mut Parser) -> Option<Self> {
|
fn parse(parser: &mut Parser) -> Option<Self> {
|
||||||
let token = parser.lexer.next()?;
|
let token = parser.lexer.next()?;
|
||||||
let expr = Expression::parse(parser, token, ExpressionPriority::Lowest);
|
let expr = Expression::parse(parser, token, ExpressionPriority::Lowest);
|
||||||
parser.expect_peek(TokenType::Semicolon);
|
parser.expect_peek(TokenType::Semicolon)?;
|
||||||
return Some(ReturnStatement { return_value: expr });
|
Some(ReturnStatement { return_value: expr })
|
||||||
}
|
|
||||||
|
|
||||||
const fn token_literal() -> &'static str {
|
|
||||||
"return"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for ReturnStatement {
|
impl Display for ReturnStatement {
|
||||||
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
||||||
let mut out = String::from(Self::token_literal());
|
let mut out: String = TokenType::Return.to_string();
|
||||||
|
|
||||||
if let Some(v) = &self.return_value {
|
if let Some(v) = &self.return_value {
|
||||||
out.push(' ');
|
out.push(' ');
|
||||||
|
@ -222,7 +214,7 @@ impl Expression {
|
||||||
left_expr
|
left_expr
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
parser.no_prefix_parse_fn_error(&ctoken.name);
|
parser.no_prefix_parse_fn_error(ctoken.name);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -275,7 +267,7 @@ pub struct Identifier {
|
||||||
impl Identifier {
|
impl Identifier {
|
||||||
pub fn new(token: TokenType, v: &str) -> Self {
|
pub fn new(token: TokenType, v: &str) -> Self {
|
||||||
Identifier {
|
Identifier {
|
||||||
token: token,
|
token,
|
||||||
value: v.to_string(),
|
value: v.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -318,26 +310,23 @@ impl IntegerLiteral {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct PrefixExpression {
|
pub struct PrefixExpression {
|
||||||
token: TokenType,
|
operator: TokenType,
|
||||||
operator: String,
|
|
||||||
right: Box<Expression>,
|
right: Box<Expression>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrefixExpression {
|
impl PrefixExpression {
|
||||||
pub fn new(token: TokenType, operator: &str, right: Expression) -> Self {
|
pub fn new(operator: TokenType, right: Expression) -> Self {
|
||||||
Self {
|
Self {
|
||||||
token,
|
operator,
|
||||||
operator: operator.to_string(),
|
|
||||||
right: Box::new(right),
|
right: Box::new(right),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(parser: &mut Parser, ctoken: Token) -> Option<Expression> {
|
pub fn parse(parser: &mut Parser, ctoken: Token) -> Option<Expression> {
|
||||||
let next_token = parser.lexer.next()?;
|
let next_token = parser.lexer.next()?;
|
||||||
let right_expr = Expression::parse(parser, next_token.clone(), ExpressionPriority::Prefix)?;
|
let right_expr = Expression::parse(parser, next_token, ExpressionPriority::Prefix)?;
|
||||||
Some(Expression::PrefixExpression(PrefixExpression {
|
Some(Expression::PrefixExpression(PrefixExpression {
|
||||||
token: ctoken.name,
|
operator: ctoken.name,
|
||||||
operator: ctoken.to_string().into(),
|
|
||||||
right: Box::new(right_expr),
|
right: Box::new(right_expr),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -355,30 +344,25 @@ impl Display for PrefixExpression {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct InfixExpression {
|
pub struct InfixExpression {
|
||||||
token: TokenType,
|
|
||||||
left: Box<Expression>,
|
left: Box<Expression>,
|
||||||
operator: String,
|
operator: TokenType,
|
||||||
right: Box<Expression>,
|
right: Box<Expression>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InfixExpression {
|
impl InfixExpression {
|
||||||
pub fn new(token: TokenType, left: Expression, operator: &str, right: Expression) -> Self {
|
pub fn new(left: Expression, operator: TokenType, right: Expression) -> Self {
|
||||||
Self {
|
Self {
|
||||||
token,
|
|
||||||
left: Box::new(left),
|
left: Box::new(left),
|
||||||
operator: operator.to_string(),
|
operator,
|
||||||
right: Box::new(right),
|
right: Box::new(right),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn parse(parser: &mut Parser, token: Token, left_expr: Expression) -> Option<Expression> {
|
pub fn parse(parser: &mut Parser, token: Token, left_expr: Expression) -> Option<Expression> {
|
||||||
let cprecedence = parser.current_precedence(&token.name);
|
let cprecedence = parser.current_precedence(token.name);
|
||||||
let next_token = parser.lexer.next()?;
|
let next_token = parser.lexer.next()?;
|
||||||
let right_expr = Expression::parse(parser, next_token, cprecedence)?;
|
let right_expr = Expression::parse(parser, next_token, cprecedence)?;
|
||||||
Some(Expression::InfixExpression(InfixExpression::new(
|
Some(Expression::InfixExpression(InfixExpression::new(
|
||||||
token.name,
|
left_expr, token.name, right_expr,
|
||||||
left_expr,
|
|
||||||
&token.to_string(),
|
|
||||||
right_expr,
|
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -403,7 +387,7 @@ pub struct BooleanExpression {
|
||||||
impl BooleanExpression {
|
impl BooleanExpression {
|
||||||
pub fn new(token: TokenType) -> Self {
|
pub fn new(token: TokenType) -> Self {
|
||||||
BooleanExpression {
|
BooleanExpression {
|
||||||
token: token,
|
token,
|
||||||
value: token == TokenType::True,
|
value: token == TokenType::True,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -440,42 +424,21 @@ impl IfExpression {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn parse(parser: &mut Parser, _ctoken: Token) -> Option<Expression> {
|
pub fn parse(parser: &mut Parser, _ctoken: Token) -> Option<Expression> {
|
||||||
if parser.expect_peek(TokenType::LParen).is_none() {
|
parser.expect_peek(TokenType::LParen)?;
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let next_token = parser.lexer.next()?;
|
let next_token = parser.lexer.next()?;
|
||||||
let condition = Expression::parse(parser, next_token.clone(), ExpressionPriority::Lowest)?;
|
let condition = Expression::parse(parser, next_token.clone(), ExpressionPriority::Lowest)?;
|
||||||
|
|
||||||
if parser.expect_peek(TokenType::RParen).is_none() {
|
parser.expect_peek(TokenType::RParen)?;
|
||||||
return None;
|
parser.expect_peek(TokenType::LBrace)?;
|
||||||
}
|
|
||||||
if parser.expect_peek(TokenType::LBrace).is_none() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let consequence = BlockStatement::parse(parser, next_token)?;
|
let consequence = BlockStatement::parse(parser, next_token)?;
|
||||||
|
let mut expr = IfExpression::new(condition, consequence, None);
|
||||||
|
|
||||||
if parser.peek_token_is(TokenType::Else) {
|
if parser.peek_token_is(TokenType::Else) {
|
||||||
let token = parser.lexer.next()?;
|
let token = parser.lexer.next()?;
|
||||||
|
parser.expect_peek(TokenType::LBrace)?;
|
||||||
if parser.expect_peek(TokenType::LBrace).is_none() {
|
expr.alternative = BlockStatement::parse(parser, token);
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let alternative = BlockStatement::parse(parser, token);
|
|
||||||
|
|
||||||
Some(Expression::IfExpression(IfExpression::new(
|
|
||||||
condition,
|
|
||||||
consequence,
|
|
||||||
alternative,
|
|
||||||
)))
|
|
||||||
} else {
|
|
||||||
Some(Expression::IfExpression(IfExpression::new(
|
|
||||||
condition,
|
|
||||||
consequence,
|
|
||||||
None,
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
|
Some(Expression::IfExpression(expr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -505,21 +468,19 @@ impl BlockStatement {
|
||||||
|
|
||||||
pub fn parse(parser: &mut Parser, _ogtoken: Token) -> Option<Self> {
|
pub fn parse(parser: &mut Parser, _ogtoken: Token) -> Option<Self> {
|
||||||
let mut stmts = vec![];
|
let mut stmts = vec![];
|
||||||
|
|
||||||
let mut ctoken = parser.lexer.next();
|
let mut ctoken = parser.lexer.next();
|
||||||
|
|
||||||
while let Some(token) = ctoken {
|
while let Some(token) = ctoken {
|
||||||
if token.name == TokenType::RBrace {
|
if token.name == TokenType::RBrace {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let stmt = Statement::parse(parser, token);
|
let stmt = Statement::parse(parser, token);
|
||||||
if stmt.is_some() {
|
|
||||||
stmts.push(stmt.unwrap());
|
if let Some(stmt) = stmt {
|
||||||
|
stmts.push(stmt);
|
||||||
}
|
}
|
||||||
ctoken = parser.lexer.next();
|
ctoken = parser.lexer.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(BlockStatement::new(stmts))
|
Some(BlockStatement::new(stmts))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -551,15 +512,11 @@ impl FunctionLiteral {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn parse(parser: &mut Parser, ctoken: Token) -> Option<Expression> {
|
pub fn parse(parser: &mut Parser, ctoken: Token) -> Option<Expression> {
|
||||||
if parser.expect_peek(TokenType::LParen).is_none() {
|
parser.expect_peek(TokenType::LParen)?;
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let ntoken = parser.lexer.peek()?.clone();
|
let ntoken = parser.lexer.peek()?.clone();
|
||||||
let parameters = FunctionLiteral::parse_function_parameters(parser, ntoken)?;
|
let parameters = FunctionLiteral::parse_function_parameters(parser, ntoken)?;
|
||||||
|
|
||||||
if parser.expect_peek(TokenType::LBrace).is_none() {
|
parser.expect_peek(TokenType::LBrace)?;
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let ntoken = parser.lexer.peek()?.clone();
|
let ntoken = parser.lexer.peek()?.clone();
|
||||||
|
|
||||||
|
@ -589,11 +546,7 @@ impl FunctionLiteral {
|
||||||
let ident = Identifier::new(token.name, &token.literal?);
|
let ident = Identifier::new(token.name, &token.literal?);
|
||||||
out.push(ident);
|
out.push(ident);
|
||||||
}
|
}
|
||||||
|
parser.expect_peek(TokenType::RParen)?;
|
||||||
if parser.expect_peek(TokenType::RParen).is_none() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(out)
|
Some(out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -662,10 +615,8 @@ impl CallExpression {
|
||||||
return Some(expressions);
|
return Some(expressions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
parser.expect_peek(TokenType::RParen)?;
|
||||||
|
|
||||||
if parser.expect_peek(TokenType::RParen).is_none() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
Some(expressions)
|
Some(expressions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,7 +128,7 @@ impl<'a> Parser<'a> {
|
||||||
self.infix_parse_fns.insert(token, f);
|
self.infix_parse_fns.insert(token, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn no_prefix_parse_fn_error(&mut self, token: &TokenType) {
|
fn no_prefix_parse_fn_error(&mut self, token: TokenType) {
|
||||||
self.errors.push(Error {
|
self.errors.push(Error {
|
||||||
reason: format!("no prefix parse function for {} found", token.to_string()),
|
reason: format!("no prefix parse function for {} found", token.to_string()),
|
||||||
});
|
});
|
||||||
|
@ -144,7 +144,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_precedence(&mut self, token: &TokenType) -> ExpressionPriority {
|
fn current_precedence(&mut self, token: TokenType) -> ExpressionPriority {
|
||||||
match PRECEDENCE_MAP.get(&token) {
|
match PRECEDENCE_MAP.get(&token) {
|
||||||
Some(p) => *p,
|
Some(p) => *p,
|
||||||
None => ExpressionPriority::Lowest,
|
None => ExpressionPriority::Lowest,
|
||||||
|
@ -312,7 +312,6 @@ mod tests {
|
||||||
Token::new(TokenType::Bang),
|
Token::new(TokenType::Bang),
|
||||||
Expression::PrefixExpression(PrefixExpression::new(
|
Expression::PrefixExpression(PrefixExpression::new(
|
||||||
TokenType::Bang,
|
TokenType::Bang,
|
||||||
"!",
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -323,7 +322,6 @@ mod tests {
|
||||||
Token::new(TokenType::Minus),
|
Token::new(TokenType::Minus),
|
||||||
Expression::PrefixExpression(PrefixExpression::new(
|
Expression::PrefixExpression(PrefixExpression::new(
|
||||||
TokenType::Minus,
|
TokenType::Minus,
|
||||||
"-",
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -334,7 +332,6 @@ mod tests {
|
||||||
Token::new(TokenType::Bang),
|
Token::new(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")),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -345,7 +342,6 @@ mod tests {
|
||||||
Token::new(TokenType::Bang),
|
Token::new(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)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -356,7 +352,6 @@ mod tests {
|
||||||
Token::new(TokenType::Bang),
|
Token::new(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)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -367,7 +362,6 @@ mod tests {
|
||||||
Token::new(TokenType::Bang),
|
Token::new(TokenType::Bang),
|
||||||
Expression::PrefixExpression(PrefixExpression::new(
|
Expression::PrefixExpression(PrefixExpression::new(
|
||||||
TokenType::Bang,
|
TokenType::Bang,
|
||||||
"!",
|
|
||||||
Expression::CallExpression(CallExpression::new(
|
Expression::CallExpression(CallExpression::new(
|
||||||
Expression::Identifier(Identifier::new(
|
Expression::Identifier(Identifier::new(
|
||||||
TokenType::Ident,
|
TokenType::Ident,
|
||||||
|
@ -399,9 +393,8 @@ mod tests {
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "5"),
|
Token::with_value(TokenType::Int, "5"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(10)),
|
Expression::IntegerLiteral(IntegerLiteral::new(10)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -411,9 +404,8 @@ mod tests {
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "5"),
|
Token::with_value(TokenType::Int, "5"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Minus,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
"-",
|
TokenType::Minus,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(10)),
|
Expression::IntegerLiteral(IntegerLiteral::new(10)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -423,9 +415,8 @@ mod tests {
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "5"),
|
Token::with_value(TokenType::Int, "5"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Asterisk,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
"*",
|
TokenType::Asterisk,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -435,9 +426,8 @@ mod tests {
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "15"),
|
Token::with_value(TokenType::Int, "15"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Slash,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
||||||
"/",
|
TokenType::Slash,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(3)),
|
Expression::IntegerLiteral(IntegerLiteral::new(3)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -447,9 +437,8 @@ mod tests {
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Int, "5"),
|
Token::with_value(TokenType::Int, "5"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::GreaterThan,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
">",
|
TokenType::GreaterThan,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
Expression::IntegerLiteral(IntegerLiteral::new(15)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -459,14 +448,12 @@ mod tests {
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "a"),
|
Token::with_value(TokenType::Ident, "a"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "b")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "b")),
|
||||||
)),
|
)),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "c")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "c")),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -476,9 +463,8 @@ mod tests {
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::True),
|
Token::new(TokenType::True),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Equals,
|
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
||||||
"==",
|
TokenType::Equals,
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -488,9 +474,8 @@ mod tests {
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::True),
|
Token::new(TokenType::True),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::NotEquals,
|
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
|
||||||
"!=",
|
TokenType::NotEquals,
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -500,9 +485,8 @@ mod tests {
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::new(TokenType::False),
|
Token::new(TokenType::False),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Equals,
|
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
||||||
"==",
|
TokenType::Equals,
|
||||||
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -607,9 +591,8 @@ mod tests {
|
||||||
Token::new(TokenType::If),
|
Token::new(TokenType::If),
|
||||||
Expression::IfExpression(IfExpression::new(
|
Expression::IfExpression(IfExpression::new(
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::GreaterThan,
|
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
||||||
">",
|
TokenType::GreaterThan,
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
|
||||||
)),
|
)),
|
||||||
BlockStatement::new(vec![Statement::ExpressionStatement(
|
BlockStatement::new(vec![Statement::ExpressionStatement(
|
||||||
|
@ -641,9 +624,8 @@ mod tests {
|
||||||
Token::new(TokenType::If),
|
Token::new(TokenType::If),
|
||||||
Expression::IfExpression(IfExpression::new(
|
Expression::IfExpression(IfExpression::new(
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::GreaterThan,
|
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
||||||
">",
|
TokenType::GreaterThan,
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
|
||||||
)),
|
)),
|
||||||
BlockStatement::new(vec![Statement::ExpressionStatement(
|
BlockStatement::new(vec![Statement::ExpressionStatement(
|
||||||
|
@ -690,9 +672,8 @@ mod tests {
|
||||||
ExpressionStatement::new(
|
ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "x"),
|
Token::with_value(TokenType::Ident, "x"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
|
@ -752,27 +733,24 @@ mod tests {
|
||||||
ExpressionStatement::new(
|
ExpressionStatement::new(
|
||||||
Token::new(TokenType::LParen),
|
Token::new(TokenType::LParen),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Asterisk,
|
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::Identifier(Identifier::new(
|
Expression::Identifier(Identifier::new(
|
||||||
TokenType::Ident,
|
TokenType::Ident,
|
||||||
"x",
|
"x",
|
||||||
)),
|
)),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::Identifier(Identifier::new(
|
Expression::Identifier(Identifier::new(
|
||||||
TokenType::Ident,
|
TokenType::Ident,
|
||||||
"y",
|
"y",
|
||||||
)),
|
)),
|
||||||
)),
|
)),
|
||||||
"*",
|
TokenType::Asterisk,
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Minus,
|
|
||||||
Expression::Identifier(Identifier::new(
|
Expression::Identifier(Identifier::new(
|
||||||
TokenType::Ident,
|
TokenType::Ident,
|
||||||
"a",
|
"a",
|
||||||
)),
|
)),
|
||||||
"-",
|
TokenType::Minus,
|
||||||
Expression::Identifier(Identifier::new(
|
Expression::Identifier(Identifier::new(
|
||||||
TokenType::Ident,
|
TokenType::Ident,
|
||||||
"b",
|
"b",
|
||||||
|
@ -808,15 +786,13 @@ mod tests {
|
||||||
vec![
|
vec![
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(1)),
|
Expression::IntegerLiteral(IntegerLiteral::new(1)),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Asterisk,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(2)),
|
Expression::IntegerLiteral(IntegerLiteral::new(2)),
|
||||||
"*",
|
TokenType::Asterisk,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(3)),
|
Expression::IntegerLiteral(IntegerLiteral::new(3)),
|
||||||
)),
|
)),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(4)),
|
Expression::IntegerLiteral(IntegerLiteral::new(4)),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
)),
|
)),
|
||||||
],
|
],
|
||||||
|
@ -828,31 +804,27 @@ mod tests {
|
||||||
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
vec![Statement::ExpressionStatement(ExpressionStatement::new(
|
||||||
Token::with_value(TokenType::Ident, "a"),
|
Token::with_value(TokenType::Ident, "a"),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::CallExpression(CallExpression::new(
|
Expression::CallExpression(CallExpression::new(
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
|
||||||
vec![
|
vec![
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(1)),
|
Expression::IntegerLiteral(IntegerLiteral::new(1)),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Asterisk,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(2)),
|
Expression::IntegerLiteral(IntegerLiteral::new(2)),
|
||||||
"*",
|
TokenType::Asterisk,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(3)),
|
Expression::IntegerLiteral(IntegerLiteral::new(3)),
|
||||||
)),
|
)),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(4)),
|
Expression::IntegerLiteral(IntegerLiteral::new(4)),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
)),
|
)),
|
||||||
],
|
],
|
||||||
)),
|
)),
|
||||||
)),
|
)),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "d")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "d")),
|
||||||
)),
|
)),
|
||||||
))],
|
))],
|
||||||
|
@ -868,15 +840,13 @@ mod tests {
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "b")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "b")),
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(1)),
|
Expression::IntegerLiteral(IntegerLiteral::new(1)),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Asterisk,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(2)),
|
Expression::IntegerLiteral(IntegerLiteral::new(2)),
|
||||||
"*",
|
TokenType::Asterisk,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(3)),
|
Expression::IntegerLiteral(IntegerLiteral::new(3)),
|
||||||
)),
|
)),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(4)),
|
Expression::IntegerLiteral(IntegerLiteral::new(4)),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
Expression::IntegerLiteral(IntegerLiteral::new(5)),
|
||||||
)),
|
)),
|
||||||
Expression::CallExpression(CallExpression::new(
|
Expression::CallExpression(CallExpression::new(
|
||||||
|
@ -884,9 +854,8 @@ mod tests {
|
||||||
vec![
|
vec![
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(6)),
|
Expression::IntegerLiteral(IntegerLiteral::new(6)),
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Asterisk,
|
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(7)),
|
Expression::IntegerLiteral(IntegerLiteral::new(7)),
|
||||||
"*",
|
TokenType::Asterisk,
|
||||||
Expression::IntegerLiteral(IntegerLiteral::new(8)),
|
Expression::IntegerLiteral(IntegerLiteral::new(8)),
|
||||||
)),
|
)),
|
||||||
],
|
],
|
||||||
|
@ -902,35 +871,30 @@ mod tests {
|
||||||
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(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Plus,
|
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "b")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "b")),
|
||||||
)),
|
)),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Slash,
|
|
||||||
Expression::InfixExpression(InfixExpression::new(
|
Expression::InfixExpression(InfixExpression::new(
|
||||||
TokenType::Asterisk,
|
|
||||||
Expression::Identifier(Identifier::new(
|
Expression::Identifier(Identifier::new(
|
||||||
TokenType::Ident,
|
TokenType::Ident,
|
||||||
"c",
|
"c",
|
||||||
)),
|
)),
|
||||||
"*",
|
TokenType::Asterisk,
|
||||||
Expression::Identifier(Identifier::new(
|
Expression::Identifier(Identifier::new(
|
||||||
TokenType::Ident,
|
TokenType::Ident,
|
||||||
"d",
|
"d",
|
||||||
)),
|
)),
|
||||||
)),
|
)),
|
||||||
"/",
|
TokenType::Slash,
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "f")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "f")),
|
||||||
)),
|
)),
|
||||||
)),
|
)),
|
||||||
"+",
|
TokenType::Plus,
|
||||||
Expression::Identifier(Identifier::new(TokenType::Ident, "g")),
|
Expression::Identifier(Identifier::new(TokenType::Ident, "g")),
|
||||||
))],
|
))],
|
||||||
)),
|
)),
|
||||||
|
|
|
@ -29,13 +29,10 @@ fn start<R: BufRead, W: Write>(mut ip: R, mut out: W) {
|
||||||
print_parser_errors(&mut out, &parser.errors).unwrap();
|
print_parser_errors(&mut out, &parser.errors).unwrap();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
match program {
|
if let Some(program) = program {
|
||||||
Some(prg) => {
|
for stmt in &program.statements {
|
||||||
for stmt in &prg.statements {
|
|
||||||
out.write_fmt(format_args!("{}\n", stmt)).unwrap();
|
out.write_fmt(format_args!("{}\n", stmt)).unwrap();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
None => (),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user