Expressions
Precedence
From lowest to highest:
Precedence |
Operators |
Associativity |
|---|---|---|
1 |
|
right |
2 |
|
left |
3 |
|
left |
4 |
|
left |
5 |
|
left |
6 |
|
left |
7 |
|
left |
8 |
|
left |
9 |
|
left |
10 |
|
left |
11 |
|
left |
12 |
unary |
right |
13 |
postfix |
left |
Expression typing
Niblick has no C integer promotions. The result type of an expression comes from its operands, its expected destination type, and explicit casts.
Result width
Expression |
Result |
|---|---|
Binary arithmetic and bitwise |
Wider operand width |
Shift |
Left operand width |
Comparison |
Common operand type |
Logical |
|
Unary |
Operand width |
Expected-type width floor
An expected destination type sets a minimum evaluation width.
Context |
Effect |
|---|---|
Assignment, initializer, argument, return, array element, struct field |
Destination type sets the minimum width |
Comparison |
Common operand type sets the minimum width |
Arithmetic, bitwise, shift, unary node |
Minimum width propagates through the expression |
Condition without a comparison |
No expected type |
Explicit cast |
Sets the evaluation width and overrides the destination floor |
Narrowing |
Requires an explicit cast |
Signedness |
Unchanged by the width floor |
uint16_t n = 1 << 8; // 0x100
uint32_t m = 1 << 20; // 0x100000
int16_t r = -200; // sign-extends the literal
bool ok = x + y < 200; // comparison uses a 16-bit common type
uint16_t narrow = (uint8_t)(1 << 8); // 0
Common type
Operand signedness |
Common type |
|---|---|
Both signed |
Signed at the wider width |
Both unsigned |
Unsigned at the wider width |
Mixed, unsigned operand narrower |
Signed at the wider width |
Mixed, unsigned operand equal or wider |
Unsigned at the wider width |
Multiplication result
Operand type |
Unsigned result |
Signed result |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Operators
Binary and ternary
Operators |
Result |
Meaning |
|---|---|---|
|
Integer |
Arithmetic |
|
Integer |
Bitwise |
|
Left operand type |
Shift |
|
|
Comparison |
|
|
Logical; short-circuit |
|
Common branch type |
Conditional |
|
Element type |
Array index |
|
Field type |
Struct field |
|
Function return type |
Named function call |
Unary
Operator |
Result |
Meaning |
|---|---|---|
|
Operand type |
No-op |
|
Operand type |
Two’s-complement negation |
|
Operand type |
Bitwise complement |
|
|
Logical negation |
|
Nibble address or |
Address of an array |
Integer overflow, division, modulo, and shift behavior are in Semantics.
Assignments
Form |
Requirement |
Behavior |
|---|---|---|
|
Variable, array element, or struct field |
Convert and store |
|
Variable, array element, or struct field |
Read, combine, store |
|
Statement |
Increment or decrement |
|
Statement |
Same effect as postfix |
Constraint |
Rule |
|---|---|
Index or member base with a call |
Rejected |
Lvalue evaluation |
Once for the read and once for the write |
Expression result |
None; these forms are statements |
x += 2;
x <<= 1;
a[i] += 3;
p.field -= 1;
x++;
--x;
Conditional expressions
Rule |
Value |
|---|---|
Condition evaluation |
Once |
Branch execution |
Selected branch only |
Branch checking |
Both branches type-checked |
Result type |
Common branch type with the destination width floor |
|
Normalized to 0 or 1 |
Mixed |
Explicit cast required |
|
Rejected |
Constant condition |
Folded at compile time |
Constant condition contexts |
Scalar |
value = cond ? a : b;
value = a ? b : c ? d : e; // a ? b : (c ? d : e)
Address-of
Form |
Result |
|---|---|
|
Nibble address |
|
|
|
Rejected |
Pointer arithmetic |
Rejected |