float is boring, let's call it real
This commit is contained in:
@@ -71,7 +71,7 @@ _instruction
|
|||||||
value
|
value
|
||||||
assign
|
assign
|
||||||
integer
|
integer
|
||||||
float
|
real
|
||||||
string
|
string
|
||||||
array
|
array
|
||||||
add
|
add
|
||||||
@@ -102,9 +102,9 @@ integer
|
|||||||
"<integer value=" tag "/>"
|
"<integer value=" tag "/>"
|
||||||
"<integer>" instruction "</integer>"
|
"<integer>" instruction "</integer>"
|
||||||
|
|
||||||
float
|
real
|
||||||
"<float value=" tag "/>"
|
"<real value=" tag "/>"
|
||||||
"<float>" instruction "</float>"
|
"<real>" instruction "</real>"
|
||||||
|
|
||||||
string
|
string
|
||||||
"<string value=" tag "/>"
|
"<string value=" tag "/>"
|
||||||
|
|||||||
@@ -7,14 +7,14 @@
|
|||||||
</call>
|
</call>
|
||||||
<assign variable="arr">
|
<assign variable="arr">
|
||||||
<array>
|
<array>
|
||||||
<float value="0.1" />
|
<real value="0.1" />
|
||||||
</array>
|
</array>
|
||||||
</assign>
|
</assign>
|
||||||
<call function="array-set">
|
<call function="array-set">
|
||||||
<arguments>
|
<arguments>
|
||||||
<value variable="arr" />
|
<value variable="arr" />
|
||||||
<integer value="0" />
|
<integer value="0" />
|
||||||
<float value="0.5" />
|
<real value="0.5" />
|
||||||
</arguments>
|
</arguments>
|
||||||
</call>
|
</call>
|
||||||
<each variable="v">
|
<each variable="v">
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ pub enum Instruction {
|
|||||||
Assign(String, Box<Instruction>),
|
Assign(String, Box<Instruction>),
|
||||||
Integer(String),
|
Integer(String),
|
||||||
IntegerCast(Box<Instruction>),
|
IntegerCast(Box<Instruction>),
|
||||||
Float(String),
|
Real(String),
|
||||||
FloatCast(Box<Instruction>),
|
RealCast(Box<Instruction>),
|
||||||
String(String),
|
String(String),
|
||||||
StringCast(Box<Instruction>),
|
StringCast(Box<Instruction>),
|
||||||
Array(Vec<Instruction>),
|
Array(Vec<Instruction>),
|
||||||
@@ -73,13 +73,13 @@ impl Instruction {
|
|||||||
Err(MissingAttribute("integer", "value"))?
|
Err(MissingAttribute("integer", "value"))?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"float" => {
|
"real" => {
|
||||||
if let Some(v) = node.attribute("value") {
|
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() {
|
} else if let Some(n) = node.first_element_child() {
|
||||||
Instruction::FloatCast(Box::new(Instruction::new(n)?))
|
Instruction::RealCast(Box::new(Instruction::new(n)?))
|
||||||
} else {
|
} else {
|
||||||
Err(MissingAttribute("float", "value"))?
|
Err(MissingAttribute("real", "value"))?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"string" => {
|
"string" => {
|
||||||
@@ -270,14 +270,14 @@ impl Instruction {
|
|||||||
))
|
))
|
||||||
} else if vals
|
} else if vals
|
||||||
.iter()
|
.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()
|
vals.iter()
|
||||||
.map(|v| {
|
.map(|v| {
|
||||||
if let Value::Integer(i) = v {
|
if let Value::Integer(i) = v {
|
||||||
Ok(*i as f64)
|
Ok(*i as f64)
|
||||||
} else if let Value::Float(f) = v {
|
} else if let Value::Real(f) = v {
|
||||||
Ok(*f)
|
Ok(*f)
|
||||||
} else {
|
} else {
|
||||||
Err(InvalidValue("add"))?
|
Err(InvalidValue("add"))?
|
||||||
@@ -287,7 +287,7 @@ impl Instruction {
|
|||||||
))
|
))
|
||||||
} else if vals.iter().all(|v| {
|
} else if vals.iter().all(|v| {
|
||||||
matches!(v, Value::Integer(_))
|
matches!(v, Value::Integer(_))
|
||||||
|| matches!(v, Value::Float(_))
|
|| matches!(v, Value::Real(_))
|
||||||
|| matches!(v, Value::String(_))
|
|| matches!(v, Value::String(_))
|
||||||
}) {
|
}) {
|
||||||
Ok(Value::String(
|
Ok(Value::String(
|
||||||
@@ -297,7 +297,7 @@ impl Instruction {
|
|||||||
s.to_string()
|
s.to_string()
|
||||||
} else if let Value::Integer(i) = v {
|
} else if let Value::Integer(i) = v {
|
||||||
i.to_string()
|
i.to_string()
|
||||||
} else if let Value::Float(f) = v {
|
} else if let Value::Real(f) = v {
|
||||||
f.to_string()
|
f.to_string()
|
||||||
} else {
|
} else {
|
||||||
Err(InvalidValue("add"))?
|
Err(InvalidValue("add"))?
|
||||||
@@ -333,14 +333,14 @@ impl Instruction {
|
|||||||
)
|
)
|
||||||
} else if vals
|
} else if vals
|
||||||
.iter()
|
.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))? {
|
let first = match vals.first().ok_or(BadChildCount("subtract", 0usize))? {
|
||||||
Value::Integer(v) => *v as f64,
|
Value::Integer(v) => *v as f64,
|
||||||
Value::Float(v) => *v,
|
Value::Real(v) => *v,
|
||||||
_ => Err(InvalidValue("subtract"))?,
|
_ => Err(InvalidValue("subtract"))?,
|
||||||
};
|
};
|
||||||
Value::Float(
|
Value::Real(
|
||||||
first
|
first
|
||||||
- vals
|
- vals
|
||||||
.iter()
|
.iter()
|
||||||
@@ -348,7 +348,7 @@ impl Instruction {
|
|||||||
.map(|val| {
|
.map(|val| {
|
||||||
Ok(match val {
|
Ok(match val {
|
||||||
Value::Integer(v) => *v as f64,
|
Value::Integer(v) => *v as f64,
|
||||||
Value::Float(v) => *v,
|
Value::Real(v) => *v,
|
||||||
_ => Err(InvalidValue("subtract"))?,
|
_ => Err(InvalidValue("subtract"))?,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -374,14 +374,14 @@ impl Instruction {
|
|||||||
))
|
))
|
||||||
} else if vals
|
} else if vals
|
||||||
.iter()
|
.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()
|
vals.iter()
|
||||||
.map(|val| {
|
.map(|val| {
|
||||||
Ok(match val {
|
Ok(match val {
|
||||||
Value::Integer(v) => *v as f64,
|
Value::Integer(v) => *v as f64,
|
||||||
Value::Float(v) => *v,
|
Value::Real(v) => *v,
|
||||||
_ => Err(InvalidValue("multiply"))?,
|
_ => Err(InvalidValue("multiply"))?,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -395,14 +395,14 @@ impl Instruction {
|
|||||||
fn divide(vals: Vec<Value>) -> Result<Value, Box<dyn Error>> {
|
fn divide(vals: Vec<Value>) -> Result<Value, Box<dyn Error>> {
|
||||||
if vals
|
if vals
|
||||||
.iter()
|
.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))? {
|
let first = match vals.first().ok_or(BadChildCount("divide", 0))? {
|
||||||
Value::Integer(v) => *v as f64,
|
Value::Integer(v) => *v as f64,
|
||||||
Value::Float(v) => *v,
|
Value::Real(v) => *v,
|
||||||
_ => Err(InvalidValue("divide"))?,
|
_ => Err(InvalidValue("divide"))?,
|
||||||
};
|
};
|
||||||
Ok(Value::Float(
|
Ok(Value::Real(
|
||||||
first
|
first
|
||||||
* vals
|
* vals
|
||||||
.iter()
|
.iter()
|
||||||
@@ -410,7 +410,7 @@ impl Instruction {
|
|||||||
.map(|val| {
|
.map(|val| {
|
||||||
Ok(match val {
|
Ok(match val {
|
||||||
Value::Integer(v) => 1.0 / (*v as f64),
|
Value::Integer(v) => 1.0 / (*v as f64),
|
||||||
Value::Float(v) => 1.0 / *v,
|
Value::Real(v) => 1.0 / *v,
|
||||||
_ => Err(InvalidValue("divide"))?,
|
_ => Err(InvalidValue("divide"))?,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -442,7 +442,7 @@ impl Instruction {
|
|||||||
match v1 {
|
match v1 {
|
||||||
Value::Integer(i1) => match v2 {
|
Value::Integer(i1) => match v2 {
|
||||||
Value::Integer(i2) => Ok(i1 - i2),
|
Value::Integer(i2) => Ok(i1 - i2),
|
||||||
Value::Float(f2) => Ok(
|
Value::Real(f2) => Ok(
|
||||||
match (i1 as f64).partial_cmp(&f2).ok_or(IncompatibleValues)? {
|
match (i1 as f64).partial_cmp(&f2).ok_or(IncompatibleValues)? {
|
||||||
Ordering::Less => -1,
|
Ordering::Less => -1,
|
||||||
Ordering::Equal => 0,
|
Ordering::Equal => 0,
|
||||||
@@ -451,7 +451,7 @@ impl Instruction {
|
|||||||
),
|
),
|
||||||
_ => Err(IncompatibleValues)?,
|
_ => Err(IncompatibleValues)?,
|
||||||
},
|
},
|
||||||
Value::Float(f1) => match v2 {
|
Value::Real(f1) => match v2 {
|
||||||
Value::Integer(i2) => Ok(
|
Value::Integer(i2) => Ok(
|
||||||
match f1.partial_cmp(&(i2 as f64)).ok_or(IncompatibleValues)? {
|
match f1.partial_cmp(&(i2 as f64)).ok_or(IncompatibleValues)? {
|
||||||
Ordering::Less => -1,
|
Ordering::Less => -1,
|
||||||
@@ -459,7 +459,7 @@ impl Instruction {
|
|||||||
Ordering::Greater => 1,
|
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::Less => -1,
|
||||||
Ordering::Equal => 0,
|
Ordering::Equal => 0,
|
||||||
Ordering::Greater => 1,
|
Ordering::Greater => 1,
|
||||||
@@ -511,25 +511,25 @@ impl Instruction {
|
|||||||
Instruction::IntegerCast(ins) => Some(Value::Integer(
|
Instruction::IntegerCast(ins) => Some(Value::Integer(
|
||||||
match ins.run(ctx, globals)?.ok_or(InvalidValue("integer"))? {
|
match ins.run(ctx, globals)?.ok_or(InvalidValue("integer"))? {
|
||||||
Value::Integer(i) => i,
|
Value::Integer(i) => i,
|
||||||
Value::Float(f) => f as i64,
|
Value::Real(f) => f as i64,
|
||||||
Value::String(s) => s.parse()?,
|
Value::String(s) => s.parse()?,
|
||||||
_ => Err(InvalidValue("integer"))?,
|
_ => Err(InvalidValue("integer"))?,
|
||||||
},
|
},
|
||||||
)),
|
)),
|
||||||
Instruction::Float(val) => Some(Value::Float(val.parse()?)),
|
Instruction::Real(val) => Some(Value::Real(val.parse()?)),
|
||||||
Instruction::FloatCast(ins) => Some(Value::Float(
|
Instruction::RealCast(ins) => Some(Value::Real(
|
||||||
match ins.run(ctx, globals)?.ok_or(InvalidValue("float"))? {
|
match ins.run(ctx, globals)?.ok_or(InvalidValue("real"))? {
|
||||||
Value::Integer(i) => i as f64,
|
Value::Integer(i) => i as f64,
|
||||||
Value::Float(f) => f,
|
Value::Real(f) => f,
|
||||||
Value::String(s) => s.parse()?,
|
Value::String(s) => s.parse()?,
|
||||||
_ => Err(InvalidValue("float"))?,
|
_ => Err(InvalidValue("real"))?,
|
||||||
},
|
},
|
||||||
)),
|
)),
|
||||||
Instruction::String(val) => Some(Value::String(val.clone())),
|
Instruction::String(val) => Some(Value::String(val.clone())),
|
||||||
Instruction::StringCast(ins) => Some(Value::String(
|
Instruction::StringCast(ins) => Some(Value::String(
|
||||||
match ins.run(ctx, globals)?.ok_or(InvalidValue("string"))? {
|
match ins.run(ctx, globals)?.ok_or(InvalidValue("string"))? {
|
||||||
Value::Integer(i) => i.to_string(),
|
Value::Integer(i) => i.to_string(),
|
||||||
Value::Float(f) => f.to_string(),
|
Value::Real(f) => f.to_string(),
|
||||||
Value::String(s) => s,
|
Value::String(s) => s,
|
||||||
_ => Err(InvalidValue("string"))?,
|
_ => Err(InvalidValue("string"))?,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ fn print(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
|||||||
if vals.len() == 1 {
|
if vals.len() == 1 {
|
||||||
match &vals[0] {
|
match &vals[0] {
|
||||||
Value::Integer(i) => print!("{}", i),
|
Value::Integer(i) => print!("{}", i),
|
||||||
Value::Float(f) => print!("{}", f),
|
Value::Real(f) => print!("{}", f),
|
||||||
Value::String(s) => print!("{}", s),
|
Value::String(s) => print!("{}", s),
|
||||||
v => print!("{:?}", v), // _ => Err("unprintable value")?,
|
v => print!("{:?}", v), // _ => Err("unprintable value")?,
|
||||||
};
|
};
|
||||||
@@ -50,7 +50,7 @@ fn print_line(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
|||||||
if vals.len() == 1 {
|
if vals.len() == 1 {
|
||||||
match &vals[0] {
|
match &vals[0] {
|
||||||
Value::Integer(i) => println!("{}", i),
|
Value::Integer(i) => println!("{}", i),
|
||||||
Value::Float(f) => println!("{}", f),
|
Value::Real(f) => println!("{}", f),
|
||||||
Value::String(s) => println!("{}", s),
|
Value::String(s) => println!("{}", s),
|
||||||
v => println!("{:?}", v), // _ => Err("unprintable value")?,
|
v => println!("{:?}", v), // _ => Err("unprintable value")?,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ impl Function {
|
|||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
Integer(i64),
|
Integer(i64),
|
||||||
Float(f64),
|
Real(f64),
|
||||||
String(String),
|
String(String),
|
||||||
Array(Rc<RefCell<Vec<Value>>>),
|
Array(Rc<RefCell<Vec<Value>>>),
|
||||||
Function(Function),
|
Function(Function),
|
||||||
@@ -61,7 +61,7 @@ impl Value {
|
|||||||
pub fn to_bool(&self) -> bool {
|
pub fn to_bool(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Value::Integer(i) => *i != 0,
|
Value::Integer(i) => *i != 0,
|
||||||
Value::Float(f) => *f != 0.0,
|
Value::Real(f) => *f != 0.0,
|
||||||
Value::String(s) => s.len() != 0,
|
Value::String(s) => s.len() != 0,
|
||||||
Value::Array(v) => v.borrow().len() != 0,
|
Value::Array(v) => v.borrow().len() != 0,
|
||||||
_ => true,
|
_ => true,
|
||||||
|
|||||||
Reference in New Issue
Block a user