Expression icon indicating copy to clipboard operation
Expression copied to clipboard

Question: Can multiplication be done with parentheses?

Open antingle opened this issue 2 years ago • 1 comments

I am currently working on creating a calculator and have been using this framework for expression parsing, which has been going wonderfully so far. I am wondering if there is a way to implement multiplication with parentheses (for example "4(2)" = 8). If this is not possible with the current state of the framework, is it something worth adding in?

antingle avatar May 07 '22 06:05 antingle

@ingleanthony yes, this is possible. You need to provide a custom implementation for the () operator, like this:

let expression = Expression("4(2)", symbols: [
      .infix("()"): { args in args[0] * args[1] },
])
print(try expression.evaluate()) // prints 8

nicklockwood avatar May 10 '22 18:05 nicklockwood