diff options
| author | Cody <cody@codyq.dev> | 2023-02-27 12:29:02 -0600 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-02-27 12:29:02 -0600 |
| commit | 0c5616a91d7280341dc6aa522daf04d151108d4e (patch) | |
| tree | 66764fc099073092c0359ddf0d18c12a1f824cd9 | |
| parent | e4199d2837d2179f17e97b8d50366d96c8babded (diff) | |
| download | sloth-0c5616a91d7280341dc6aa522daf04d151108d4e.tar.gz | |
Cleaned up some code
| -rw-r--r-- | examples/guessing.sloth | 2 | ||||
| -rw-r--r-- | src/ast/mod.rs | 2 | ||||
| -rw-r--r-- | src/ast/parser.rs | 25 | ||||
| -rw-r--r-- | src/interpreter.rs | 15 | ||||
| -rw-r--r-- | src/main.rs | 22 |
5 files changed, 20 insertions, 46 deletions
diff --git a/examples/guessing.sloth b/examples/guessing.sloth index 5d69944..1938269 100644 --- a/examples/guessing.sloth +++ b/examples/guessing.sloth @@ -4,7 +4,7 @@ var tries = 0; var correct = false; while !correct { - print("\nPick a number between 1 and 5: "); + print("\nPick a number between 1 and 10: "); val human = parse_int(readln()); if human == computer { diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 4e1d639..b3e7c36 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1,5 +1,3 @@ -use std::fmt::Display; - use crate::lexer::{Literal, TokenType}; pub mod parser; diff --git a/src/ast/parser.rs b/src/ast/parser.rs index bc29f94..85be68c 100644 --- a/src/ast/parser.rs +++ b/src/ast/parser.rs @@ -1,5 +1,5 @@ use super::{Expr, Stmt}; -use crate::lexer::{Literal, Token, TokenType}; +use crate::lexer::{Token, TokenType}; pub struct AstParser<'a> { tokens: Vec<Token<'a>>, @@ -11,19 +11,10 @@ impl<'a> AstParser<'a> { pub fn new(tokens: Vec<Token<'a>>) -> Self { Self { tokens, index: 0 } } - - fn previous(&self) -> Option<&Token> { - self.tokens.get(self.index - 1) - } - fn peek(&self) -> &Token { &self.tokens[self.index] } - fn peek_nth(&self, nth: usize) -> Option<&Token> { - self.tokens.get(self.index + nth) - } - fn advance(&mut self) -> Option<&Token> { if self.eof() { return None; @@ -50,16 +41,6 @@ impl<'a> AstParser<'a> { self.advance_if(|it| it.tt == *next) } - fn advance_seq(&mut self, seq: &[TokenType]) -> bool { - for token in seq { - if !self.advance_if_eq(token) { - return false; - } - } - - true - } - fn consume(&mut self, next: TokenType, error: &str) { if std::mem::discriminant(&self.peek().tt) != std::mem::discriminant(&next) { panic!("{error}"); @@ -309,6 +290,7 @@ macro_rules! binary_expr { } #[rustfmt::skip] +#[allow(unused_parens)] impl<'a> AstParser<'a> { // Binary expressions in order of precedence from lowest to highest. binary_expr!(logical_or , logical_and , (TokenType::PipePipe)); @@ -385,9 +367,6 @@ mod tests { #[test] fn basic_expression_c() { - let lexer = Lexer::new("9 > 6 && 5 + 7 == 32 || \"apple\" != \"banana\""); - let tokens = lexer.collect_vec(); - // TODO: } } diff --git a/src/interpreter.rs b/src/interpreter.rs index adf6755..a7937db 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::fmt::{Debug, Display}; +use std::fmt::Display; use itertools::Itertools; @@ -42,10 +42,10 @@ impl AstVisitor<Value> for AstInterpreter { self.memory.insert(ident.clone(), (value, true)); } Stmt::Function { - ident: name, - arguments, - return_type, - body, + ident: _, + arguments: _, + return_type: _, + body: _, } => todo!(), Stmt::If { condition, body } => { let result = self.visit_expr(condition); @@ -74,7 +74,7 @@ impl AstVisitor<Value> for AstInterpreter { self.interpret(body); } } - Stmt::Return { value } => todo!(), + Stmt::Return { value: _ } => todo!(), }; // FIXME: Honestly should probably abandon this "visitor" pattern. 2 functions @@ -168,7 +168,6 @@ impl AstVisitor<Value> for AstInterpreter { self.callables.insert(ident.clone(), callable); result } - _ => unimplemented!("{:?}", expr), } } } @@ -196,7 +195,7 @@ pub trait SlothCallable { pub struct InternalFunction<'a>(pub &'a dyn Fn(&[Value]) -> Value); impl<'a> SlothCallable for InternalFunction<'a> { - fn call(&self, interpreter: &mut AstInterpreter, args: &[Value]) -> Value { + fn call(&self, _interpreter: &mut AstInterpreter, args: &[Value]) -> Value { self.0(args) } } diff --git a/src/main.rs b/src/main.rs index 4ee455c..91db8c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,20 +6,18 @@ clippy::manual_ok_or, unused_lifetimes )] -#![allow(unused)] pub mod ast; pub mod interpreter; pub mod lexer; -use std::io::{self, BufRead, Read, Write}; -use std::{env, fs}; +use std::io::{BufRead, Write}; +use std::{env, fs, io}; use itertools::Itertools; use rand::Rng; use crate::ast::parser::AstParser; -use crate::ast::AstVisitor; use crate::interpreter::{AstInterpreter, InternalFunction, Value}; use crate::lexer::Lexer; @@ -55,12 +53,12 @@ fn main() { let mut buffer = String::new(); for arg in args { - write!(&mut buffer, "{}", arg); + write!(&mut buffer, "{}", arg).unwrap(); } let mut stdout = io::stdout(); - stdout.lock().write_all(buffer.as_bytes()); - stdout.flush(); + stdout.lock().write_all(buffer.as_bytes()).unwrap(); + stdout.flush().unwrap(); Value::Nil })), @@ -73,13 +71,13 @@ fn main() { let mut buffer = String::new(); for arg in args { - write!(&mut buffer, "{}", arg); + write!(&mut buffer, "{}", arg).unwrap(); } - writeln!(&mut buffer); + writeln!(&mut buffer).unwrap(); let mut stdout = io::stdout(); - stdout.lock().write_all(buffer.as_bytes()); - stdout.flush(); + stdout.lock().write_all(buffer.as_bytes()).unwrap(); + stdout.flush().unwrap(); Value::Nil })), @@ -87,7 +85,7 @@ fn main() { interpreter.callables.insert( "readln".to_owned(), - Box::new(InternalFunction(&|args| { + Box::new(InternalFunction(&|_| { let stdin = io::stdin(); let mut line = String::new(); stdin |
