From 1e053f840e651e0d5698d4135e3c2846ab0469d9 Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Mon, 14 Oct 2024 16:37:02 -0400 Subject: [PATCH] fix many warnings --- examples/repl.rs | 10 ++-------- src/executor.rs | 5 +---- src/lib.rs | 19 ++++++++++--------- src/parser.rs | 23 +++++------------------ src/tokenizer.rs | 10 +++------- 5 files changed, 21 insertions(+), 46 deletions(-) diff --git a/examples/repl.rs b/examples/repl.rs index 834c6c9..e978730 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -1,16 +1,10 @@ - -use lamm::{Tokenizer, Parser, Executor}; use std::io::{self, BufReader}; fn main() { - let tokenizer = Tokenizer::new(BufReader::new(io::stdin())); - let parser = Parser::new(tokenizer); - let values = Executor::new(parser); - - for value in values { + for value in lamm::evaluate(BufReader::new(io::stdin())) { match value { Ok(v) => println!("{v}"), Err(e) => eprintln!("{e}"), } } -} \ No newline at end of file +} diff --git a/src/executor.rs b/src/executor.rs index 40d3515..8b5b429 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -1,6 +1,5 @@ use super::{Value, Type, FunctionDeclaration}; use super::parser::{ParseTree, ParseError}; -use super::tokenizer::Op; use std::collections::HashMap; use std::borrow::Cow; @@ -186,8 +185,6 @@ impl>> Executor { self.exec(*scope, &mut Cow::Borrowed(&locals)) } }, - ParseTree::GlobalEqu(ident, body) => todo!(), - ParseTree::LazyGlobalEqu(ident, body) => todo!(), ParseTree::FunctionDefinition(ident, args, r, body, scope) => { let existing = locals.get(&ident).or(self.globals.get(&ident)).cloned(); @@ -197,7 +194,7 @@ impl>> Executor { let locals = locals.to_mut(); locals.insert(ident.clone(), Object::Function(Function { - decl: FunctionDeclaration { name: ident.clone(), r, args }, + decl: FunctionDeclaration { _name: ident.clone(), _r: r, args }, body: Some(body) })); diff --git a/src/lib.rs b/src/lib.rs index 228b459..2173d49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,21 +3,18 @@ mod tokenizer; mod parser; mod executor; -pub use tokenizer::{Tokenizer, TokenizeError}; -pub use parser::{Parser, ParseError}; -pub use executor::{Executor, RuntimeError}; - use std::fmt::Display; +use std::io::BufRead; #[derive(Clone, Debug)] -pub(crate) enum Type { +pub enum Type { Float, Int, Bool, String, Nil, Any, - Function(Box, Vec), + _Function(Box, Vec), } impl Display for Type { @@ -29,7 +26,7 @@ impl Display for Type { Self::String => "String".into(), Self::Nil => "Nil".into(), Self::Any => "Any".into(), - Self::Function(r, _) => format!("Function -> {}", *r) + Self::_Function(r, _) => format!("Function -> {}", *r) }) } } @@ -70,7 +67,11 @@ impl Display for Value { #[derive(Clone, Debug)] pub(crate) struct FunctionDeclaration { - name: String, - r: Type, + _name: String, + _r: Type, args: Vec<(String, Type)>, } + +pub fn evaluate(r: R) -> impl Iterator> { + executor::Executor::new(parser::Parser::new(tokenizer::Tokenizer::new(r))) +} diff --git a/src/parser.rs b/src/parser.rs index a53e134..64285f9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -34,7 +34,7 @@ impl Display for ParseError { impl error::Error for ParseError {} #[derive(Clone, Debug)] -pub enum ParseTree { +pub(crate) enum ParseTree { // Mathematical Operators Add(Box, Box), Sub(Box, Box), @@ -54,8 +54,6 @@ pub enum ParseTree { // Defining Objects Equ(String, Box, Box), LazyEqu(String, Box, Box), - GlobalEqu(String, Box), - LazyGlobalEqu(String, Box), FunctionDefinition(String, Vec<(String, Type)>, Type, Box, Box), // Functional Operations @@ -129,7 +127,7 @@ impl ParseTree { Box::new(ParseTree::parse(tokens, globals, locals)?), Box::new(ParseTree::parse(tokens, globals, locals)?) )), - Op::Equ | Op::LazyEqu | Op::GlobalEqu | Op::LazyGlobalEqu => { + Op::Equ | Op::LazyEqu => { let token = tokens.next() .ok_or(ParseError::UnexpectedEndInput)? .map_err(|e| ParseError::TokenizeError(e))?; @@ -144,12 +142,6 @@ impl ParseTree { Box::new(ParseTree::parse(tokens, globals, locals)?), Box::new(ParseTree::parse(tokens, globals, locals)?) )), - Op::GlobalEqu => Ok(ParseTree::GlobalEqu(ident.clone(), - Box::new(ParseTree::parse(tokens, globals, locals)?) - )), - Op::LazyGlobalEqu => Ok(ParseTree::LazyGlobalEqu(ident.clone(), - Box::new(ParseTree::parse(tokens, globals, locals)?) - )), _ => panic!("Operator literally changed under your nose"), } } else { @@ -173,8 +165,8 @@ impl ParseTree { let locals = locals.to_mut(); locals.insert(ident.clone(), FunctionDeclaration { - name: ident.clone(), - r: Type::Any, + _name: ident.clone(), + _r: Type::Any, args: args.clone(), }); @@ -240,7 +232,7 @@ impl ParseTree { } /// Parses input tokens and produces ParseTrees for an Executor -pub struct Parser>> { +pub(crate) struct Parser>> { tokens: I, // These are used to keep track of functions in the current context @@ -258,11 +250,6 @@ impl>> Parser { locals: HashMap::new() } } - - pub fn globals(mut self, g: HashMap) -> Self { - self.globals = g; - self - } } impl>> Iterator for Parser { diff --git a/src/tokenizer.rs b/src/tokenizer.rs index ecf6c28..f9cd95a 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -33,7 +33,7 @@ impl Display for TokenizeError { impl error::Error for TokenizeError {} #[derive(Debug, Clone)] -pub enum Op { +pub(crate) enum Op { Add, Sub, Mul, @@ -42,8 +42,6 @@ pub enum Op { Equ, Mod, LazyEqu, - GlobalEqu, - LazyGlobalEqu, FunctionDeclare(usize), Compose, Id, @@ -62,7 +60,7 @@ pub enum Op { } #[derive(Debug, Clone)] -pub enum Token { +pub(crate) enum Token { Identifier(String), Operator(Op), Constant(Value), @@ -100,8 +98,6 @@ impl Token { "%" => Ok(Token::Operator(Op::Mod)), "=" => Ok(Token::Operator(Op::Equ)), "." => Ok(Token::Operator(Op::LazyEqu)), - "=>" => Ok(Token::Operator(Op::GlobalEqu)), - ".>" => Ok(Token::Operator(Op::LazyGlobalEqu)), "~" => Ok(Token::Operator(Op::Compose)), "," => Ok(Token::Operator(Op::Id)), "?" => Ok(Token::Operator(Op::If)), @@ -149,7 +145,7 @@ impl Token { } /// Tokenize an input stream of source code for a Parser -pub struct Tokenizer { +pub(crate) struct Tokenizer { reader: R, tokens: VecDeque, }