Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Can multiplication be done with parentheses? #39

Closed
antingle opened this issue May 7, 2022 · 1 comment
Closed

Question: Can multiplication be done with parentheses? #39

antingle opened this issue May 7, 2022 · 1 comment
Labels

Comments

@antingle
Copy link

antingle commented May 7, 2022

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?

@nicklockwood
Copy link
Owner

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants