Skip to content

Commit

Permalink
support subtraction operation in array symbolic value (#251)
Browse files Browse the repository at this point in the history
* support subtraction in symbolic value
  • Loading branch information
katat authored Jan 21, 2025
1 parent 1bcd765 commit 1f4844c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/mast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ impl Symbolic {
}
Symbolic::Generic(g) => gens.get(&g.value),
Symbolic::Add(a, b) => a.eval(gens, tast) + b.eval(gens, tast),
Symbolic::Sub(a, b) => a.eval(gens, tast) - b.eval(gens, tast),
Symbolic::Mul(a, b) => a.eval(gens, tast) * b.eval(gens, tast),
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ pub enum Symbolic {
/// Generic parameter
Generic(Ident),
Add(Box<Symbolic>, Box<Symbolic>),
Sub(Box<Symbolic>, Box<Symbolic>),
Mul(Box<Symbolic>, Box<Symbolic>),
}

Expand All @@ -198,6 +199,7 @@ impl Display for Symbolic {
Symbolic::Constant(ident) => write!(f, "{}", ident.value),
Symbolic::Generic(ident) => write!(f, "{}", ident.value),
Symbolic::Add(lhs, rhs) => write!(f, "{} + {}", lhs, rhs),
Symbolic::Sub(lhs, rhs) => write!(f, "{} - {}", lhs, rhs),
Symbolic::Mul(lhs, rhs) => write!(f, "{} * {}", lhs, rhs),
}
}
Expand All @@ -219,7 +221,7 @@ impl Symbolic {
Symbolic::Generic(ident) => {
generics.insert(ident.value.clone());
}
Symbolic::Add(lhs, rhs) | Symbolic::Mul(lhs, rhs) => {
Symbolic::Add(lhs, rhs) | Symbolic::Mul(lhs, rhs) | Symbolic::Sub(lhs, rhs) => {
generics.extend(lhs.extract_generics());
generics.extend(rhs.extract_generics());
}
Expand Down Expand Up @@ -251,6 +253,7 @@ impl Symbolic {
// no protected flags are needed, as this is based on expression nodes which already ordered the operations
match op {
Op2::Addition => Ok(Symbolic::Add(Box::new(lhs), Box::new(rhs?))),
Op2::Subtraction => Ok(Symbolic::Sub(Box::new(lhs), Box::new(rhs?))),
Op2::Multiplication => Ok(Symbolic::Mul(Box::new(lhs), Box::new(rhs?))),
_ => Err(Error::new(
"mast",
Expand Down

0 comments on commit 1f4844c

Please sign in to comment.