wiki:Evaluator

Version 6 (modified by dani, 14 years ago) (diff)

--

The Evaluator

The Evaluator is probably the most exciting and important feature in LCD4Linux. Basically, it's a subsystem which evaluates expressions and returns the result. Nearly everything in LCD4Linux passes the Evaluator, so you can use expressions for nearly everything!

An Expression is just a simple string. The result of an expression is a string, too. Internally, the Evaluator uses a two-way-representation of every value, it is stored as a string and as a floating point number (for mathematical calculations). Conversion between the two representations will be done on the fly: If a operation expects a numerical argument (e.g. an addition), but the argument is a string, it will be convertet to float, and vice versa.

The Evaluator has a very relaxed error handling: If some error occurs, it emits a message, but it continues, usually with a NULL result (which is either an empty string or 0).

The Evaluator knows Operators and Functions. Operators are hardcoded into the Evaluator, while Functions can be added at runtime. This is what Plugins do: They just add one or more functions to the Evaluator.

The Evaluator can be easily tested using LCD4Linux's 'interactive mode': Just run 'lcd4linux -i', and you will get a prompt where you can enter an expression (e.g. '2+3'), and the result will be displayed (e.g. '5'). This is very handy for testing plugins!

Operators

TokenDescriptionExampleResult
; expression lists2+3;2*36
= variable assignementsa=2+35
? : conditional a?b:ca > 1024 ? 'kB' : 'MB''kB'
| logical OR 1 | 0 1
& logical AND 1 & 0 0
< less than 2 < 3 1
> greater than 2 > 3 0
+ addition 2 + 3 5
- subtraction or sign 2 - 3 -1
. string concatenation 'Hi' . ' ' . 'there' 'Hi there'
* multiplication2 * 3 6
/ division 6 / 2 3
% modulo 5 % 2 1
power 23 8
! logical NOT !0 1
( ) opening/closing brace 2 * (3 + 4) 14
, comma (argument seperator) func (2, 3)
== equal 2 == 3 0
!= not equal 2 != 3 1
<= less or equal 2 <= 3 1
>= greater or equal 2 >= 3 0

Functions

The Evaluator does not have any built-in functions, but such functions are added by plugins. A plugin can be an explicit one, but display drivers can add functions, too (all specific display features are implemented as plugins, e.g. contrast, backlight, GPO's, temperature sensors, fan speed reading and setting, and so on). This is described in detail in the corresponding display driver documentation.

Functions can have a fixed or variable number of parameters; this depends on the implementation, and is described in detail in the corresponding documentation.

Functions usually return something, but they can have side-effects, too: a function can set or control something, or trigger an action (e.g. if your display has a software-controlled backlight, this is implemented as a function, usually with variable parameters: 'LCD::backlight()' returns the current backlight level, while 'LCD::backlight(n)' sets the backlight to 'n')