I've noticed that it actually seems there's no eval() plugin available around here. Granted, it's not that hard to write such functionality yourself, but it can always be nice to have a set of commands ready to go, so I figured I'd make a plugin out of this.
So, what is it?
This plugin lets you type in a mathematical expression as a string and get the resulting value as easily as this:
result as double float
result = evaluate("5 * 9")
// 'result' will now be equal to 45
There is no real limit to the number of terms you may use; using brackets as well as boolean arithmetics you can feed it an arbitrarily complex expression and (hopefully
) get the correct result rerurned.
For the sake of accuracy, all values are considered as double floats. Therefore, for example, evaluate("5 / 2") will yield 2.5, and not drop the 5 due to integer division. In other words, there is no need to write the expression as "5 / 2.0" or similar for these situations.
Progress:
The plugin seems to be pretty stable in its current state, and does indeed evaluate standard and boolean expressions correctly - you can download the current "demo" version a bit further down in this post. Except for poor error handling (when for example passing an incomprehendable expression), I would say that the plugin is in working order in its current state, and is mainly in line for new / extended features; most notably being the possibility to use mathematical functions in expressions.
Commands in current version
init eval - lets the plugin create the objects it needs to operate.
free eval - frees the memory previously allocated by
init eval
evaluate - evaluates an expression string, that might contain custom variables set by
set variable, and returns the resulting value as a double float.
set variable - sets a variable for use within the evaluation system.
purge variables - removes all previously declared variable entries.
It should be noted that undeclared variable names in expressions are ignored by the evaluate function. They are
not replaced with default values of zero.
A note on the boolean operators
I figured that, at a later point, the evaluation function should allow variable declarations / value setting within expressions themselves, using the '=' sign (it already supports variables, but they must be set using the
set variable command explicitly as of current). Therefore, I decided to use the C++ (and most other languages as well, for that matter) equality operator '==' instead of the single equality sign from DB. Thus, it seemed reasonable to use those notations for the other boolean operators as well.
Here's a list:
? '==' equality check DBP equivalent: '='
? '!=' not equal DBP equivalent: '<>'
? '&&' and operator DBP equivalent: 'AND'
? '||' or operator DBP equivalent: 'OR'
? '^' xor operator DBP equivalent: 'XOR'
? '!' not operator DBP equivalent: 'NOT'
<, <=, >, >= are the same.
It should also be noted that spaces / tabs are removed from the expression during parsing, so one can format the input string as desired.
Example code
// Prepare the evaluation system for use
init eval
// Create some custom variables to be used within evaluated expressions
set variable "myVar1", 9
set variable "myVar2", 41.2
// Print some simple calculations to the screen
print "1 + 2 == "; evaluate("1 + 2")
print "myVar1 ("; evaluate("myVar1"); ") * myVar2 ("; evaluate("myVar2"); ") == "; evaluate("myVar1 * myVar2")
// Print a boolean expression
// (Reads [1 AND 0 (false)] XOR [1 OR 0 (true)], which should evaluate to true,
// represented by a one in DarkBASIC style.
print "(1 && 0) ^ (1 || 0) == "; evaluate("(1 && 0) ^ (1 || 0)")
// A slightly more complex expression
expr$ = "(((3564 - 145) / 3.1) > 49.9) * (myVar2 - ((myVar1 != 2) * 5)) + 12.4"
// We can really just go on like this... but then it would be rather
// tedious for you to verify that it actually works. ;-)
print expr$; " == "; evaluate(expr$)
print ""
print "Now enter your own expressions (leave empty to quit)."
_continue = 1
while _continue
input " > ", expr$
if len(expr$) < 1
_continue = 0
else
print " "; evaluate(expr$)
endif
endwhile
// Finally, let the plugin delete all objects it has created.
// Failing to do this will likely cause memory leaks, so beware.
free eval
end
Download plugin
Comes with a keyword and help files.
Click here to download.
Well, thanks for your time.
Would appreciate some feedback