mirror of
https://github.com/rm-dr/daisy
synced 2025-08-24 04:05:29 -07:00
Added tuples
This commit is contained in:
@ -43,7 +43,7 @@ pub fn evaluate(t: &Expression, context: &mut Context, allow_incomplete: bool) -
|
||||
|
||||
let new = match g {
|
||||
Expression::Quantity(_, _) => None,
|
||||
|
||||
Expression::Tuple(_, _) => None,
|
||||
Expression::Constant(_, c) => { Some(evaluate(&c.value(), context, false).unwrap()) },
|
||||
Expression::Variable(l, s) => {
|
||||
// Don't move up, re-evaluate
|
||||
|
@ -32,6 +32,15 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, EvalEr
|
||||
|
||||
if args.len() != 1 {panic!()};
|
||||
let a = &args[0];
|
||||
|
||||
// All the functions below take only one argument
|
||||
if let Expression::Tuple(l, v) = a {
|
||||
return Err((
|
||||
*l + *loc,
|
||||
EvalError::BadArguments(f.to_string(), 1, v.len())
|
||||
))
|
||||
};
|
||||
|
||||
let Expression::Quantity(l, q) = a else {panic!()};
|
||||
|
||||
|
||||
|
@ -13,6 +13,7 @@ pub enum EvalError {
|
||||
IncompatibleUnits(String, String),
|
||||
Undefined(String),
|
||||
EvaluationError,
|
||||
BadArguments(String, usize, usize)
|
||||
}
|
||||
|
||||
|
||||
@ -39,6 +40,11 @@ impl ToString for EvalError {
|
||||
},
|
||||
EvalError::EvaluationError => {
|
||||
String::from("Could not evaluate")
|
||||
},
|
||||
EvalError::BadArguments(s, want, got) => {
|
||||
format!("{s} takes {want} argument{}, got {got}",
|
||||
if *want == 1 {""} else {"s"},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use std::collections::VecDeque;
|
||||
use crate::parser::LineLocation;
|
||||
use crate::quantity::Quantity;
|
||||
use crate::parser::Operator;
|
||||
@ -12,6 +13,31 @@ pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Ex
|
||||
|
||||
match op {
|
||||
Operator::Function(_) => unreachable!("Functions are handled seperately."),
|
||||
Operator::Tuple => {
|
||||
if args.len() != 2 { panic!() };
|
||||
let a = &args[0];
|
||||
let b = &args[1];
|
||||
|
||||
let mut loc = *op_loc;
|
||||
let mut vec: VecDeque<Expression> = VecDeque::new();
|
||||
if let Expression::Tuple(l, v) = a {
|
||||
loc += *l;
|
||||
for i in v { vec.push_back(i.clone()) }
|
||||
} else {
|
||||
loc += a.get_linelocation();
|
||||
vec.push_back(a.clone())
|
||||
}
|
||||
|
||||
if let Expression::Tuple(l, v) = b {
|
||||
loc += *l;
|
||||
for i in v { vec.push_back(i.clone()) }
|
||||
} else {
|
||||
loc += b.get_linelocation();
|
||||
vec.push_back(b.clone())
|
||||
}
|
||||
|
||||
return Ok(Some(Expression::Tuple(loc, vec)))
|
||||
}
|
||||
|
||||
Operator::Negative => {
|
||||
if args.len() != 1 { panic!() };
|
||||
|
Reference in New Issue
Block a user