Skip to content

Commit

Permalink
support subtraction in symbolic value
Browse files Browse the repository at this point in the history
  • Loading branch information
katat committed Jan 4, 2025
1 parent 6f8db33 commit ceb449a
Show file tree
Hide file tree
Showing 2 changed files with 7 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 @@ -398,6 +398,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
7 changes: 6 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,9 @@ 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 +255,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 ceb449a

Please sign in to comment.