diff --git a/src/interpret.rs b/src/interpret.rs index e7d8460..43d48d9 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -126,8 +126,8 @@ impl ExecEnv { match kind { // Arithmetic operators. Add | Subtract | Multiply | Divide => { - let lhs = lhs.to_i32(&expr.span)?; - let rhs = rhs.to_i32(&expr.span)?; + let lhs = lhs.try_into_i32(&expr.span)?; + let rhs = rhs.try_into_i32(&expr.span)?; let res = match kind { Add => lhs.checked_add(rhs), @@ -145,8 +145,8 @@ impl ExecEnv { // Numeric comparisons. Less | Greater => { - let lhs = lhs.to_i32(&expr.span)?; - let rhs = rhs.to_i32(&expr.span)?; + let lhs = lhs.try_into_i32(&expr.span)?; + let rhs = rhs.try_into_i32(&expr.span)?; let res = match kind { Less => lhs < rhs, @@ -168,8 +168,8 @@ impl ExecEnv { // Logical connectives. And | Or => { - let lhs = lhs.to_bool(); - let rhs = rhs.to_bool(); + let lhs = lhs.into_bool(); + let rhs = rhs.into_bool(); let res = match kind { And => 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(), // TODO: not too happy with constructing an artificial @@ -225,7 +225,7 @@ impl ExecEnv { .as_ref() .map(|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) }) .unwrap_or(Ok(crate::brian::DEFAULT_TAPE_SIZE_LIMIT))?, @@ -233,7 +233,7 @@ impl ExecEnv { ); } 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); } } diff --git a/src/parser.rs b/src/parser.rs index 53b4cee..893f44b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -80,7 +80,7 @@ impl<'source> Parser<'source> { )), Token::Rickroll => Ok(Stmt::new( self.semi_terminated(StmtKind::Rickroll)?, - start..self.lexer.span().end + start..self.lexer.span().end, )), Token::Identifier(_) @@ -123,7 +123,7 @@ impl<'source> Parser<'source> { match self .lexer .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 { iden: if self.tdark { @@ -206,7 +206,7 @@ impl<'source> Parser<'source> { let next = self .lexer .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)?)) }, start..self.lexer.span().end, @@ -225,13 +225,13 @@ impl<'source> Parser<'source> { Ok(ExprKind::BinOp { lhs: Box::new( lhs.take() - .ok_or(Error::new(ErrorKind::MissingLhs, self.lexer.span()))?, + .ok_or_else(|| Error::new(ErrorKind::MissingLhs, self.lexer.span()))?, ), rhs: { let next = self .lexer .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)?) }, kind, @@ -245,12 +245,12 @@ impl<'source> Parser<'source> { match self .lexer .next() - .ok_or(Error::unexpected_eof(self.lexer.span().start))? + .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))? { t if t == terminate => { - break buf - .take() - .ok_or(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span()))? + break buf.take().ok_or_else(|| { + Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span()) + })? } t => buf = Some(self.parse_expr(t, &mut buf)?), } @@ -266,7 +266,7 @@ impl<'source> Parser<'source> { match self .lexer .next() - .ok_or(Error::unexpected_eof(self.lexer.span().start))? + .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))? { Token::RightCurly => break, t => block.push(self.parse(t)?), @@ -284,14 +284,13 @@ impl<'source> Parser<'source> { match self .lexer .next() - .ok_or(Error::unexpected_eof(self.lexer.span().start))? + .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))? { // Print to stdout Token::Print => { - let stmt = StmtKind::Print(buf.take().ok_or(Error::new( - ErrorKind::UnexpectedToken(Token::Print), - self.lexer.span(), - ))?); + let stmt = StmtKind::Print(buf.take().ok_or_else(|| { + Error::new(ErrorKind::UnexpectedToken(Token::Print), self.lexer.span()) + })?); break self.semi_terminated(stmt)?; } @@ -352,7 +351,7 @@ impl<'source> Parser<'source> { match self .lexer .next() - .ok_or(Error::unexpected_eof(self.lexer.span().start))? + .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))? { Token::RightParen => break, Token::Identifier(i) => { @@ -362,7 +361,7 @@ impl<'source> Parser<'source> { match self .lexer .next() - .ok_or(Error::unexpected_eof(self.lexer.span().start))? + .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))? { Token::Comma => continue, Token::RightParen => break, @@ -392,7 +391,7 @@ impl<'source> Parser<'source> { let tape_len = match self .lexer .next() - .ok_or(Error::unexpected_eof(self.lexer.span().start))? + .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))? { Token::LeftParen => { let len = Some(self.expr_flow(Token::RightParen)?); @@ -413,7 +412,7 @@ impl<'source> Parser<'source> { match self .lexer .next() - .ok_or(Error::unexpected_eof(self.lexer.span().start))? + .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))? { Token::Plus | Token::Minus @@ -443,7 +442,7 @@ impl<'source> Parser<'source> { match self .lexer .next() - .ok_or(Error::unexpected_eof(self.lexer.span().start))? + .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))? { // End of argument list Token::RightParen => { @@ -478,7 +477,7 @@ impl<'source> Parser<'source> { let init = match self .lexer .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::Semicolon => None, diff --git a/src/variables.rs b/src/variables.rs index 6492cae..f90173e 100644 --- a/src/variables.rs +++ b/src/variables.rs @@ -122,7 +122,7 @@ impl Value { /// Attempt to coerce a Value to an integer. If the conversion /// fails, the generated error message is associated with the /// given span. - pub fn to_i32(self, span: &Range) -> Result { + pub fn try_into_i32(self, span: &Range) -> Result { match self { Value::Int(i) => Ok(i), _ => Err(Error { @@ -133,13 +133,13 @@ impl Value { } /// Coerce a Value to a boolean. The conversion cannot fail. - pub fn to_bool(self) -> bool { + pub fn into_bool(self) -> bool { match self { // Booleans and abooleans have a trivial conversion. Value::Bool(b) => b, Value::Abool(b) => b.into(), // 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. Value::Int(x) => x != 0, // Functios are always truthy.