Fix some clippy pedantic errors

main
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;
@ -11,7 +14,7 @@ pub struct Env<'env> {
impl Env<'_> {
pub fn new() -> Self {
Env {
values: Default::default(),
values: Arc::default(),
parent: None,
}
}
@ -58,7 +61,7 @@ impl Default for Env<'_> {
impl<'env> Env<'env> {
pub fn new_with_parent(parent: &'env Env<'env>) -> Self {
Env {
values: Default::default(),
values: Arc::default(),
parent: Some(parent),
}
}

View File

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

View File

@ -20,7 +20,7 @@ pub struct FunctionMap(HashMap<String, (Box<dyn Function>, FunctionType)>);
impl FunctionMap {
pub fn new() -> Self {
Default::default()
FunctionMap::default()
}
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) {
sub_env.set_here(name, value.clone());
}
for stmt in self.body.iter() {
for stmt in &self.body {
stmt.eval(&sub_env, map);
}
Ok(Value::Nothing)

View File

@ -15,9 +15,9 @@ mod value;
// mod parser;
mod pest_parser;
fn run(functions: FunctionMap, statement: Statement) {
fn run(functions: &FunctionMap, statement: &Statement) {
let env = Env::new();
statement.eval(&env, &functions);
statement.eval(&env, functions);
}
#[derive(Clone, Debug, PartialEq, Eq)]
@ -59,7 +59,7 @@ fn functions(&self) -> Option<FunctionMap> {
fn main() {
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();
m.insert_holder(funcs);
m.insert_native("print", |a: &Value| {
@ -68,5 +68,5 @@ fn main() {
m.insert_native("waluigi", || "Waluigi");
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"]
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 pairs = LangParser::parse(Rule::root_statement, &source).unwrap();
handle_rules(pairs)
@ -53,6 +53,10 @@ impl IntoIterator for FuncHolder {
}
fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder) {
let z = p.next().unwrap();
parse_statement(z)
}
fn parse_statement(pair: Pair<Rule>) -> (Statement, FuncHolder) {
match pair.as_rule() {
Rule::root_statement => {
@ -138,52 +142,9 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
match pair.as_rule() {
Rule::expression => {
let mut inner = pair.into_inner();
let expr = 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
parse_expression(inner.next().unwrap())
}
}
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 => {
Rule::equality | Rule::inequality | Rule::sum | Rule::product => {
let mut inner = pair.into_inner();
let lhs = parse_expression(inner.next().unwrap());
if let Some(op) = inner.next() {
@ -229,7 +190,7 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
fn parse_value(pair: Pair<Rule>) -> Expression {
match pair.as_rule() {
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::call => {
@ -251,7 +212,7 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
}
}
fn parse_const_value(pair: Pair<Rule>) -> Value {
fn parse_const_value(pair: &Pair<Rule>) -> Value {
match pair.as_rule() {
Rule::number => {
if let Ok(f) = pair.as_str().parse::<i32>() {
@ -268,7 +229,3 @@ fn handle_rules(mut p: pest::iterators::Pairs<Rule>) -> (Statement, FuncHolder)
_ => 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> {
None
}
fn to_bool(&self) -> bool {
true
}
}
impl PartialEq<Value> for dyn CustomValue {

View File

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