Expression Evaluator
Constant expressions are supported by NBC for many statement arguments as well as variable initialization. Expressions are evaluated by the compiler when the program is compiled, not at run time. The compiler will return an error if it encounters an expression that contains whitespace. "4+4" is a valid constant expression but "4 + 4" is not.
The expression evaluator supports the following operators:
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
^ | exponent |
% | modulo (remainder) |
& | bitwise and |
| | bitwise or |
~ | bitwise xor |
<< | shift left |
>> | shift right |
() | grouping subexpressions |
PI | constant value |
The expression evaluator also supports the following compile-time functions:
tan(x), sin(x), cos(x)
sinh(x), cosh(x)
arctan(x), cotan(x)
arg(x)
exp(x), ln(x), log10(x), log2(x), logn(x, n)
sqr(x), sqrt(x)
trunc(x), int(x), ceil(x), floor(x), heav(x)
abs(x), sign(x), zero(x), ph(x)
rnd(x), random(x)
max(x, y), min(x, y)
power(x, exp), intpower(x, exp)
The following example demonstrates how to use a constant expression:
// expression value will be truncated to an integer set val, 3+(PI*2)-sqrt(30)