Clippy conformance

This commit is contained in:
Alex Bethel 2021-06-16 10:35:06 -05:00
parent cdb4a22443
commit 4011997e60
3 changed files with 32 additions and 33 deletions

View file

@ -126,8 +126,8 @@ impl ExecEnv {
match kind { match kind {
// Arithmetic operators. // Arithmetic operators.
Add | Subtract | Multiply | Divide => { Add | Subtract | Multiply | Divide => {
let lhs = lhs.to_i32(&expr.span)?; let lhs = lhs.try_into_i32(&expr.span)?;
let rhs = rhs.to_i32(&expr.span)?; let rhs = rhs.try_into_i32(&expr.span)?;
let res = match kind { let res = match kind {
Add => lhs.checked_add(rhs), Add => lhs.checked_add(rhs),
@ -145,8 +145,8 @@ impl ExecEnv {
// Numeric comparisons. // Numeric comparisons.
Less | Greater => { Less | Greater => {
let lhs = lhs.to_i32(&expr.span)?; let lhs = lhs.try_into_i32(&expr.span)?;
let rhs = rhs.to_i32(&expr.span)?; let rhs = rhs.try_into_i32(&expr.span)?;
let res = match kind { let res = match kind {
Less => lhs < rhs, Less => lhs < rhs,
@ -168,8 +168,8 @@ impl ExecEnv {
// Logical connectives. // Logical connectives.
And | Or => { And | Or => {
let lhs = lhs.to_bool(); let lhs = lhs.into_bool();
let rhs = rhs.to_bool(); let rhs = rhs.into_bool();
let res = match kind { let res = match kind {
And => lhs && rhs, And => lhs && rhs,
Or => lhs || rhs, Or => lhs || rhs,
@ -179,7 +179,7 @@ impl ExecEnv {
} }
} }
} }
Not(expr) => Bool(!self.eval_expr(&expr)?.to_bool()), Not(expr) => Bool(!self.eval_expr(&expr)?.into_bool()),
Literal(value) => value.clone(), Literal(value) => value.clone(),
// TODO: not too happy with constructing an artificial // TODO: not too happy with constructing an artificial
@ -225,7 +225,7 @@ impl ExecEnv {
.as_ref() .as_ref()
.map(|tape_len| { .map(|tape_len| {
self.eval_expr(tape_len) self.eval_expr(tape_len)
.and_then(|v| v.to_i32(&stmt.span)) .and_then(|v| v.try_into_i32(&stmt.span))
.map(|len| len as usize) .map(|len| len as usize)
}) })
.unwrap_or(Ok(crate::brian::DEFAULT_TAPE_SIZE_LIMIT))?, .unwrap_or(Ok(crate::brian::DEFAULT_TAPE_SIZE_LIMIT))?,
@ -233,7 +233,7 @@ impl ExecEnv {
); );
} }
StmtKind::If { cond, body } => { StmtKind::If { cond, body } => {
if self.eval_expr(cond)?.to_bool() { if self.eval_expr(cond)?.into_bool() {
return self.eval_stmts_hs(&body.block, true); return self.eval_stmts_hs(&body.block, true);
} }
} }

View file

@ -80,7 +80,7 @@ impl<'source> Parser<'source> {
)), )),
Token::Rickroll => Ok(Stmt::new( Token::Rickroll => Ok(Stmt::new(
self.semi_terminated(StmtKind::Rickroll)?, self.semi_terminated(StmtKind::Rickroll)?,
start..self.lexer.span().end start..self.lexer.span().end,
)), )),
Token::Identifier(_) Token::Identifier(_)
@ -123,7 +123,7 @@ impl<'source> Parser<'source> {
match self match self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
{ {
Token::Identifier(iden) => Ok(Iden { Token::Identifier(iden) => Ok(Iden {
iden: if self.tdark { iden: if self.tdark {
@ -206,7 +206,7 @@ impl<'source> Parser<'source> {
let next = self let next = self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))?; .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?;
ExprKind::Not(Box::new(self.parse_expr(next, buf)?)) ExprKind::Not(Box::new(self.parse_expr(next, buf)?))
}, },
start..self.lexer.span().end, start..self.lexer.span().end,
@ -225,13 +225,13 @@ impl<'source> Parser<'source> {
Ok(ExprKind::BinOp { Ok(ExprKind::BinOp {
lhs: Box::new( lhs: Box::new(
lhs.take() lhs.take()
.ok_or(Error::new(ErrorKind::MissingLhs, self.lexer.span()))?, .ok_or_else(|| Error::new(ErrorKind::MissingLhs, self.lexer.span()))?,
), ),
rhs: { rhs: {
let next = self let next = self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))?; .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?;
Box::new(self.parse_expr(next, &mut None)?) Box::new(self.parse_expr(next, &mut None)?)
}, },
kind, kind,
@ -245,12 +245,12 @@ impl<'source> Parser<'source> {
match self match self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
{ {
t if t == terminate => { t if t == terminate => {
break buf break buf.take().ok_or_else(|| {
.take() Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())
.ok_or(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span()))? })?
} }
t => buf = Some(self.parse_expr(t, &mut buf)?), t => buf = Some(self.parse_expr(t, &mut buf)?),
} }
@ -266,7 +266,7 @@ impl<'source> Parser<'source> {
match self match self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
{ {
Token::RightCurly => break, Token::RightCurly => break,
t => block.push(self.parse(t)?), t => block.push(self.parse(t)?),
@ -284,14 +284,13 @@ impl<'source> Parser<'source> {
match self match self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
{ {
// Print to stdout // Print to stdout
Token::Print => { Token::Print => {
let stmt = StmtKind::Print(buf.take().ok_or(Error::new( let stmt = StmtKind::Print(buf.take().ok_or_else(|| {
ErrorKind::UnexpectedToken(Token::Print), Error::new(ErrorKind::UnexpectedToken(Token::Print), self.lexer.span())
self.lexer.span(), })?);
))?);
break self.semi_terminated(stmt)?; break self.semi_terminated(stmt)?;
} }
@ -352,7 +351,7 @@ impl<'source> Parser<'source> {
match self match self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
{ {
Token::RightParen => break, Token::RightParen => break,
Token::Identifier(i) => { Token::Identifier(i) => {
@ -362,7 +361,7 @@ impl<'source> Parser<'source> {
match self match self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
{ {
Token::Comma => continue, Token::Comma => continue,
Token::RightParen => break, Token::RightParen => break,
@ -392,7 +391,7 @@ impl<'source> Parser<'source> {
let tape_len = match self let tape_len = match self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
{ {
Token::LeftParen => { Token::LeftParen => {
let len = Some(self.expr_flow(Token::RightParen)?); let len = Some(self.expr_flow(Token::RightParen)?);
@ -413,7 +412,7 @@ impl<'source> Parser<'source> {
match self match self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
{ {
Token::Plus Token::Plus
| Token::Minus | Token::Minus
@ -443,7 +442,7 @@ impl<'source> Parser<'source> {
match self match self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
{ {
// End of argument list // End of argument list
Token::RightParen => { Token::RightParen => {
@ -478,7 +477,7 @@ impl<'source> Parser<'source> {
let init = match self let init = match self
.lexer .lexer
.next() .next()
.ok_or(Error::unexpected_eof(self.lexer.span().start))? .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
{ {
Token::Equal => Some(self.expr_flow(Token::Semicolon)?), Token::Equal => Some(self.expr_flow(Token::Semicolon)?),
Token::Semicolon => None, Token::Semicolon => None,

View file

@ -122,7 +122,7 @@ impl Value {
/// Attempt to coerce a Value to an integer. If the conversion /// Attempt to coerce a Value to an integer. If the conversion
/// fails, the generated error message is associated with the /// fails, the generated error message is associated with the
/// given span. /// given span.
pub fn to_i32(self, span: &Range<usize>) -> Result<i32, Error> { pub fn try_into_i32(self, span: &Range<usize>) -> Result<i32, Error> {
match self { match self {
Value::Int(i) => Ok(i), Value::Int(i) => Ok(i),
_ => Err(Error { _ => Err(Error {
@ -133,13 +133,13 @@ impl Value {
} }
/// Coerce a Value to a boolean. The conversion cannot fail. /// Coerce a Value to a boolean. The conversion cannot fail.
pub fn to_bool(self) -> bool { pub fn into_bool(self) -> bool {
match self { match self {
// Booleans and abooleans have a trivial conversion. // Booleans and abooleans have a trivial conversion.
Value::Bool(b) => b, Value::Bool(b) => b,
Value::Abool(b) => b.into(), Value::Abool(b) => b.into(),
// The empty string is falsey, other strings are truthy. // The empty string is falsey, other strings are truthy.
Value::Str(s) => s.len() != 0, Value::Str(s) => !s.is_empty(),
// 0 is falsey, nonzero is truthy. // 0 is falsey, nonzero is truthy.
Value::Int(x) => x != 0, Value::Int(x) => x != 0,
// Functios are always truthy. // Functios are always truthy.