0

I mean function that is only able to do logical and mathematical operations, for example: 3+5<=9, "string"=="string" && (3<9 || 12>3), but not like this a+b<c, "string".length > 3 or calling functions, so the programmer must resolve (by regex or otherwise) all the possible names before it is evaluated ?

edit:

So I found one good reason to use eval() ...

9
  • 1
    You can make a parser yourself.. there are many tutorials on the web Commented Aug 9, 2012 at 13:21
  • possible duplicate of Equation (expression) parser with precedence? Commented Aug 9, 2012 at 13:23
  • possible duplicate of Javascript math parser library Commented Aug 9, 2012 at 13:24
  • @Esailija: yes but isn't it reinventing the wheel ? As I think of it the eval function should have a few options to restrict what code inside can do. Or maybe another written for scratch meant for such simple cases, much faster (when run by js engine and not my crappy code) well pity Commented Aug 9, 2012 at 13:24
  • I didn't read your motives well then, of course you can use an already made library as well. But then you won't learn anything (Since this task is usually done for learning) Commented Aug 9, 2012 at 13:26

2 Answers 2

2

Tbh I think the simplest answer to your question is 'no there isn't'.

You can write one yourself, find an existing one (which will be a JS library) - or get a parser generator to generate you a parser that can run on Javascript.

ANTLR is one such tool/platform - you'll find the Javascript runtime download on the downloads page. You will need the ANTLRWorks tool in order to get to grips with it, however, and produce a gammar that actually works.

It's possible to get a rudimentary Math parser (supporting the four main operators) up and running in a few minutes once you know what you're doing.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, there is a solution: just create a new function object, it has advantage that it does not pollute scope... well it looks definitely safer: https://stackoverflow.com/a/7650148/393087

Yes, I know , this is not what I really wanted but is closer in right direction in terms of safety.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.