Difference between revisions of "K65 Evaluator"

From KK's Wiki
Jump to: navigation, search
(Created page with "Category:K65 Evaluator in K65 is compile-time executed expression language very similar to C/C++ expressions. === Evaluator operators === Available operators and their pr...")
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:K65]]
 
[[Category:K65]]
Evaluator in K65 is compile-time executed expression language very similar to C/C++ expressions.
+
__FORCETOC__
 +
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 ===
Line 13: Line 34:
 
! rowspan=5| 2
 
! rowspan=5| 2
 
<small>highest</small>
 
<small>highest</small>
| style="border-bottom-style: none" | <code>++</code>
+
| style="border-bottom-style: none; text-align: center" | <code>++</code>
 
| style="border-bottom-style: none" | Suffix increment
 
| style="border-bottom-style: none" | Suffix increment
 
| style="vertical-align: top" rowspan="5" | Left-to-right
 
| style="vertical-align: top" rowspan="5" | Left-to-right
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>--</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>--</code>
 
| style="border-bottom-style: none; border-top-style: none" | Suffix decrement
 
| style="border-bottom-style: none; border-top-style: none" | Suffix decrement
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>()</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>()</code>
 
| style="border-bottom-style: none; border-top-style: none" | Function call
 
| style="border-bottom-style: none; border-top-style: none" | Function call
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>[]</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>[]</code>
 
| style="border-bottom-style: none; border-top-style: none" | Array subscripting
 
| style="border-bottom-style: none; border-top-style: none" | Array subscripting
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>.</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>.</code>
 
| style="border-bottom-style: none; border-top-style: none" | Single argument function call
 
| style="border-bottom-style: none; border-top-style: none" | Single argument function call
 
|-
 
|-
 
! rowspan=6| 3
 
! rowspan=6| 3
| style="border-bottom-style: none" | <code>++</code>
+
| style="border-bottom-style: none; text-align: center" | <code>++</code>
 
| style="border-bottom-style: none" | Prefix increment
 
| style="border-bottom-style: none" | Prefix increment
 
| style="vertical-align: top" rowspan="6" | Right-to-left
 
| style="vertical-align: top" rowspan="6" | Right-to-left
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>--</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>--</code>
 
| style="border-bottom-style: none; border-top-style: none" | Prefix decrement
 
| style="border-bottom-style: none; border-top-style: none" | Prefix decrement
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>+</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>+</code>
 
| style="border-bottom-style: none; border-top-style: none" | Unary plus
 
| style="border-bottom-style: none; border-top-style: none" | Unary plus
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>-</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>-</code>
 
| style="border-bottom-style: none; border-top-style: none" | Unary minus
 
| style="border-bottom-style: none; border-top-style: none" | Unary minus
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>!</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>!</code>
 
| style="border-bottom-style: none; border-top-style: none" | Logical NOT
 
| style="border-bottom-style: none; border-top-style: none" | Logical NOT
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>~</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>~</code>
 
| style="border-bottom-style: none; border-top-style: none" | Bitwise NOT (One's Complement)
 
| style="border-bottom-style: none; border-top-style: none" | Bitwise NOT (One's Complement)
 
|-
 
|-
 
! rowspan=3| 5
 
! rowspan=3| 5
| style="border-bottom-style: none" | <code>*</code>
+
| style="border-bottom-style: none; text-align: center" | <code>*</code>
 
| style="border-bottom-style: none" | Multiplication
 
| style="border-bottom-style: none" | Multiplication
 
| style="vertical-align: top" rowspan="3" | Left-to-right
 
| style="vertical-align: top" rowspan="3" | Left-to-right
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>/</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>/</code>
 
| style="border-bottom-style: none; border-top-style: none" | Division
 
| style="border-bottom-style: none; border-top-style: none" | Division
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>%</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>%</code>
 
| style="border-bottom-style: none; border-top-style: none" | Modulo (remainder)
 
| style="border-bottom-style: none; border-top-style: none" | Modulo (remainder)
 
|-
 
|-
 
! rowspan=2| 6
 
! rowspan=2| 6
| style="border-bottom-style: none" | <code>+</code>
+
| style="border-bottom-style: none; text-align: center" | <code>+</code>
 
| style="border-bottom-style: none" | Addition
 
| style="border-bottom-style: none" | Addition
 
| style="vertical-align: top" rowspan="2" | Left-to-right
 
| style="vertical-align: top" rowspan="2" | Left-to-right
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>-</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>-</code>
 
| style="border-bottom-style: none; border-top-style: none" | Subtraction
 
| style="border-bottom-style: none; border-top-style: none" | Subtraction
 
|-
 
|-
 
! rowspan=2| 7
 
! rowspan=2| 7
| style="border-bottom-style: none" | <code>&lt;&lt;</code>
+
| style="border-bottom-style: none; text-align: center" | <code>&lt;&lt;</code>
 
| style="border-bottom-style: none" | Bitwise left shift
 
| style="border-bottom-style: none" | Bitwise left shift
 
| style="vertical-align: top" rowspan="2" | Left-to-right
 
| style="vertical-align: top" rowspan="2" | Left-to-right
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&gt;&gt;</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>&gt;&gt;</code>
 
| style="border-bottom-style: none; border-top-style: none" | Bitwise right shift
 
| style="border-bottom-style: none; border-top-style: none" | Bitwise right shift
 
|-
 
|-
 
! rowspan=6| 8
 
! rowspan=6| 8
| style="border-bottom-style: none" | <code>&lt;</code>
+
| style="border-bottom-style: none; text-align: center" | <code>&lt;</code>
 
| style="border-bottom-style: none" | Less than
 
| style="border-bottom-style: none" | Less than
 
| style="vertical-align: top" rowspan="6" | Left-to-right
 
| style="vertical-align: top" rowspan="6" | Left-to-right
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&lt;=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>&lt;=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Less than or equal to
 
| style="border-bottom-style: none; border-top-style: none" | Less than or equal to
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&gt;</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>&gt;</code>
 
| style="border-bottom-style: none; border-top-style: none" | Greater than
 
| style="border-bottom-style: none; border-top-style: none" | Greater than
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&gt;=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>&gt;=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Greater than or equal to
 
| style="border-bottom-style: none; border-top-style: none" | Greater than or equal to
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>?&gt;</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>?&gt;</code>
 
| style="border-bottom-style: none; border-top-style: none" | Select greater value (maximum)
 
| style="border-bottom-style: none; border-top-style: none" | Select greater value (maximum)
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>?&lt;</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>?&lt;</code>
 
| style="border-bottom-style: none; border-top-style: none" | Select smaller value (minimum)
 
| style="border-bottom-style: none; border-top-style: none" | Select smaller value (minimum)
 
|-
 
|-
 
! rowspan=2| 9
 
! rowspan=2| 9
| style="border-bottom-style: none" | <code>==</code>
+
| style="border-bottom-style: none; text-align: center" | <code>==</code>
 
| style="border-bottom-style: none" | Equal to
 
| style="border-bottom-style: none" | Equal to
 
| style="vertical-align: top" rowspan="2" | Left-to-right
 
| style="vertical-align: top" rowspan="2" | Left-to-right
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>!=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>!=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Not equal to
 
| style="border-bottom-style: none; border-top-style: none" | Not equal to
 
|-
 
|-
 
! 10
 
! 10
| <code>&amp;</code>
+
| style="text-align: center" |<code>&amp;</code>
 
| Bitwise AND
 
| Bitwise AND
 
| Left-to-right
 
| Left-to-right
 
|-
 
|-
 
! 11
 
! 11
| <code>^</code>
+
| style="text-align: center" |<code>^</code>
 
| Bitwise XOR (exclusive or)
 
| Bitwise XOR (exclusive or)
 
| Left-to-right
 
| Left-to-right
 
|-
 
|-
 
! 12
 
! 12
| <code><nowiki>|</nowiki></code>
+
| style="text-align: center" |<code><nowiki>|</nowiki></code>
 
| Bitwise OR (inclusive or)
 
| Bitwise OR (inclusive or)
 
| Left-to-right
 
| Left-to-right
 
|-
 
|-
 
! 13
 
! 13
| <code>&amp;&amp;</code>
+
| style="text-align: center" |<code>&amp;&amp;</code>
 
| Logical AND
 
| Logical AND
 
| Left-to-right
 
| Left-to-right
 
|-
 
|-
 
! 14
 
! 14
| <code><nowiki>||</nowiki></code>
+
| style="text-align: center" |<code><nowiki>||</nowiki></code>
 
| Logical OR
 
| Logical OR
 
| Left-to-right
 
| Left-to-right
 
|-
 
|-
 
! 15
 
! 15
| style="border-bottom-style: none" | <code>?:</code>
+
| style="border-bottom-style: none; text-align: center" | <code>?:</code>
 
| style="border-bottom-style: none" | Ternary conditional
 
| style="border-bottom-style: none" | Ternary conditional
 
| Right-to-left
 
| Right-to-left
 
|-
 
|-
 
! rowspan=11| 16
 
! rowspan=11| 16
| style="border-bottom-style: none" | <code>=</code>
+
| style="border-bottom-style: none; text-align: center" | <code>=</code>
 
| style="border-bottom-style: none" | Direct assignment
 
| style="border-bottom-style: none" | Direct assignment
 
| style="vertical-align: top" rowspan="11" | Right-to-left
 
| style="vertical-align: top" rowspan="11" | Right-to-left
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>+=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>+=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by sum
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by sum
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>-=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>-=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by difference
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by difference
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>*=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>*=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by product
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by product
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>/=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>/=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by quotient
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by quotient
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>%=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>%=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by remainder
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by remainder
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&lt;&lt;=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>&lt;&lt;=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise left shift
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise left shift
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&gt;&gt;=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>&gt;&gt;=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise right shift
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise right shift
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>&amp;=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>&amp;=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise AND
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise AND
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code>^=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code>^=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise XOR
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise XOR
 
|-
 
|-
| style="border-bottom-style: none; border-top-style: none" | <code><nowiki>|</nowiki>=</code>
+
| style="border-bottom-style: none; border-top-style: none; text-align: center" | <code><nowiki>|</nowiki>=</code>
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise OR
 
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise OR
 
|-
 
|-
 
! 18
 
! 18
 
<small>lowest</small>
 
<small>lowest</small>
| <code>,</code>
+
| style="text-align: center" |<code>,</code>
 
| Expression list (executes in sequence, returns value of the last)
 
| Expression list (executes in sequence, returns value of the last)
 
| Left-to-right
 
| Left-to-right
 +
|}
 +
 +
 +
=== Evaluator functions ===
 +
Following functions are available within evaluator expressions.
 +
{| class="wikitable"
 +
! 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
 +
|-
 
|}
 
|}

Latest revision as of 14: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