Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Expressions

This chapter describes the expression grammar fic actually parses. Expressions appear as:

  • the right-hand side of assignments inside event bodies;
  • the payload of a channel send (c!e);
  • the body of a guard ([b]);
  • arguments to calls, sub-process calls, and computation-event carriers.

One grammar (Expr) covers literals, variables, calls, sizeof, unary and binary operators, field access, and casts. There’s no separate arithmetic vs. logical vs. boolean sub-grammar — a boolean-shaped subset of Expr is what’s required in guard position (see below), not a different grammar.

Expr    ::= Lit | ident | CExpr | 'sizeof' '(' Type ')'
          | '(' Expr ')'                  -- grouping
          | UnOp Expr | Expr BinOp Expr
          | FieldBase '.' ident           -- field access
          | Expr 'as' Type                -- cast
FieldBase ::= ident | CExpr | FieldBase '.' ident  -- var, call, or field chain

Lit     ::= int_lit | hex_lit | bool_lit
                                        -- hex_lit allows '_' (0xFE00_0000); int_lit does not
UnOp    ::= '-' | '!' | '~'
BinOp   ::= '+' | '-' | '*' | '/' | '%'
          | '==' | '!=' | '<' | '<=' | '>' | '>='
          | '&&' | '||'
          | '&' | '|' | '^' | '<<' | '>>'

Operator precedence

Loosest to tightest:

LevelOperators
1||
2&&
3|
4^
5&
6==, !=
7<, <=, >, >=
8<<, >>
9+, -
10*, /, %
11as
12unary -, !, ~
13primary atoms

Names, mappings and fields

A variable is a program or body variable or a system const. A mapping is not a value: m alone is an error, and only m.vaddr and m.paddr are expressions, the two addresses of a mapping. Any other x.f is field access on a struct value (a variable, a call, or a chain of fields). Field access binds only to those: a unary operator does not apply to it directly, so write !(s.ok) and -(s.a), not !s.ok.

A constant subexpression is folded at compile time in the checked arithmetic of its type; a constant that overflows is a compile error, not a wrapped value.

Casts

e as T converts an integer or an address to another integer type or to Address, explicitly: fic never widens or narrows on its own, so m.vaddr + w with w: uint32 is an error and m.vaddr + w as uint is the way to write it. The semantics are Rust’s: widening is lossless, narrowing is modulo 2ⁿ (300 as uint8 is 44, 200 as uint8 as int8 is -56), and a constant cast folds at compile time. as binds tighter than every binary operator and looser than unary: -x as uint is (-x) as uint. A cast to bool, to a struct or to an array is rejected, as is a cast of one.

Call expressions (CExpr)

CExpr ::= ident '.' ident '(' (Expr (',' Expr)*)? ')'

A call expression invokes a function exported by an imported .c, .h or .pnk file, prefixed with the import alias chosen at the import declaration. The alias is required — there are no local callables in Fiducia, only externally-implemented functions reached through an alias.

The first ident is the alias; the second is the function name. No whitespace is permitted around the . or before the (:

eth.send_frame()                  // OK
eth.send_frame(pkt, len)          // OK — arguments are full Expr, comma-separated
drv .start()                      // rejected — whitespace before '.'
drv. start()                      // rejected — whitespace after '.'
drv.start ()                      // rejected — whitespace before '('

Guard expressions

A guard ([b] P) requires a boolean-shaped Expr — arithmetic like [a + b] is rejected at parse time, since a guard must denote a yes/no decision, not a value. Boolean-shaped means: a boolean literal, a variable, a call, a field access, a !-negated boolean-shaped expression, or a comparison/&&/|| combination of boolean-shaped expressions.

[a + b == c]              // OK — comparison of arithmetic is boolean-shaped
[eth.is_forward()]         // OK — call
[!flag && ready]           // OK — negation + conjunction
[true]                     // OK
[a + b]                    // rejected — arithmetic isn't boolean-shaped

A call in a guard must be side-effect free, because the generated C may evaluate a guard more than once. For a Pancake function with a contract, fic --check and --emit=xta reject a call whose contract is impure.