mparlak / Flee

Fast Lightweight Expression Evaluator
629 stars 120 forks source link

Syntax Documentation #85

Open Allsetra-RB opened 4 years ago

Allsetra-RB commented 4 years ago

Hi,

I was having some trouble finding the exact syntax the library requires. Just wanted to mention that it's worth documenting. I found a post on codeproject that describes the syntax. Consider working it into the GetStarted documentation page.

(The link might be out of date. I found that I needed to use ; as a separator in the if statement, which makes sense since in Dutch the , conflicts with a decimal point.)

gotmachine commented 3 years ago

The lack of a documentation for the expression language syntax is indeed annoying. Took the liberty to convert the codeproject table to markdown, in case @mparlak has some time to update the wiki. So far I haven't found anything that is outdated, but I haven't tested everything.

The Expression Language

The expression language that this library parses is a mix of elements of C# and VB.NET. Since the aim of this library is speed, the language is strongly typed (same rules as C#) and there is no late binding. Unlike C#, the language is not case-sensitive. Here is a breakdown of the language elements:

Element Description Example
+, - Additive 100 + a
*, /, % Multiplicative 100 * 2 / (3 % 2)
^ Power 2 ^ 16
- Negation -6 + 10
+ Concatenation "abc" + "def"
<<, >> Shift 0x80 >> 2
=, <>, <, >, <=, >= Comparison 2.5 > 100
And, Or, Xor, Not Logical (1 > 10) and (true or not false)
And, Or, Xor, Not Bitwise 100 And 44 or (not 255)
If Conditional If(a > 100, "greater", "less")
Cast Cast and conversion cast(100.25, int)
[] Array index 1 + arr[i+1]
. Member varA.varB.function("a")
String literal "string!"
Char literal 'c'
Boolean literal true AND false
Real literal Double and single 100.25 + 100.25f
Integer literal Signed/unsigned 32/64 bit 100 + 100U + 100L + 100LU
Hex literal 0xFF + 0xABCDU + 0x80L + 0xC9LU
### The Expression Language

The expression language that this library parses is a mix of elements of C# and VB.NET. Since the aim of this library is speed, the language is strongly typed (same rules as C#) and there is no late binding. Unlike C#, the language is not case-sensitive. Here is a breakdown of the language elements:

| Element             | Description               | Example                            |
|---------------------|---------------------------|------------------------------------|
| +, -                | Additive                  | `100 + a `                         |
| *, /, %             | Multiplicative            | `100 * 2 / (3 % 2)`                |
| ^                   | Power                     | `2 ^ 16`                           |
| -                   | Negation                  | `-6 + 10`                          |
| +                   | Concatenation             | `"abc" + "def"`                    |
| <<, >>              | Shift                     | `0x80 >> 2`                        |
| =, <>, <, >, <=, >= | Comparison                | `2.5 > 100`                        |
| And, Or, Xor, Not   | Logical                   | `(1 > 10) and (true or not false)` |
| And, Or, Xor, Not   | Bitwise                   | `100 And 44 or (not 255)`          |
| If                  | Conditional               | `If(a > 100, "greater", "less")`   |
| Cast                | Cast and conversion       | `cast(100.25, int)`                |
| []                  | Array index               | `1 + arr[i+1]`                     |
| .                   | Member                    | `varA.varB.function("a")`          |
| String literal      |                           | `"string!"`                        |
| Char literal        |                           | `'c'`                              |
| Boolean literal     |                           | `true AND false`                   |
| Real literal        | Double and single         | `100.25 + 100.25f`                 |
| Integer literal     | Signed/unsigned 32/64 bit | `100 + 100U + 100L + 100LU`        |
| Hex literal         |                           | `0xFF + 0xABCDU + 0x80L + 0xC9LU`  |
hunkydoryrepair commented 3 years ago

Would like to see these in order of operator precedence, too.

The "Getting Started" mentions overloading operators, but that also is undocumented. I can see the code that is checking for overloaded operators, but not how to set them. I supplied my own "Multiply" function but it does not get used.