Search code examples
phpscriptingprogramming-languagesembedded

mini scripting language on top of php


I am looking for a mini scripting language that runs on php, I need to interpret simple formulas (something that a scripting language for a spreadsheet would do). The main thing that I need is Variables and affectation and mathematical operations conditions and if statement.

ex :

if(price1>360) result = (price1 * Q) + price1 * Q /17 else ...


Solution

  • I suggest you take a look at some of the concepts used in a template system like Smarty and implement your own. For instance, the math template allows you to perform operations like this:

    {* $height=4, $width=5 *}
    {math equation="x + y" x=$height y=$width}
    

    You can just grab the math functions from here, if you want a general guideline of how to create them. There is still a risk of issues since this uses eval() to do the computations.

    In addition, here is a class to evaluate RPN (Reverse Polish Notation) to support the operators: +, - , *, /, IF, THEN, SWAP, DUP, =, <>, >, <. >= and <= .