K65 Evaluator
From KK's Wiki
Evaluator in K65 is compiletime 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  Lefttoright 


Suffix decrement  
()

Function call  
[]

Array subscripting  
.

Single argument function call  
3  ++

Prefix increment  Righttoleft 


Prefix decrement  
+

Unary plus  


Unary minus  
!

Logical NOT  
~

Bitwise NOT (One's Complement)  
5  *

Multiplication  Lefttoright 
/

Division  
%

Modulo (remainder)  
6  +

Addition  Lefttoright 


Subtraction  
7  <<

Bitwise left shift  Lefttoright 
>>

Bitwise right shift  
8  <

Less than  Lefttoright 
<=

Less than or equal to  
>

Greater than  
>=

Greater than or equal to  
?>

Select greater value (maximum)  
?<

Select smaller value (minimum)  
9  ==

Equal to  Lefttoright 
!=

Not equal to  
10  &

Bitwise AND  Lefttoright 
11  ^

Bitwise XOR (exclusive or)  Lefttoright 
12  

Bitwise OR (inclusive or)  Lefttoright 
13  &&

Logical AND  Lefttoright 
14  

Logical OR  Lefttoright 
15  ?:

Ternary conditional  Righttoleft 
16  =

Direct assignment  Righttoleft 
+=

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)  Lefttoright 
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 ( xfloor(x) ) 
index( tab, x )  1dimensional indexing operator ( same as tab[x] ) 
index( tab, x, y )  2dimensional 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 