B4A Library Equation Evaluator

None of the other equation evaluators I found met my all requirements

-Being able to make a transfinite amount of variables, not limited to a single character as the name
-Working both offline and off (ie: Using Google Calculator, Wolfram Alpha)
-Not requiring a webview + my telnet workaround (ie: Javascript)
-Allowing you to make functions both at design time and run time
-Handling both strings and numbers
-Allowing custom variable types
-Didn't take 10 lines of code per variable/function
-Supports scope

So I ported the one I made for a game engine from VB6.
Yes, this is powerful to make scripting engines out of

Most of the syntax is the same as B4A, with some exceptions.
^ properly does exponents for example.

Usage:

To add subs at design time:
Declare it in Eval.RegisterInternals
Add it to Eval.InternalFunction

To add subs at run time, use the equation:
function <name> (parameters separated by commas)
ie: function test (temp as int = 2)
Yes, it supports optional parameters. Something not even B4A does :)

Parameters and variables can be of the following types:
Numbers ("int", "integer", "long", "single", "double", "number", "num", "byte", "float", "val")
Text ("string", "text", "txt", "char") All strings must be in single or double quotes when entered into an equation. this works like javascript, in that if you start with ', you can put " inside. And same for starting with "
Boolean ("yesno", "boolean", "bool", "bit")

To add a custom variable type:
dim <type name> (methods separated by commas)

Because = can be used in an equation, I couldn't use = to set the contents of a variable, instead I went with the C method of using == to set variables.
You cannot set the values of constants
ie: dim tempstr as string
tempstr == "test" (sets tempstr to "test")
tempstr = "moo" (will return false, cause tempstr = "test")
tempstr & "moo" (will return "testmoo")
tempstr == ("test", "moo") will turn tempstr into an array
tempstr(0) will return "test"

If you start any equation with a number, then an @ symbol, it uses the number as the scope, and anything following the @ as the equation.

Normally, you'd use Eval.Val(Equation) and check if Eval.EvalError contains data. I store errors like dividing by zero there. I made a debug version which combines the 2 into Eval.ValError

There's some included functions for making a calculator GUI as I have in another program. Mainly: EnumVariables, EnumCustomVariables, EnumStrings
 

Attachments

  • Eval.zip
    19 KB · Views: 276

Informatix

Expert
Licensed User
Longtime User
Because = can be used in an equation, I couldn't use = to set the contents of a variable, instead I went with the C method of using == to set variables.
In C, as in Java, it's the contrary. = is an assignment operator while == is a comparison operator.
 

ELCHARO

Member
Licensed User
Longtime User
NeoTechni:
Nice work. Thanks.
If posible to create a few examples to show the library posibilities.
I need to understand the posibilities reading the source code , and of course is the long way.

Thanks. Again. Ricardo
 
Top