clippy suggestions
This commit is contained in:
@@ -30,7 +30,7 @@ pub fn compile(program: String) -> Result<(), Box<dyn Error>> {
|
|||||||
.first_element_child()
|
.first_element_child()
|
||||||
.ok_or(InvalidProgram)?
|
.ok_or(InvalidProgram)?
|
||||||
.children()
|
.children()
|
||||||
.find(|node| util::tag_name(&node) == "main")
|
.find(|node| util::tag_name(node) == "main")
|
||||||
.ok_or(MissingChild("program", "main"))?;
|
.ok_or(MissingChild("program", "main"))?;
|
||||||
let main_ast = Instruction::from_children(main)?;
|
let main_ast = Instruction::from_children(main)?;
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ impl Instruction {
|
|||||||
Ok(match util::tag_name(&node).as_str() {
|
Ok(match util::tag_name(&node).as_str() {
|
||||||
"value" => Instruction::Value(
|
"value" => Instruction::Value(
|
||||||
node.attribute("variable")
|
node.attribute("variable")
|
||||||
.and_then(|a| Some(String::from(a)))
|
.map(String::from)
|
||||||
.ok_or(MissingAttribute("value", "variable"))?,
|
.ok_or(MissingAttribute("value", "variable"))?,
|
||||||
),
|
),
|
||||||
"assign" => Instruction::Assign(
|
"assign" => Instruction::Assign(
|
||||||
|
|||||||
@@ -234,7 +234,7 @@ impl RunUtil for Instruction {
|
|||||||
pub trait Run {
|
pub trait Run {
|
||||||
fn run(&self, ctx: &mut Context, globals: &Context) -> Result<Option<Value>, Box<dyn Error>>;
|
fn run(&self, ctx: &mut Context, globals: &Context) -> Result<Option<Value>, Box<dyn Error>>;
|
||||||
fn run_all(
|
fn run_all(
|
||||||
ins: &Vec<Instruction>,
|
ins: &[Instruction],
|
||||||
ctx: &mut Context,
|
ctx: &mut Context,
|
||||||
globals: &Context,
|
globals: &Context,
|
||||||
) -> Result<Option<Vec<Value>>, Box<dyn Error>>;
|
) -> Result<Option<Vec<Value>>, Box<dyn Error>>;
|
||||||
@@ -242,7 +242,7 @@ pub trait Run {
|
|||||||
|
|
||||||
impl Run for Instruction {
|
impl Run for Instruction {
|
||||||
fn run_all(
|
fn run_all(
|
||||||
ins: &Vec<Instruction>,
|
ins: &[Instruction],
|
||||||
ctx: &mut Context,
|
ctx: &mut Context,
|
||||||
globals: &Context,
|
globals: &Context,
|
||||||
) -> Result<Option<Vec<Value>>, Box<dyn Error>> {
|
) -> Result<Option<Vec<Value>>, Box<dyn Error>> {
|
||||||
@@ -250,11 +250,11 @@ impl Run for Instruction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, ctx: &mut Context, globals: &Context) -> Result<Option<Value>, Box<dyn Error>> {
|
fn run(&self, ctx: &mut Context, globals: &Context) -> Result<Option<Value>, Box<dyn Error>> {
|
||||||
Ok(if let None = ctx.value(&String::from("__return")) {
|
Ok(if ctx.value(&String::from("__return")).is_none() {
|
||||||
match self {
|
match self {
|
||||||
Instruction::Value(key) => {
|
Instruction::Value(key) => {
|
||||||
Some(match ctx.value(key).ok_or(UnknownVariable(key.clone()))? {
|
Some(match ctx.value(key).ok_or(UnknownVariable(key.clone()))? {
|
||||||
Value::Array(vecrc) => Value::Array(Rc::clone(&vecrc)),
|
Value::Array(vecrc) => Value::Array(Rc::clone(vecrc)),
|
||||||
val => val.clone(),
|
val => val.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -388,7 +388,7 @@ impl Run for Instruction {
|
|||||||
let vals: Vec<Value> =
|
let vals: Vec<Value> =
|
||||||
Instruction::run_all(args, ctx, globals)?.ok_or(InvalidValue("call"))?;
|
Instruction::run_all(args, ctx, globals)?.ok_or(InvalidValue("call"))?;
|
||||||
let fct_val = ctx
|
let fct_val = ctx
|
||||||
.value(&fct_name)
|
.value(fct_name)
|
||||||
.ok_or(UnknownVariable(fct_name.clone()))?;
|
.ok_or(UnknownVariable(fct_name.clone()))?;
|
||||||
if let Value::Function(f) = fct_val {
|
if let Value::Function(f) = fct_val {
|
||||||
let mut local = ctx.clone();
|
let mut local = ctx.clone();
|
||||||
@@ -511,7 +511,7 @@ pub fn run(program: String) -> Result<(), Box<dyn Error>> {
|
|||||||
.first_element_child()
|
.first_element_child()
|
||||||
.ok_or(InvalidProgram)?
|
.ok_or(InvalidProgram)?
|
||||||
.children()
|
.children()
|
||||||
.find(|node| util::tag_name(&node) == "main")
|
.find(|node| util::tag_name(node) == "main")
|
||||||
.ok_or(MissingChild("program", "main"))?;
|
.ok_or(MissingChild("program", "main"))?;
|
||||||
let main_ast = Instruction::from_children(main)?;
|
let main_ast = Instruction::from_children(main)?;
|
||||||
|
|
||||||
@@ -519,7 +519,7 @@ pub fn run(program: String) -> Result<(), Box<dyn Error>> {
|
|||||||
.first_element_child()
|
.first_element_child()
|
||||||
.ok_or(InvalidProgram)?
|
.ok_or(InvalidProgram)?
|
||||||
.children()
|
.children()
|
||||||
.filter(|node| node.tag_name().name() == String::from("function"));
|
.filter(|node| node.tag_name().name() == "function");
|
||||||
|
|
||||||
for fun in functions {
|
for fun in functions {
|
||||||
ctx.assign(
|
ctx.assign(
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ fn print_line(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn input(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
fn input(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
||||||
if vals.len() == 0 {
|
if vals.is_empty() {
|
||||||
let mut line = String::new();
|
let mut line = String::new();
|
||||||
stdin().read_line(&mut line)?;
|
stdin().read_line(&mut line)?;
|
||||||
line.pop();
|
line.pop();
|
||||||
@@ -181,7 +181,7 @@ fn array_length(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
|||||||
fn to_ascii(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
fn to_ascii(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
||||||
if vals.len() == 1 {
|
if vals.len() == 1 {
|
||||||
if let Value::Integer(i) = &vals[0] {
|
if let Value::Integer(i) = &vals[0] {
|
||||||
if &0 <= i && i <= &255 {
|
if (0..=255).contains(i) {
|
||||||
Ok(Some(Value::String(String::from_utf8(vec![*i as u8])?)))
|
Ok(Some(Value::String(String::from_utf8(vec![*i as u8])?)))
|
||||||
} else {
|
} else {
|
||||||
Err(InvalidArgument("to-ascii", "integer").into())
|
Err(InvalidArgument("to-ascii", "integer").into())
|
||||||
@@ -211,12 +211,9 @@ fn from_ascii(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_args(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
fn get_args(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
||||||
if vals.len() == 0 {
|
if vals.is_empty() {
|
||||||
Ok(Some(Value::Array(Rc::new(RefCell::new(
|
Ok(Some(Value::Array(Rc::new(RefCell::new(
|
||||||
std::env::args()
|
std::env::args().skip(1).map(Value::String).collect(),
|
||||||
.skip(1)
|
|
||||||
.map(|arg| Value::String(arg))
|
|
||||||
.collect(),
|
|
||||||
)))))
|
)))))
|
||||||
} else {
|
} else {
|
||||||
Err(BadArgumentCount("get-args", vals.len(), 0).into())
|
Err(BadArgumentCount("get-args", vals.len(), 0).into())
|
||||||
@@ -233,7 +230,7 @@ fn write_file(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
|||||||
.append(Value::to_bool(&vals[2]))
|
.append(Value::to_bool(&vals[2]))
|
||||||
.open(path)
|
.open(path)
|
||||||
{
|
{
|
||||||
if let Ok(_) = write!(file, "{}", contents) {
|
if write!(file, "{}", contents).is_ok() {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
} else {
|
} else {
|
||||||
Err(InaccessibleFile(path.clone()).into())
|
Err(InaccessibleFile(path.clone()).into())
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ impl Function {
|
|||||||
}
|
}
|
||||||
self.args
|
self.args
|
||||||
.iter()
|
.iter()
|
||||||
.zip(args.into_iter())
|
.zip(args)
|
||||||
.for_each(|(p, a)| ctx.assign(p.clone(), a));
|
.for_each(|(p, a)| ctx.assign(p.clone(), a));
|
||||||
for i in self.ins.iter() {
|
for i in self.ins.iter() {
|
||||||
i.run(ctx, globals)?;
|
i.run(ctx, globals)?;
|
||||||
@@ -37,20 +37,22 @@ impl Function {
|
|||||||
|
|
||||||
pub fn from(fun: &Node<'_, '_>) -> Result<Function, Box<dyn Error>> {
|
pub fn from(fun: &Node<'_, '_>) -> Result<Function, Box<dyn Error>> {
|
||||||
Ok(Function {
|
Ok(Function {
|
||||||
args: util::find_node(&fun, "arguments")
|
args: util::find_node(fun, "arguments")
|
||||||
.ok_or(MissingChild("call", "arguments"))?
|
.ok_or(MissingChild("call", "arguments"))?
|
||||||
.children()
|
.children()
|
||||||
.filter(Node::is_element)
|
.filter(Node::is_element)
|
||||||
.map(|n| n.attribute("name").and_then(|s| Some(String::from(s))))
|
.map(|n| n.attribute("name").map(String::from))
|
||||||
.collect::<Option<Vec<String>>>()
|
.collect::<Option<Vec<String>>>()
|
||||||
.ok_or(Unnamed("argument"))?,
|
.ok_or(Unnamed("argument"))?,
|
||||||
ins: Instruction::from_children(
|
ins: Instruction::from_children(
|
||||||
util::find_node(&fun, "body").ok_or(MissingChild("call", "body"))?,
|
util::find_node(fun, "body").ok_or(MissingChild("call", "body"))?,
|
||||||
)?,
|
)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StdFn = fn(Vec<Value>) -> Result<Option<Value>, Box<dyn Error>>;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
Integer(i64),
|
Integer(i64),
|
||||||
@@ -58,7 +60,7 @@ pub enum Value {
|
|||||||
String(String),
|
String(String),
|
||||||
Array(Rc<RefCell<Vec<Value>>>),
|
Array(Rc<RefCell<Vec<Value>>>),
|
||||||
Function(Function),
|
Function(Function),
|
||||||
StdFunction(fn(Vec<Value>) -> Result<Option<Value>, Box<dyn Error>>),
|
StdFunction(StdFn),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
@@ -66,7 +68,7 @@ impl Value {
|
|||||||
match self {
|
match self {
|
||||||
Value::Integer(i) => *i != 0,
|
Value::Integer(i) => *i != 0,
|
||||||
Value::Real(f) => *f != 0.0,
|
Value::Real(f) => *f != 0.0,
|
||||||
Value::String(s) => s.len() != 0,
|
Value::String(s) => !s.is_empty(),
|
||||||
Value::Array(v) => v.borrow().len() != 0,
|
Value::Array(v) => v.borrow().len() != 0,
|
||||||
_ => true,
|
_ => true,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user