From ed233c17dea504a0c7dce447531e85cf0f3fb189 Mon Sep 17 00:00:00 2001 From: Altareos <8584797+Altareos@users.noreply.github.com> Date: Sun, 5 Jun 2022 01:19:54 +0200 Subject: [PATCH] float is boring, let's call it real --- doc/syntax.md | 8 +++--- sample/stl.pl.xml | 4 +-- src/instruction.rs | 64 +++++++++++++++++++++++----------------------- src/stl.rs | 4 +-- src/value.rs | 4 +-- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/doc/syntax.md b/doc/syntax.md index 5d6acbb..595a110 100644 --- a/doc/syntax.md +++ b/doc/syntax.md @@ -71,7 +71,7 @@ _instruction value assign integer - float + real string array add @@ -102,9 +102,9 @@ integer "" "" instruction "" -float - "" - "" instruction "" +real + "" + "" instruction "" string "" diff --git a/sample/stl.pl.xml b/sample/stl.pl.xml index 4bee69a..8ba8597 100644 --- a/sample/stl.pl.xml +++ b/sample/stl.pl.xml @@ -7,14 +7,14 @@ - + - + diff --git a/src/instruction.rs b/src/instruction.rs index 43fe269..f170654 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -14,8 +14,8 @@ pub enum Instruction { Assign(String, Box), Integer(String), IntegerCast(Box), - Float(String), - FloatCast(Box), + Real(String), + RealCast(Box), String(String), StringCast(Box), Array(Vec), @@ -73,13 +73,13 @@ impl Instruction { Err(MissingAttribute("integer", "value"))? } } - "float" => { + "real" => { if let Some(v) = node.attribute("value") { - Instruction::Float(String::from(v)) + Instruction::Real(String::from(v)) } else if let Some(n) = node.first_element_child() { - Instruction::FloatCast(Box::new(Instruction::new(n)?)) + Instruction::RealCast(Box::new(Instruction::new(n)?)) } else { - Err(MissingAttribute("float", "value"))? + Err(MissingAttribute("real", "value"))? } } "string" => { @@ -270,14 +270,14 @@ impl Instruction { )) } else if vals .iter() - .all(|v| matches!(v, Value::Integer(_)) || matches!(v, Value::Float(_))) + .all(|v| matches!(v, Value::Integer(_)) || matches!(v, Value::Real(_))) { - Ok(Value::Float( + Ok(Value::Real( vals.iter() .map(|v| { if let Value::Integer(i) = v { Ok(*i as f64) - } else if let Value::Float(f) = v { + } else if let Value::Real(f) = v { Ok(*f) } else { Err(InvalidValue("add"))? @@ -287,7 +287,7 @@ impl Instruction { )) } else if vals.iter().all(|v| { matches!(v, Value::Integer(_)) - || matches!(v, Value::Float(_)) + || matches!(v, Value::Real(_)) || matches!(v, Value::String(_)) }) { Ok(Value::String( @@ -297,7 +297,7 @@ impl Instruction { s.to_string() } else if let Value::Integer(i) = v { i.to_string() - } else if let Value::Float(f) = v { + } else if let Value::Real(f) = v { f.to_string() } else { Err(InvalidValue("add"))? @@ -333,14 +333,14 @@ impl Instruction { ) } else if vals .iter() - .all(|v| matches!(v, Value::Integer(_)) || matches!(v, Value::Float(_))) + .all(|v| matches!(v, Value::Integer(_)) || matches!(v, Value::Real(_))) { let first = match vals.first().ok_or(BadChildCount("subtract", 0usize))? { Value::Integer(v) => *v as f64, - Value::Float(v) => *v, + Value::Real(v) => *v, _ => Err(InvalidValue("subtract"))?, }; - Value::Float( + Value::Real( first - vals .iter() @@ -348,7 +348,7 @@ impl Instruction { .map(|val| { Ok(match val { Value::Integer(v) => *v as f64, - Value::Float(v) => *v, + Value::Real(v) => *v, _ => Err(InvalidValue("subtract"))?, }) }) @@ -374,14 +374,14 @@ impl Instruction { )) } else if vals .iter() - .all(|v| matches!(v, Value::Integer(_)) || matches!(v, Value::Float(_))) + .all(|v| matches!(v, Value::Integer(_)) || matches!(v, Value::Real(_))) { - Ok(Value::Float( + Ok(Value::Real( vals.iter() .map(|val| { Ok(match val { Value::Integer(v) => *v as f64, - Value::Float(v) => *v, + Value::Real(v) => *v, _ => Err(InvalidValue("multiply"))?, }) }) @@ -395,14 +395,14 @@ impl Instruction { fn divide(vals: Vec) -> Result> { if vals .iter() - .all(|v| matches!(v, Value::Integer(_)) || matches!(v, Value::Float(_))) + .all(|v| matches!(v, Value::Integer(_)) || matches!(v, Value::Real(_))) { let first = match vals.first().ok_or(BadChildCount("divide", 0))? { Value::Integer(v) => *v as f64, - Value::Float(v) => *v, + Value::Real(v) => *v, _ => Err(InvalidValue("divide"))?, }; - Ok(Value::Float( + Ok(Value::Real( first * vals .iter() @@ -410,7 +410,7 @@ impl Instruction { .map(|val| { Ok(match val { Value::Integer(v) => 1.0 / (*v as f64), - Value::Float(v) => 1.0 / *v, + Value::Real(v) => 1.0 / *v, _ => Err(InvalidValue("divide"))?, }) }) @@ -442,7 +442,7 @@ impl Instruction { match v1 { Value::Integer(i1) => match v2 { Value::Integer(i2) => Ok(i1 - i2), - Value::Float(f2) => Ok( + Value::Real(f2) => Ok( match (i1 as f64).partial_cmp(&f2).ok_or(IncompatibleValues)? { Ordering::Less => -1, Ordering::Equal => 0, @@ -451,7 +451,7 @@ impl Instruction { ), _ => Err(IncompatibleValues)?, }, - Value::Float(f1) => match v2 { + Value::Real(f1) => match v2 { Value::Integer(i2) => Ok( match f1.partial_cmp(&(i2 as f64)).ok_or(IncompatibleValues)? { Ordering::Less => -1, @@ -459,7 +459,7 @@ impl Instruction { Ordering::Greater => 1, }, ), - Value::Float(f2) => Ok(match f1.partial_cmp(&f2).ok_or(IncompatibleValues)? { + Value::Real(f2) => Ok(match f1.partial_cmp(&f2).ok_or(IncompatibleValues)? { Ordering::Less => -1, Ordering::Equal => 0, Ordering::Greater => 1, @@ -511,25 +511,25 @@ impl Instruction { Instruction::IntegerCast(ins) => Some(Value::Integer( match ins.run(ctx, globals)?.ok_or(InvalidValue("integer"))? { Value::Integer(i) => i, - Value::Float(f) => f as i64, + Value::Real(f) => f as i64, Value::String(s) => s.parse()?, _ => Err(InvalidValue("integer"))?, }, )), - Instruction::Float(val) => Some(Value::Float(val.parse()?)), - Instruction::FloatCast(ins) => Some(Value::Float( - match ins.run(ctx, globals)?.ok_or(InvalidValue("float"))? { + Instruction::Real(val) => Some(Value::Real(val.parse()?)), + Instruction::RealCast(ins) => Some(Value::Real( + match ins.run(ctx, globals)?.ok_or(InvalidValue("real"))? { Value::Integer(i) => i as f64, - Value::Float(f) => f, + Value::Real(f) => f, Value::String(s) => s.parse()?, - _ => Err(InvalidValue("float"))?, + _ => Err(InvalidValue("real"))?, }, )), Instruction::String(val) => Some(Value::String(val.clone())), Instruction::StringCast(ins) => Some(Value::String( match ins.run(ctx, globals)?.ok_or(InvalidValue("string"))? { Value::Integer(i) => i.to_string(), - Value::Float(f) => f.to_string(), + Value::Real(f) => f.to_string(), Value::String(s) => s, _ => Err(InvalidValue("string"))?, }, diff --git a/src/stl.rs b/src/stl.rs index 8dec93b..a49faef 100644 --- a/src/stl.rs +++ b/src/stl.rs @@ -35,7 +35,7 @@ fn print(vals: Vec) -> Result, Box> { if vals.len() == 1 { match &vals[0] { Value::Integer(i) => print!("{}", i), - Value::Float(f) => print!("{}", f), + Value::Real(f) => print!("{}", f), Value::String(s) => print!("{}", s), v => print!("{:?}", v), // _ => Err("unprintable value")?, }; @@ -50,7 +50,7 @@ fn print_line(vals: Vec) -> Result, Box> { if vals.len() == 1 { match &vals[0] { Value::Integer(i) => println!("{}", i), - Value::Float(f) => println!("{}", f), + Value::Real(f) => println!("{}", f), Value::String(s) => println!("{}", s), v => println!("{:?}", v), // _ => Err("unprintable value")?, }; diff --git a/src/value.rs b/src/value.rs index de6f563..2f477e1 100644 --- a/src/value.rs +++ b/src/value.rs @@ -50,7 +50,7 @@ impl Function { #[derive(Clone, Debug)] pub enum Value { Integer(i64), - Float(f64), + Real(f64), String(String), Array(Rc>>), Function(Function), @@ -61,7 +61,7 @@ impl Value { pub fn to_bool(&self) -> bool { match self { Value::Integer(i) => *i != 0, - Value::Float(f) => *f != 0.0, + Value::Real(f) => *f != 0.0, Value::String(s) => s.len() != 0, Value::Array(v) => v.borrow().len() != 0, _ => true,