1
0
Fork 0

Fixed all warnings reported by clippy

master
Ishan Jain 3 years ago
parent f3f50eed47
commit b0cbe5d4f7

@ -84,22 +84,18 @@ impl LetStatement {
let ntoken = parser.lexer.next()?;
let expr = Expression::parse(parser, ntoken, ExpressionPriority::Lowest);
parser.expect_peek(TokenType::Semicolon);
parser.expect_peek(TokenType::Semicolon)?;
Some(Self {
name: Identifier::new(TokenType::Let, &ident.literal?),
value: expr,
})
}
const fn token_literal() -> &'static str {
"let"
}
}
impl Display for LetStatement {
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 {
let a: String = v.into();
@ -125,18 +121,14 @@ impl ReturnStatement {
fn parse(parser: &mut Parser) -> Option<Self> {
let token = parser.lexer.next()?;
let expr = Expression::parse(parser, token, ExpressionPriority::Lowest);
parser.expect_peek(TokenType::Semicolon);
return Some(ReturnStatement { return_value: expr });
}
const fn token_literal() -> &'static str {
"return"
parser.expect_peek(TokenType::Semicolon)?;
Some(ReturnStatement { return_value: expr })
}
}
impl Display for ReturnStatement {
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 {
out.push(' ');
@ -222,7 +214,7 @@ impl Expression {
left_expr
}
None => {
parser.no_prefix_parse_fn_error(&ctoken.name);
parser.no_prefix_parse_fn_error(ctoken.name);
None
}
}
@ -275,7 +267,7 @@ pub struct Identifier {
impl Identifier {
pub fn new(token: TokenType, v: &str) -> Self {
Identifier {
token: token,
token,
value: v.to_string(),
}
}
@ -318,26 +310,23 @@ impl IntegerLiteral {
#[derive(Debug, PartialEq)]
pub struct PrefixExpression {
token: TokenType,
operator: String,
operator: TokenType,
right: Box<Expression>,
}
impl PrefixExpression {
pub fn new(token: TokenType, operator: &str, right: Expression) -> Self {
pub fn new(operator: TokenType, right: Expression) -> Self {
Self {
token,
operator: operator.to_string(),
operator,
right: Box::new(right),
}
}
pub fn parse(parser: &mut Parser, ctoken: Token) -> Option<Expression> {
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 {
token: ctoken.name,
operator: ctoken.to_string().into(),
operator: ctoken.name,
right: Box::new(right_expr),
}))
}
@ -355,30 +344,25 @@ impl Display for PrefixExpression {
#[derive(Debug, PartialEq)]
pub struct InfixExpression {
token: TokenType,
left: Box<Expression>,
operator: String,
operator: TokenType,
right: Box<Expression>,
}
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 {
token,
left: Box::new(left),
operator: operator.to_string(),
operator,
right: Box::new(right),
}
}
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 right_expr = Expression::parse(parser, next_token, cprecedence)?;
Some(Expression::InfixExpression(InfixExpression::new(
token.name,
left_expr,
&token.to_string(),
right_expr,
left_expr, token.name, right_expr,
)))
}
}
@ -403,7 +387,7 @@ pub struct BooleanExpression {
impl BooleanExpression {
pub fn new(token: TokenType) -> Self {
BooleanExpression {
token: token,
token,
value: token == TokenType::True,
}
}
@ -440,54 +424,33 @@ impl IfExpression {
}
}
pub fn parse(parser: &mut Parser, _ctoken: Token) -> Option<Expression> {
if parser.expect_peek(TokenType::LParen).is_none() {
return None;
}
parser.expect_peek(TokenType::LParen)?;
let next_token = parser.lexer.next()?;
let condition = Expression::parse(parser, next_token.clone(), ExpressionPriority::Lowest)?;
if parser.expect_peek(TokenType::RParen).is_none() {
return None;
}
if parser.expect_peek(TokenType::LBrace).is_none() {
return None;
}
parser.expect_peek(TokenType::RParen)?;
parser.expect_peek(TokenType::LBrace)?;
let consequence = BlockStatement::parse(parser, next_token)?;
let mut expr = IfExpression::new(condition, consequence, None);
if parser.peek_token_is(TokenType::Else) {
let token = parser.lexer.next()?;
if parser.expect_peek(TokenType::LBrace).is_none() {
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,
)))
parser.expect_peek(TokenType::LBrace)?;
expr.alternative = BlockStatement::parse(parser, token);
}
Some(Expression::IfExpression(expr))
}
}
impl Display for IfExpression {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
let mut out = format!(
"if {} {{ {} }} ",
"if {} {{ {} }}",
self.condition.to_string(),
self.consequence.to_string()
);
if let Some(alternative) = &self.alternative {
out += &format!("else {{ {} }}", alternative.to_string());
out += &format!(" else {{ {} }}", alternative.to_string());
}
f.write_str(&out)
}
@ -505,21 +468,19 @@ impl BlockStatement {
pub fn parse(parser: &mut Parser, _ogtoken: Token) -> Option<Self> {
let mut stmts = vec![];
let mut ctoken = parser.lexer.next();
while let Some(token) = ctoken {
if token.name == TokenType::RBrace {
break;
}
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();
}
Some(BlockStatement::new(stmts))
}
}
@ -551,15 +512,11 @@ impl FunctionLiteral {
}
}
pub fn parse(parser: &mut Parser, ctoken: Token) -> Option<Expression> {
if parser.expect_peek(TokenType::LParen).is_none() {
return None;
}
parser.expect_peek(TokenType::LParen)?;
let ntoken = parser.lexer.peek()?.clone();
let parameters = FunctionLiteral::parse_function_parameters(parser, ntoken)?;
if parser.expect_peek(TokenType::LBrace).is_none() {
return None;
}
parser.expect_peek(TokenType::LBrace)?;
let ntoken = parser.lexer.peek()?.clone();
@ -589,11 +546,7 @@ impl FunctionLiteral {
let ident = Identifier::new(token.name, &token.literal?);
out.push(ident);
}
if parser.expect_peek(TokenType::RParen).is_none() {
return None;
}
parser.expect_peek(TokenType::RParen)?;
Some(out)
}
}
@ -662,10 +615,8 @@ impl CallExpression {
return Some(expressions);
}
}
parser.expect_peek(TokenType::RParen)?;
if parser.expect_peek(TokenType::RParen).is_none() {
return None;
}
Some(expressions)
}
}

@ -128,7 +128,7 @@ impl<'a> Parser<'a> {
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 {
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) {
Some(p) => *p,
None => ExpressionPriority::Lowest,
@ -312,7 +312,6 @@ mod tests {
Token::new(TokenType::Bang),
Expression::PrefixExpression(PrefixExpression::new(
TokenType::Bang,
"!",
Expression::IntegerLiteral(IntegerLiteral::new(5)),
)),
))],
@ -323,7 +322,6 @@ mod tests {
Token::new(TokenType::Minus),
Expression::PrefixExpression(PrefixExpression::new(
TokenType::Minus,
"-",
Expression::IntegerLiteral(IntegerLiteral::new(15)),
)),
))],
@ -334,7 +332,6 @@ mod tests {
Token::new(TokenType::Bang),
Expression::PrefixExpression(PrefixExpression::new(
TokenType::Bang,
"!",
Expression::Identifier(Identifier::new(TokenType::Ident, "foobar")),
)),
))],
@ -345,7 +342,6 @@ mod tests {
Token::new(TokenType::Bang),
Expression::PrefixExpression(PrefixExpression::new(
TokenType::Bang,
"!",
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
)),
))],
@ -356,7 +352,6 @@ mod tests {
Token::new(TokenType::Bang),
Expression::PrefixExpression(PrefixExpression::new(
TokenType::Bang,
"!",
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
)),
))],
@ -367,7 +362,6 @@ mod tests {
Token::new(TokenType::Bang),
Expression::PrefixExpression(PrefixExpression::new(
TokenType::Bang,
"!",
Expression::CallExpression(CallExpression::new(
Expression::Identifier(Identifier::new(
TokenType::Ident,
@ -399,9 +393,8 @@ mod tests {
vec![Statement::ExpressionStatement(ExpressionStatement::new(
Token::with_value(TokenType::Int, "5"),
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::IntegerLiteral(IntegerLiteral::new(5)),
"+",
TokenType::Plus,
Expression::IntegerLiteral(IntegerLiteral::new(10)),
)),
))],
@ -411,9 +404,8 @@ mod tests {
vec![Statement::ExpressionStatement(ExpressionStatement::new(
Token::with_value(TokenType::Int, "5"),
Expression::InfixExpression(InfixExpression::new(
TokenType::Minus,
Expression::IntegerLiteral(IntegerLiteral::new(5)),
"-",
TokenType::Minus,
Expression::IntegerLiteral(IntegerLiteral::new(10)),
)),
))],
@ -423,9 +415,8 @@ mod tests {
vec![Statement::ExpressionStatement(ExpressionStatement::new(
Token::with_value(TokenType::Int, "5"),
Expression::InfixExpression(InfixExpression::new(
TokenType::Asterisk,
Expression::IntegerLiteral(IntegerLiteral::new(5)),
"*",
TokenType::Asterisk,
Expression::IntegerLiteral(IntegerLiteral::new(15)),
)),
))],
@ -435,9 +426,8 @@ mod tests {
vec![Statement::ExpressionStatement(ExpressionStatement::new(
Token::with_value(TokenType::Int, "15"),
Expression::InfixExpression(InfixExpression::new(
TokenType::Slash,
Expression::IntegerLiteral(IntegerLiteral::new(15)),
"/",
TokenType::Slash,
Expression::IntegerLiteral(IntegerLiteral::new(3)),
)),
))],
@ -447,9 +437,8 @@ mod tests {
vec![Statement::ExpressionStatement(ExpressionStatement::new(
Token::with_value(TokenType::Int, "5"),
Expression::InfixExpression(InfixExpression::new(
TokenType::GreaterThan,
Expression::IntegerLiteral(IntegerLiteral::new(5)),
">",
TokenType::GreaterThan,
Expression::IntegerLiteral(IntegerLiteral::new(15)),
)),
))],
@ -459,14 +448,12 @@ mod tests {
vec![Statement::ExpressionStatement(ExpressionStatement::new(
Token::with_value(TokenType::Ident, "a"),
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
"+",
TokenType::Plus,
Expression::Identifier(Identifier::new(TokenType::Ident, "b")),
)),
"+",
TokenType::Plus,
Expression::Identifier(Identifier::new(TokenType::Ident, "c")),
)),
))],
@ -476,9 +463,8 @@ mod tests {
vec![Statement::ExpressionStatement(ExpressionStatement::new(
Token::new(TokenType::True),
Expression::InfixExpression(InfixExpression::new(
TokenType::Equals,
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
"==",
TokenType::Equals,
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
)),
))],
@ -488,9 +474,8 @@ mod tests {
vec![Statement::ExpressionStatement(ExpressionStatement::new(
Token::new(TokenType::True),
Expression::InfixExpression(InfixExpression::new(
TokenType::NotEquals,
Expression::BooleanExpression(BooleanExpression::new(TokenType::True)),
"!=",
TokenType::NotEquals,
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
)),
))],
@ -500,9 +485,8 @@ mod tests {
vec![Statement::ExpressionStatement(ExpressionStatement::new(
Token::new(TokenType::False),
Expression::InfixExpression(InfixExpression::new(
TokenType::Equals,
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
"==",
TokenType::Equals,
Expression::BooleanExpression(BooleanExpression::new(TokenType::False)),
)),
))],
@ -607,9 +591,8 @@ mod tests {
Token::new(TokenType::If),
Expression::IfExpression(IfExpression::new(
Expression::InfixExpression(InfixExpression::new(
TokenType::GreaterThan,
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
">",
TokenType::GreaterThan,
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
)),
BlockStatement::new(vec![Statement::ExpressionStatement(
@ -641,9 +624,8 @@ mod tests {
Token::new(TokenType::If),
Expression::IfExpression(IfExpression::new(
Expression::InfixExpression(InfixExpression::new(
TokenType::GreaterThan,
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
">",
TokenType::GreaterThan,
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
)),
BlockStatement::new(vec![Statement::ExpressionStatement(
@ -690,9 +672,8 @@ mod tests {
ExpressionStatement::new(
Token::with_value(TokenType::Ident, "x"),
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::Identifier(Identifier::new(TokenType::Ident, "x")),
"+",
TokenType::Plus,
Expression::Identifier(Identifier::new(TokenType::Ident, "y")),
)),
),
@ -752,27 +733,24 @@ mod tests {
ExpressionStatement::new(
Token::new(TokenType::LParen),
Expression::InfixExpression(InfixExpression::new(
TokenType::Asterisk,
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::Identifier(Identifier::new(
TokenType::Ident,
"x",
)),
"+",
TokenType::Plus,
Expression::Identifier(Identifier::new(
TokenType::Ident,
"y",
)),
)),
"*",
TokenType::Asterisk,
Expression::InfixExpression(InfixExpression::new(
TokenType::Minus,
Expression::Identifier(Identifier::new(
TokenType::Ident,
"a",
)),
"-",
TokenType::Minus,
Expression::Identifier(Identifier::new(
TokenType::Ident,
"b",
@ -808,15 +786,13 @@ mod tests {
vec![
Expression::IntegerLiteral(IntegerLiteral::new(1)),
Expression::InfixExpression(InfixExpression::new(
TokenType::Asterisk,
Expression::IntegerLiteral(IntegerLiteral::new(2)),
"*",
TokenType::Asterisk,
Expression::IntegerLiteral(IntegerLiteral::new(3)),
)),
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::IntegerLiteral(IntegerLiteral::new(4)),
"+",
TokenType::Plus,
Expression::IntegerLiteral(IntegerLiteral::new(5)),
)),
],
@ -828,31 +804,27 @@ mod tests {
vec![Statement::ExpressionStatement(ExpressionStatement::new(
Token::with_value(TokenType::Ident, "a"),
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
"+",
TokenType::Plus,
Expression::CallExpression(CallExpression::new(
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
vec![
Expression::IntegerLiteral(IntegerLiteral::new(1)),
Expression::InfixExpression(InfixExpression::new(
TokenType::Asterisk,
Expression::IntegerLiteral(IntegerLiteral::new(2)),
"*",
TokenType::Asterisk,
Expression::IntegerLiteral(IntegerLiteral::new(3)),
)),
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::IntegerLiteral(IntegerLiteral::new(4)),
"+",
TokenType::Plus,
Expression::IntegerLiteral(IntegerLiteral::new(5)),
)),
],
)),
)),
"+",
TokenType::Plus,
Expression::Identifier(Identifier::new(TokenType::Ident, "d")),
)),
))],
@ -868,15 +840,13 @@ mod tests {
Expression::Identifier(Identifier::new(TokenType::Ident, "b")),
Expression::IntegerLiteral(IntegerLiteral::new(1)),
Expression::InfixExpression(InfixExpression::new(
TokenType::Asterisk,
Expression::IntegerLiteral(IntegerLiteral::new(2)),
"*",
TokenType::Asterisk,
Expression::IntegerLiteral(IntegerLiteral::new(3)),
)),
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::IntegerLiteral(IntegerLiteral::new(4)),
"+",
TokenType::Plus,
Expression::IntegerLiteral(IntegerLiteral::new(5)),
)),
Expression::CallExpression(CallExpression::new(
@ -884,9 +854,8 @@ mod tests {
vec![
Expression::IntegerLiteral(IntegerLiteral::new(6)),
Expression::InfixExpression(InfixExpression::new(
TokenType::Asterisk,
Expression::IntegerLiteral(IntegerLiteral::new(7)),
"*",
TokenType::Asterisk,
Expression::IntegerLiteral(IntegerLiteral::new(8)),
)),
],
@ -902,35 +871,30 @@ mod tests {
Expression::CallExpression(CallExpression::new(
Expression::Identifier(Identifier::new(TokenType::Ident, "add")),
vec![Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::InfixExpression(InfixExpression::new(
TokenType::Plus,
Expression::Identifier(Identifier::new(TokenType::Ident, "a")),
"+",
TokenType::Plus,
Expression::Identifier(Identifier::new(TokenType::Ident, "b")),
)),
"+",
TokenType::Plus,
Expression::InfixExpression(InfixExpression::new(
TokenType::Slash,
Expression::InfixExpression(InfixExpression::new(
TokenType::Asterisk,
Expression::Identifier(Identifier::new(
TokenType::Ident,
"c",
)),
"*",
TokenType::Asterisk,
Expression::Identifier(Identifier::new(
TokenType::Ident,
"d",
)),
)),
"/",
TokenType::Slash,
Expression::Identifier(Identifier::new(TokenType::Ident, "f")),
)),
)),
"+",
TokenType::Plus,
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();
continue;
}
match program {
Some(prg) => {
for stmt in &prg.statements {
out.write_fmt(format_args!("{}\n", stmt)).unwrap();
}
if let Some(program) = program {
for stmt in &program.statements {
out.write_fmt(format_args!("{}\n", stmt)).unwrap();
}
None => (),
};
}
}

Loading…
Cancel
Save