Added tuples

This commit is contained in:
2023-08-03 14:39:53 -07:00
parent bc3bd3d1fc
commit 999f24ce52
7 changed files with 85 additions and 4 deletions

View File

@ -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

View File

@ -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!()};

View File

@ -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"},
)
}
}
}

View File

@ -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!() };