lint: clippy fixes
This commit is contained in:
parent
5e072c0773
commit
cbdff2c170
5 changed files with 28 additions and 27 deletions
|
@ -13,7 +13,7 @@ struct Args {
|
||||||
#[derive(Debug, Subcommand)]
|
#[derive(Debug, Subcommand)]
|
||||||
enum Command {
|
enum Command {
|
||||||
#[clap(name = "-c", about = "Run a program passed in as a string")]
|
#[clap(name = "-c", about = "Run a program passed in as a string")]
|
||||||
RunCommand { command: String },
|
RunCmd { command: String },
|
||||||
|
|
||||||
#[clap(about = "Run a Mute file")]
|
#[clap(about = "Run a Mute file")]
|
||||||
Run { file: String },
|
Run { file: String },
|
||||||
|
@ -28,15 +28,15 @@ fn main() {
|
||||||
match args.command {
|
match args.command {
|
||||||
Command::Run { file } => {
|
Command::Run { file } => {
|
||||||
let input = std::fs::read_to_string(file).unwrap();
|
let input = std::fs::read_to_string(file).unwrap();
|
||||||
let env = Environment::new();
|
let env = Environment::default();
|
||||||
let res = run(&env, &input);
|
let res = run(&env, &input);
|
||||||
match res {
|
match res {
|
||||||
Ok(expressions) => expressions.into_iter().for_each(|expr| println!("{expr}")),
|
Ok(expressions) => expressions.into_iter().for_each(|expr| println!("{expr}")),
|
||||||
Err(err) => println!("{}", err),
|
Err(err) => println!("{}", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::RunCommand { command } => {
|
Command::RunCmd { command } => {
|
||||||
let env = Environment::new();
|
let env = Environment::default();
|
||||||
let res = run(&env, &command);
|
let res = run(&env, &command);
|
||||||
match res {
|
match res {
|
||||||
Ok(expressions) => expressions.into_iter().for_each(|expr| println!("{expr}")),
|
Ok(expressions) => expressions.into_iter().for_each(|expr| println!("{expr}")),
|
||||||
|
@ -49,11 +49,11 @@ fn main() {
|
||||||
|
|
||||||
fn run(env: &Environment, command: &str) -> Result<Vec<Node>> {
|
fn run(env: &Environment, command: &str) -> Result<Vec<Node>> {
|
||||||
let ast = parse(command)?;
|
let ast = parse(command)?;
|
||||||
eval(&env, ast)
|
eval(env, ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn repl() {
|
fn repl() {
|
||||||
let env = Environment::new();
|
let env = Environment::default();
|
||||||
let mut rl = DefaultEditor::new().unwrap();
|
let mut rl = DefaultEditor::new().unwrap();
|
||||||
|
|
||||||
println!("Mute -- REPL");
|
println!("Mute -- REPL");
|
||||||
|
|
1
src/env/core.rs
vendored
1
src/env/core.rs
vendored
|
@ -68,7 +68,6 @@ pub(super) fn core() -> HashMap<String, Node> {
|
||||||
},
|
},
|
||||||
_ => Ok(node),
|
_ => Ok(node),
|
||||||
})
|
})
|
||||||
.map(|node| node)
|
|
||||||
.collect::<Result<Vec<Node>, _>>()?;
|
.collect::<Result<Vec<Node>, _>>()?;
|
||||||
|
|
||||||
Ok(Node::List(inner))
|
Ok(Node::List(inner))
|
||||||
|
|
34
src/env/mod.rs
vendored
34
src/env/mod.rs
vendored
|
@ -17,22 +17,6 @@ pub struct RawEnvironment {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Environment {
|
impl Environment {
|
||||||
pub fn new() -> Environment {
|
|
||||||
let mut env = HashMap::new();
|
|
||||||
let core = core::core();
|
|
||||||
let io = io::io();
|
|
||||||
|
|
||||||
env.extend(core);
|
|
||||||
env.extend(io);
|
|
||||||
|
|
||||||
let env = RawEnvironment {
|
|
||||||
current: RefCell::new(env),
|
|
||||||
outer: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
Environment(Rc::new(env))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get(&self, ident: &str) -> Option<Node> {
|
pub fn get(&self, ident: &str) -> Option<Node> {
|
||||||
if let Some(val) = self.0.current.borrow().get(ident) {
|
if let Some(val) = self.0.current.borrow().get(ident) {
|
||||||
return Some(val.clone());
|
return Some(val.clone());
|
||||||
|
@ -54,3 +38,21 @@ impl Environment {
|
||||||
self.0.current.borrow_mut().insert(ident, val);
|
self.0.current.borrow_mut().insert(ident, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::default::Default for Environment {
|
||||||
|
fn default() -> Self {
|
||||||
|
let mut env = HashMap::new();
|
||||||
|
let core = core::core();
|
||||||
|
let io = io::io();
|
||||||
|
|
||||||
|
env.extend(core);
|
||||||
|
env.extend(io);
|
||||||
|
|
||||||
|
let env = RawEnvironment {
|
||||||
|
current: RefCell::new(env),
|
||||||
|
outer: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
Environment(Rc::new(env))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -198,7 +198,7 @@ mod test {
|
||||||
#[case("(str (+ 1 2))", "3")]
|
#[case("(str (+ 1 2))", "3")]
|
||||||
#[case("(str (list 1 2 3))", "(1 2 3)")]
|
#[case("(str (list 1 2 3))", "(1 2 3)")]
|
||||||
fn test_evaluator(#[case] input: &str, #[case] expected: &str) {
|
fn test_evaluator(#[case] input: &str, #[case] expected: &str) {
|
||||||
let env = Environment::new();
|
let env = Environment::default();
|
||||||
let ast = parser::parse_str(input).unwrap();
|
let ast = parser::parse_str(input).unwrap();
|
||||||
let res = eval(&env, ast)
|
let res = eval(&env, ast)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -215,7 +215,7 @@ mod test {
|
||||||
#[case("(not-a-func :uwu)")]
|
#[case("(not-a-func :uwu)")]
|
||||||
#[case("{:a}")]
|
#[case("{:a}")]
|
||||||
fn test_evaluator_fail(#[case] input: &str) {
|
fn test_evaluator_fail(#[case] input: &str) {
|
||||||
let env = Environment::new();
|
let env = Environment::default();
|
||||||
let ast = parser::parse_str(input).unwrap();
|
let ast = parser::parse_str(input).unwrap();
|
||||||
let res = eval(&env, ast);
|
let res = eval(&env, ast);
|
||||||
|
|
||||||
|
|
|
@ -8,12 +8,12 @@ use crate::{Error, Node};
|
||||||
|
|
||||||
pub fn parse_str(input: &str) -> crate::Result<Vec<Node>> {
|
pub fn parse_str(input: &str) -> crate::Result<Vec<Node>> {
|
||||||
let mut tokens = lexer::read(input)
|
let mut tokens = lexer::read(input)
|
||||||
.map_err(|err| Error::ParserError(err))?
|
.map_err(Error::ParserError)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.peekable();
|
.peekable();
|
||||||
let mut ast = Vec::new();
|
let mut ast = Vec::new();
|
||||||
|
|
||||||
while let Some(node) = next_statement(&mut tokens).map_err(|err| Error::ParserError(err))? {
|
while let Some(node) = next_statement(&mut tokens).map_err(Error::ParserError)? {
|
||||||
ast.push(node)
|
ast.push(node)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue