0

I am creating online calculator which allows user to perform mathmatical operations. In core, I am using math.js evaluator to get the results of the operations.

Currently, user can type operations one by one in command line, but next step of my idea should allow user to write few lines of javascript (or custom syntax) code in some textarea or different editor and then evaluate whole input. Problem is that math.js eval cannot handle that, but, it allows to extend the built-in functionality by custom functions.

Is there any way to extend the library to handle for/if/switch statements? Or maybe should I think about some different approach (parse each line and check for specific statement name? What should I do with brackets them?).

Has anyone enforced similar problem? Any suggestions how I can handle that problem?

Kamil
  • 782
  • 1
  • 6
  • 22
  • What would you want the user to do with the javascript code? It's not as if js would be a mathematical expression that evaluates to a single value. – Bergi May 31 '16 at 15:02
  • Can you show us a user input you would like to evaluate and the expected result? – le_m May 31 '16 at 15:15
  • @Bergi and @le_m, let's say user want's to control the flow of the script that he wrote, like in Matlab, where you can define custom function, inside where you can type for example `for i = 1:6 c[i] = a[i] * b[i] end` and similar.. I wanted to allow users to fill in whole algorithms and use them as a functions which could be run from command line next – Kamil May 31 '16 at 15:20
  • I think you will want to write your own scripting language for that, not use javascript. – Bergi May 31 '16 at 15:21
  • @Bergi not really, I think it could handle normal javascript style code, not sure if checking that with regular expressions is a good idea.. – Kamil May 31 '16 at 15:29
  • @Kamil: That's a very bad idea, javascript is not a regular language. – Bergi May 31 '16 at 15:31

1 Answers1

0

Is there any way to extend the library to handle for/if/switch statements?

No that's not supported. It's on the wishlist though, see:

https://github.com/josdejong/mathjs/issues/467

Jos de Jong
  • 6,602
  • 3
  • 38
  • 58
  • Yeah, I was checking that yesterday, thanks. However, I was thinking about solution to parse whole input code line by line and detect for/if/switch statements, and those lines should be skipped, otherwise lines should be wrapped with math.eval, maybe that should do the trick.. – Kamil Jun 01 '16 at 13:42
  • It does not have to be strictly connected to mathjs library, it can be wrapper for that which will generate proper code – Kamil Jun 01 '16 at 13:43