Fix some clippy pedantic errors

This commit is contained in:
Julius 2022-07-04 00:11:55 +02:00
parent 699ebeda48
commit 7236b204b9
Signed by: j00lz
GPG key ID: AF241B0AA237BBA2
7 changed files with 182 additions and 220 deletions

View file

@ -1,4 +1,7 @@
use std::{collections::HashMap, sync::{Arc, RwLock}}; use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
use crate::value::Value; use crate::value::Value;
@ -11,7 +14,7 @@ pub struct Env<'env> {
impl Env<'_> { impl Env<'_> {
pub fn new() -> Self { pub fn new() -> Self {
Env { Env {
values: Default::default(), values: Arc::default(),
parent: None, parent: None,
} }
} }
@ -58,7 +61,7 @@ impl Default for Env<'_> {
impl<'env> Env<'env> { impl<'env> Env<'env> {
pub fn new_with_parent(parent: &'env Env<'env>) -> Self { pub fn new_with_parent(parent: &'env Env<'env>) -> Self {
Env { Env {
values: Default::default(), values: Arc::default(),
parent: Some(parent), parent: Some(parent),
} }
} }

View file

@ -14,7 +14,7 @@ macro_rules! impl_function {
let total_count = { let total_count = {
let mut count = 0; let mut count = 0;
$( $(
let _: Option<$ty> = None; let _n: Option<$ty> = None;
count += 1; count += 1;
)* )*
count count
@ -44,7 +44,7 @@ macro_rules! impl_function {
let total_count = { let total_count = {
let mut count = 0; let mut count = 0;
$( $(
let _: Option<$ty> = None; let _n: Option<$ty> = None;
count += 1; count += 1;
)* )*
count count
@ -79,7 +79,7 @@ macro_rules! impl_ref_function {
let total_count = { let total_count = {
let mut count = 1; let mut count = 1;
$( $(
let _: Option<$ty> = None; let _n: Option<$ty> = None;
count += 1; count += 1;
)* )*
count count
@ -114,7 +114,7 @@ macro_rules! impl_ref_function {
let total_count = { let total_count = {
let mut count = 1; let mut count = 1;
$( $(
let _: Option<$ty> = None; let _n: Option<$ty> = None;
count += 1; count += 1;
)* )*
count count

View file

@ -20,7 +20,7 @@ pub struct FunctionMap(HashMap<String, (Box<dyn Function>, FunctionType)>);
impl FunctionMap { impl FunctionMap {
pub fn new() -> Self { pub fn new() -> Self {
Default::default() FunctionMap::default()
} }
pub fn insert_native<S, P, F>(&mut self, name: S, f: F) pub fn insert_native<S, P, F>(&mut self, name: S, f: F)
@ -107,7 +107,7 @@ impl Function for InterpreterFunction {
for (name, value) in self.parameters.iter().zip(args) { for (name, value) in self.parameters.iter().zip(args) {
sub_env.set_here(name, value.clone()); sub_env.set_here(name, value.clone());
} }
for stmt in self.body.iter() { for stmt in &self.body {
stmt.eval(&sub_env, map); stmt.eval(&sub_env, map);
} }
Ok(Value::Nothing) Ok(Value::Nothing)

View file

@ -15,9 +15,9 @@ mod value;
// mod parser; // mod parser;
mod pest_parser; mod pest_parser;
fn run(functions: FunctionMap, statement: Statement) { fn run(functions: &FunctionMap, statement: &Statement) {
let env = Env::new(); let env = Env::new();
statement.eval(&env, &functions); statement.eval(&env, functions);
} }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
@ -59,7 +59,7 @@ fn functions(&self) -> Option<FunctionMap> {
fn main() { fn main() {
let f = read_to_string("./test.foo").unwrap(); let f = read_to_string("./test.foo").unwrap();
let (res, funcs) = pest_parser::parse(f); let (res, funcs) = pest_parser::parse(&f);
let mut m = FunctionMap::new(); let mut m = FunctionMap::new();
m.insert_holder(funcs); m.insert_holder(funcs);
m.insert_native("print", |a: &Value| { m.insert_native("print", |a: &Value| {
@ -68,5 +68,5 @@ fn main() {
m.insert_native("waluigi", || "Waluigi"); m.insert_native("waluigi", || "Waluigi");
m.insert_native("native_test", || Foo { x: 41 }); m.insert_native("native_test", || Foo { x: 41 });
run(m, res); run(&m, &res);
} }

View file

@ -7,7 +7,7 @@ use crate::{expression::Expression, statement::Statement, value::Value};
#[grammar = "grammar.pest"] #[grammar = "grammar.pest"]
pub struct LangParser; pub struct LangParser;
pub fn parse<S: ToString>(source: S) -> (Statement, FuncHolder) { pub fn parse<S: ToString>(source: &S) -> (Statement, FuncHolder) {
let source = source.to_string(); let source = source.to_string();
let pairs = LangParser::parse(Rule::root_statement, &source).unwrap(); let pairs = LangParser::parse(Rule::root_statement, &source).unwrap();
handle_rules(pairs) handle_rules(pairs)
@ -53,7 +53,11 @@ impl IntoIterator for FuncHolder {
} }
fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder) { fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder) {
fn parse_statement(pair: Pair<Rule>) -> (Statement, FuncHolder) { let z = p.next().unwrap();
parse_statement(z)
}
fn parse_statement(pair: Pair<Rule>) -> (Statement, FuncHolder) {
match pair.as_rule() { match pair.as_rule() {
Rule::root_statement => { Rule::root_statement => {
let (z, f) = pair.into_inner().map(parse_statement).unzip(); let (z, f) = pair.into_inner().map(parse_statement).unzip();
@ -132,58 +136,15 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
Rule::EOI => (Statement::Body(vec![]), FuncHolder::default()), Rule::EOI => (Statement::Body(vec![]), FuncHolder::default()),
x => unreachable!("How did we get to {:?}", x), x => unreachable!("How did we get to {:?}", x),
} }
} }
fn parse_expression(pair: Pair<Rule>) -> Expression { fn parse_expression(pair: Pair<Rule>) -> Expression {
match pair.as_rule() { match pair.as_rule() {
Rule::expression => { Rule::expression => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let expr = parse_expression(inner.next().unwrap()); parse_expression(inner.next().unwrap())
if let Some(c) = inner.next() {
let mut inner = c.into_inner();
let name = inner.next().unwrap();
let args = inner
.next()
.unwrap()
.into_inner()
.map(parse_expression)
.collect();
Expression::CallMethod(Box::new(expr), name.as_str().to_string(), args)
} else {
expr
} }
} Rule::equality | Rule::inequality | Rule::sum | Rule::product => {
Rule::equality => {
let mut inner = pair.into_inner();
let lhs = parse_expression(inner.next().unwrap());
if let Some(op) = inner.next() {
let rhs = parse_expression(inner.next().unwrap());
Expression::Binary(Box::new(lhs), op.as_str().parse().unwrap(), Box::new(rhs))
} else {
lhs
}
}
Rule::inequality => {
let mut inner = pair.into_inner();
let lhs = parse_expression(inner.next().unwrap());
if let Some(op) = inner.next() {
let rhs = parse_expression(inner.next().unwrap());
Expression::Binary(Box::new(lhs), op.as_str().parse().unwrap(), Box::new(rhs))
} else {
lhs
}
}
Rule::sum => {
let mut inner = pair.into_inner();
let lhs = parse_expression(inner.next().unwrap());
if let Some(op) = inner.next() {
let rhs = parse_expression(inner.next().unwrap());
Expression::Binary(Box::new(lhs), op.as_str().parse().unwrap(), Box::new(rhs))
} else {
lhs
}
}
Rule::product => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let lhs = parse_expression(inner.next().unwrap()); let lhs = parse_expression(inner.next().unwrap());
if let Some(op) = inner.next() { if let Some(op) = inner.next() {
@ -204,9 +165,9 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
} }
_ => unreachable!(), _ => unreachable!(),
} }
} }
fn method_call(pair: Pair<Rule>) -> Option<(String, Vec<Expression>)> { fn method_call(pair: Pair<Rule>) -> Option<(String, Vec<Expression>)> {
match pair.as_rule() { match pair.as_rule() {
Rule::method_call => { Rule::method_call => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
@ -224,12 +185,12 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
} }
_ => None, _ => None,
} }
} }
fn parse_value(pair: Pair<Rule>) -> Expression { fn parse_value(pair: Pair<Rule>) -> Expression {
match pair.as_rule() { match pair.as_rule() {
Rule::const_value => { Rule::const_value => {
Expression::Constant(parse_const_value(pair.into_inner().next().unwrap())) Expression::Constant(parse_const_value(&pair.into_inner().next().unwrap()))
} }
Rule::identifier => Expression::Variable(pair.as_str().to_string()), Rule::identifier => Expression::Variable(pair.as_str().to_string()),
Rule::call => { Rule::call => {
@ -249,9 +210,9 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
} }
_ => unreachable!(), _ => unreachable!(),
} }
} }
fn parse_const_value(pair: Pair<Rule>) -> Value { fn parse_const_value(pair: &Pair<Rule>) -> Value {
match pair.as_rule() { match pair.as_rule() {
Rule::number => { Rule::number => {
if let Ok(f) = pair.as_str().parse::<i32>() { if let Ok(f) = pair.as_str().parse::<i32>() {
@ -267,8 +228,4 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
Rule::char => Value::Char(pair.as_str().chars().next().unwrap()), Rule::char => Value::Char(pair.as_str().chars().next().unwrap()),
_ => unreachable!(), _ => unreachable!(),
} }
}
let z = p.next().unwrap();
parse_statement(z)
} }

View file

@ -30,6 +30,10 @@ pub trait CustomValue: std::fmt::Debug + BoxClone {
fn functions(&self) -> Option<FunctionMap> { fn functions(&self) -> Option<FunctionMap> {
None None
} }
fn to_bool(&self) -> bool {
true
}
} }
impl PartialEq<Value> for dyn CustomValue { impl PartialEq<Value> for dyn CustomValue {

View file

@ -34,9 +34,8 @@ impl Value {
Value::String(v) => !v.is_empty(), Value::String(v) => !v.is_empty(),
Value::Bool(v) => *v, Value::Bool(v) => *v,
Value::Char(v) => *v != '\0', Value::Char(v) => *v != '\0',
Value::Custom(_) => false, Value::Custom(c) => c.to_bool(),
Value::Nothing => false, Value::Nothing | Value::Error => false,
Value::Error => false,
} }
} }
@ -155,7 +154,7 @@ impl Display for Value {
Value::Char(v) => v.to_string(), Value::Char(v) => v.to_string(),
Value::Nothing => "()".to_string(), Value::Nothing => "()".to_string(),
Value::Error => "Error".to_string(), Value::Error => "Error".to_string(),
_ => unreachable!(), Value::Custom(_) => unreachable!(),
} }
) )
} }
@ -234,8 +233,7 @@ impl PartialEq for Value {
(Value::Char(lhs), Value::Char(rhs)) => lhs == rhs, (Value::Char(lhs), Value::Char(rhs)) => lhs == rhs,
(Value::Custom(lhs), other) => lhs.as_ref() == other, (Value::Custom(lhs), other) => lhs.as_ref() == other,
(other, Value::Custom(rhs)) => rhs.as_ref() == other, (other, Value::Custom(rhs)) => rhs.as_ref() == other,
(Value::Nothing, Value::Nothing) => true, (Value::Nothing, Value::Nothing) | (Value::Error, Value::Error) => true,
(Value::Error, Value::Error) => true,
_ => false, _ => false,
} }
} }