Author Topic: String formula evaluator  (Read 2571 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
String formula evaluator
« on: September 22, 2018, 02:50:30 PM »
I am looking for advice on a string formula evaluator, ideally the formula would be sent to the evaluator and it would return an object that could be cast into a numeric type.


I am currently working with Flee but it only does the basic stuff. I need ROUND, ABS, MIN, MAX, etc. and it doesn't work well with the new project.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: String formula evaluator
« Reply #1 on: September 22, 2018, 08:19:34 PM »
There's no easy way to do this unless you build a simple interpreter. The hardest part is parsing and lexing but as you may only need a small subset of a real language it will be a lot easier.

http://www.craftinginterpreters.com/

The link above is excellent and even just reading through part 1 (the Java interpreter) will give you more than enough info to get something going or at least some direction or a better understanding of Flee.

I've built the Java version and about to start the C version, my interest is in building a small scripting language to automate my plugins. What you are doing is no different only you are parsing short sentences which will be a lot easier (in theory :) )
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: String formula evaluator
« Reply #2 on: September 22, 2018, 11:54:48 PM »
Precisely why I am looking for an existing library.

Ideally the library would allow me to import functions that would be interpreted something like:

Code - C#: [Select]
  1. Evaluator.functions.Add(foo);
  2.  
  3. function double foo(double value)
  4. {
  5.     //dostuff here
  6. }

I'm having a little bit of difficulty right now .. just got home and I am pretty sure I have consumed nearly a whole 1.5L bottle so forgive me if my message is a little incoherent
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: String formula evaluator
« Reply #3 on: September 24, 2018, 03:49:11 PM »
As far as the orig question goes, read the first two sections.

https://llvm.org/docs/tutorial/

Quote
Kaleidoscope: Implementing a Language with LLVM
1. Kaleidoscope: Tutorial Introduction and the Lexer
2. Kaleidoscope: Implementing a Parser and AST
...

Going to IR/L is a bit overkill for a simple parser like this (a whole mess more code).

Mick,
If you put that code up on github (or the like), let me know. That would be cool.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: String formula evaluator
« Reply #4 on: September 24, 2018, 04:38:39 PM »
Oh my ... and Saturday night was something else ... turns out I drank a whole 1.5L bottle of Captain Morgan.

Anyway, the reading assignment looks pretty cool. I'm sure its something I could work with. A week or two should give me enough time to have enough to do what I need, maybe even more :-)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: String formula evaluator
« Reply #5 on: September 24, 2018, 05:58:12 PM »
Oh my ... and Saturday night was something else ... turns out I drank a whole 1.5L bottle of Captain Morgan.

Anyway, the reading assignment looks pretty cool. I'm sure its something I could work with. A week or two should give me enough time to have enough to do what I need, maybe even more :-)

:D

It's a good read/study and well worth it I reckon. You will definitely be able to knock up something simple n C# :)



Mick,
If you put that code up on github (or the like), let me know. That would be cool.

Will do, the C section by the author is still in progress but is starting to get interesting - dynamic strings are next :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: String formula evaluator
« Reply #6 on: September 24, 2018, 10:01:31 PM »
Fortunately for me, there is really only a small subset of mathematical functions that I need to implement and I will only need to evaluate two functions, although those two will need to be evaluated on average about 300 times each. 
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie