Skip to content

Commit

Permalink
Allow wildcard matching a variable as function name
Browse files Browse the repository at this point in the history
- Do not add implicit multiplication operator between two numbers when parsing, to prevent input bugs
  • Loading branch information
benruijl committed Sep 7, 2024
1 parent 32a685c commit 274c852
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
16 changes: 14 additions & 2 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,21 @@ impl Pattern {
if name.get_wildcard_level() > 0 {
if let Some(w) = match_stack.get(name) {
if let Match::FunctionName(fname) = w {
name = *fname
name = *fname;
} else if let Match::Single(a) = w {
if let AtomView::Var(v) = a {
name = v.get_symbol();
} else {
Err(TransformerError::ValueError(format!(
"Wildcard must be a function name instead of {}",
w.to_atom()
)))?;
}
} else {
unreachable!("Wildcard must be a function name")
Err(TransformerError::ValueError(format!(
"Wildcard must be a function name instead of {}",
w.to_atom()
)))?;
}
} else if !match_stack.settings.allow_new_wildcards_on_rhs {
Err(TransformerError::ValueError(format!(
Expand Down
26 changes: 19 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,14 @@ impl Token {

Ok(())
} else {
Err(format!(
"operator expected, but found '{}'. Are parentheses unbalanced?",
self
))
if let Token::Number(n) = other {
Err(format!("operator expected between '{}' and '{}'", self, n))
} else {
Err(format!(
"operator expected, but found '{}'. Are parentheses unbalanced?",
self
))
}
}
}

Expand Down Expand Up @@ -851,8 +855,16 @@ impl Token {
}
']' => stack.push(Token::CloseBracket),
_ => {
if unsafe { stack.last().unwrap_unchecked() }.is_normal() {
// insert multiplication: x y -> x*y
if unsafe { stack.last().unwrap_unchecked() }.is_normal()
&& (!c.is_ascii_digit()
|| !matches!(
unsafe { stack.last().unwrap_unchecked() },
Token::Number(_)
))
{
// insert implicit multiplication: x y -> x*y
// do not allow implicit multiplication between two numbers as the risk
// of a typo is too high
stack.push(Token::Op(true, true, Operator::Mul, vec![]));
extra_ops.push(c);
} else if c.is_ascii_digit() {
Expand Down Expand Up @@ -1324,7 +1336,7 @@ mod test {

#[test]
fn float() {
let input = Atom::parse("1.2`20x+1e-5`20+1e+5 1.1234e23 +2exp(5)").unwrap();
let input = Atom::parse("1.2`20x+1e-5`20+1e+5 * 1.1234e23 +2exp(5)").unwrap();

let r = format!(
"{}",
Expand Down

0 comments on commit 274c852

Please sign in to comment.