Difference between revisions of "K65 Evaluator"
From KK's Wiki
m |
|||
| Line 2: | Line 2: | ||
__FORCETOC__ | __FORCETOC__ | ||
Evaluator in '''[[K65]]''' is compile-time executed expression language very similar to C/C++ expressions. | Evaluator in '''[[K65]]''' is compile-time executed expression language very similar to C/C++ expressions. | ||
| + | |||
| + | === Evaluator introduction === | ||
| + | In every place where the compiler expects a single number, you can invoke the evaluator by simply using square brackets. | ||
| + | |||
| + | data NumberFour { | ||
| + | [2+2] | ||
| + | } | ||
| + | |||
| + | The evaluator can be also invoked freely outside any section body, e.g. to set variables: | ||
| + | |||
| + | [ FOUR = 4 ] | ||
| + | |||
| + | which can be later used freely as compiler constants and within further evaluator expressions: | ||
| + | |||
| + | data FourFiveSix { | ||
| + | FOUR // value of variable FOUR defined earlier | ||
| + | [FIVE = FOUR+1] // defines variable FIVE and returns its value | ||
| + | [FIVE+1] // uses recently set FIVE value | ||
| + | } | ||
| + | |||
=== Evaluator operators === | === Evaluator operators === | ||
Latest revision as of 13:51, 12 June 2015
Evaluator in K65 is compile-time executed expression language very similar to C/C++ expressions.
Evaluator introduction
In every place where the compiler expects a single number, you can invoke the evaluator by simply using square brackets.
data NumberFour {
[2+2]
}
The evaluator can be also invoked freely outside any section body, e.g. to set variables:
[ FOUR = 4 ]
which can be later used freely as compiler constants and within further evaluator expressions:
data FourFiveSix {
FOUR // value of variable FOUR defined earlier
[FIVE = FOUR+1] // defines variable FIVE and returns its value
[FIVE+1] // uses recently set FIVE value
}
Evaluator operators
Available operators and their precedence levels are similar to operators in C and C++
| Precedence | Operator | Description | Associativity |
|---|---|---|---|
| 2
highest |
++
|
Suffix increment | Left-to-right |
--
|
Suffix decrement | ||
()
|
Function call | ||
[]
|
Array subscripting | ||
.
|
Single argument function call | ||
| 3 | ++
|
Prefix increment | Right-to-left |
--
|
Prefix decrement | ||
+
|
Unary plus | ||
-
|
Unary minus | ||
!
|
Logical NOT | ||
~
|
Bitwise NOT (One's Complement) | ||
| 5 | *
|
Multiplication | Left-to-right |
/
|
Division | ||
%
|
Modulo (remainder) | ||
| 6 | +
|
Addition | Left-to-right |
-
|
Subtraction | ||
| 7 | <<
|
Bitwise left shift | Left-to-right |
>>
|
Bitwise right shift | ||
| 8 | <
|
Less than | Left-to-right |
<=
|
Less than or equal to | ||
>
|
Greater than | ||
>=
|
Greater than or equal to | ||
?>
|
Select greater value (maximum) | ||
?<
|
Select smaller value (minimum) | ||
| 9 | ==
|
Equal to | Left-to-right |
!=
|
Not equal to | ||
| 10 | &
|
Bitwise AND | Left-to-right |
| 11 | ^
|
Bitwise XOR (exclusive or) | Left-to-right |
| 12 | |
|
Bitwise OR (inclusive or) | Left-to-right |
| 13 | &&
|
Logical AND | Left-to-right |
| 14 | ||
|
Logical OR | Left-to-right |
| 15 | ?:
|
Ternary conditional | Right-to-left |
| 16 | =
|
Direct assignment | Right-to-left |
+=
|
Assignment by sum | ||
-=
|
Assignment by difference | ||
*=
|
Assignment by product | ||
/=
|
Assignment by quotient | ||
%=
|
Assignment by remainder | ||
<<=
|
Assignment by bitwise left shift | ||
>>=
|
Assignment by bitwise right shift | ||
&=
|
Assignment by bitwise AND | ||
^=
|
Assignment by bitwise XOR | ||
|=
|
Assignment by bitwise OR | ||
| 18
lowest |
,
|
Expression list (executes in sequence, returns value of the last) | Left-to-right |
Evaluator functions
Following functions are available within evaluator expressions.
| Function name | Description |
|---|---|
| acos( x ) | Arcus cosinus |
| addbyte( sec, b ) | Add byte b to section sec |
| asin( x ) | Arcus sinus |
| ceil( x ) | Round up to nearest integer |
| clamp( x, min, max ) | Clamp x to range min to max |
| color( r, g, b ) | Return palette index for nearest color to color (r,g,b) |
| color( x ) | Return palette index for nearest color specified in format 0xRRGGBB |
| cos( x ) | Cosinus |
| error( err ) | Print error message err ane terminate compilation |
| floor( x ) | Round down to nearest integer |
| frac( x ) | Get fractional part ( x-floor(x) ) |
| index( tab, x ) | 1-dimensional indexing operator ( same as tab[x] ) |
| index( tab, x, y ) | 2-dimensional indexing operator ( same as tab[x,y] ) |
| max( a, b ) | Maximum |
| min( a, b ) | Minimum |
| pow( x, y ) | Power function |
| print( msg ) | Print message msg |
| rnd( ) | Random value 0 <= x < 1 |
| round( x ) | Round to nearest integer |
| sin( x ) | Sinus |
| size( sec ) | Current size of section sec |
| sqrt( x ) | Square root |