Chapter 13
Appendices
A. Full EBNF Grammar
Programs and Items
program ::= [ mode_directive ] { item }
item ::= function | struct_decl | enum_decl | trait_decl
| impl_block | test_block | statement
mode_directive ::= "#mode" ("casual" | "strict" )
function ::= "fn" IDENT "(" [ param_list ] ")" [ "->" type_expr ]
{ contract } block
param_list ::= param { "," param }
param ::= [ "..." ] IDENT ":" type_expr [ "=" expression ]
contract ::= "require" expression [ "," STRING ]
| "ensure" closure [ "," STRING ]
struct_decl ::= "struct" IDENT "{" field_decl { "," field_decl } "}"
field_decl ::= IDENT ":" type_expr
enum_decl ::= "enum" IDENT "{" variant { "," variant } "}"
variant ::= IDENT [ "(" type_expr { "," type_expr } ")" ]
trait_decl ::= "trait" IDENT "{" { trait_method } "}"
trait_method ::= "fn" IDENT "(" [ param_list ] ")" [ "->" type_expr ]
impl_block ::= "impl" IDENT "for" IDENT "{" { function } "}"
test_block ::= "test" STRING block
Types
type_expr ::= [ phase_prefix ] type_name
| "[" type_expr "]"
phase_prefix ::= "~" | "*"
type_name ::= "Int" | "Float" | "Bool" | "String" | "Array"
| "Map" | "Tuple" | "Set" | "Fn" | "Channel"
| "Range" | IDENT
Statements
statement ::= binding | assignment | destructure | return_stmt
| break_stmt | continue_stmt | defer_stmt
| import_stmt | expr_stmt
binding ::= phase_kw IDENT [ ":" type_expr ] "=" expression
phase_kw ::= "flux" | "fix" | "let"
assignment ::= lvalue "=" expression
| lvalue compound_op expression
lvalue ::= IDENT | expression "." IDENT | expression "[" expression "]"
compound_op ::= "+=" | "-=" | "*=" | "/=" | "%="
| "&=" | "|=" | "^=" | "<<=" | ">>="
destructure ::= phase_kw "[" array_pat "]" "=" expression
| phase_kw "{" struct_pat "}" "=" expression
array_pat ::= IDENT { "," IDENT } [ "," "..." IDENT ]
struct_pat ::= IDENT { "," IDENT }
return_stmt ::= "return" [ expression ]
break_stmt ::= "break"
continue_stmt ::= "continue"
defer_stmt ::= "defer" block
import_stmt ::= "import" STRING [ "as" IDENT ]
| "import" "{" import_list "}" "from" STRING
import_list ::= IDENT { "," IDENT }
Expressions
expression ::= nil_coalesce
nil_coalesce ::= or_expr { "??" or_expr }
or_expr ::= and_expr { "||" and_expr }
and_expr ::= bit_or { "&&" bit_or }
bit_or ::= bit_xor { "|" bit_xor }
bit_xor ::= bit_and { "^" bit_and }
bit_and ::= equality { "&" equality }
equality ::= comparison { ("==" | "!=" ) comparison }
comparison ::= shift { ("<" | ">" | "<=" | ">=" ) shift }
shift ::= range_expr { ("<<" | ">>" ) range_expr }
range_expr ::= addition [ ".." addition ]
addition ::= multiply { ("+" | "-" ) multiply }
multiply ::= unary { ("*" | "/" | "%" ) unary }
unary ::= ("-" | "!" | "~" ) unary | postfix
postfix ::= primary { postfix_op }
postfix_op ::= "." IDENT [ "(" [ arg_list ] ")" ]
| "?." IDENT [ "(" [ arg_list ] ")" ]
| "[" expression "]"
| "?[" expression "]"
| "(" [ arg_list ] ")"
| "?"
primary ::= INT | FLOAT | STRING | "true" | "false" | "nil"
| IDENT [ "::" IDENT [ "(" [ arg_list ] ")" ] ]
| IDENT "{" field_init { "," field_init } "}"
| "(" expression [ "," expression { "," expression } ] ")"
| "[" [ expression { "," expression } ] "]"
| closure | if_expr | match_expr
| for_expr | while_expr | loop_expr
| forge_expr | scope_expr | spawn_expr | select_expr
| try_catch | freeze_expr | thaw_expr | clone_expr
| anneal_expr | sublimate_expr | crystallize_expr
| print_expr | "..." expression
| block
closure ::= "|" [ closure_params ] "|" ( expression | block )
closure_params ::= closure_param { "," closure_param }
closure_param ::= [ "..." ] IDENT [ "=" expression ]
block ::= "{" { statement } [ expression ] "}"
arg_list ::= expression { "," expression }
field_init ::= IDENT ":" expression
Match and Select
match_expr ::= "match" expression "{" { match_arm } "}"
match_arm ::= [ phase_qual ] pattern [ "if" expression ] "=>" ( expression | block ) [ "," ]
phase_qual ::= "fluid" | "crystal"
pattern ::= INT | ["-" ] INT | FLOAT | ["-" ] FLOAT
| STRING | "true" | "false" | "nil"
| "_" | IDENT
| INT ".." INT
select_expr ::= "select" "{" { select_arm } "}"
select_arm ::= IDENT "from" expression "=>" block [ "," ]
| "default" "=>" block [ "," ]
| "timeout" "(" expression ")" "=>" block [ "," ]
Lexical Grammar
IDENT ::= (letter | "_" ) { letter | digit | "_" }
INT ::= digit { digit }
FLOAT ::= digit { digit } "." digit { digit }
STRING ::= '"' { str_char | escape | interp } '"'
| "'" { str_char | escape } "'"
| '"""' { any | interp } '"""'
interp ::= "${" expression "}"
escape ::= "\n" | "\t" | "\r" | "\0" | "\\" | "\""
| "\'" | "\$" | "\x" hex hex
comment ::= "//" { any_except_newline }
| "/*" { any | comment } "*/"
B. Keyword Table
# Keyword Category Description
1 fluxBinding Declare mutable variable
2 fixBinding Declare immutable variable
3 letBinding Declare variable (inferred phase)
4 freezePhase Transition to crystal
5 thawPhase Transition to fluid
6 forgePhase Controlled mutation block
7 clonePhase Deep copy a value
8 annealPhase Thaw, transform, re-freeze
9 crystallizePhase Scoped crystallization
10 sublimatePhase Transition to sublimated
11 fnDeclaration Function declaration
12 structDeclaration Struct declaration
13 enumDeclaration Enum declaration
14 traitDeclaration Trait declaration
15 implDeclaration Implementation block
16 testDeclaration Test block
17 ifControl Conditional expression
18 elseControl Alternative branch
19 forControl For-in loop
20 inControl Iterator keyword
21 whileControl While loop
22 loopControl Infinite loop
23 matchControl Pattern matching
24 returnJump Return from function
25 breakJump Exit loop
26 continueJump Next loop iteration
27 scopeConcurrency Structured concurrency block
28 spawnConcurrency Launch concurrent task
29 tryError Exception handling block
30 catchError Error handler
31 deferError Deferred execution
32 importModule Import module
33 fromModule Selective import source
34 asModule Import alias
35 trueLiteral Boolean true
36 falseLiteral Boolean false
37 nilLiteral Nil value
38 printBuilt-in Output to stdout
C. Operator Precedence
Operators are listed from lowest precedence (loosest binding) to highest precedence
(tightest binding). Operators on the same level have equal precedence.
Level Operators Associativity Description
1 ??Left Nil coalesce
2 ||Left Logical OR
3 &&Left Logical AND
4 |Left Bitwise OR
5 ^Left Bitwise XOR
6 &Left Bitwise AND
7 == !=Left Equality
8 < > <= >=Left Comparison
9 << >>Left Bit shift
10 ..None Range
11 + -Left Addition, subtraction
12 * / %Left Multiplication, division, modulo
13 - ! ~Right (prefix) Negation, logical NOT, bitwise NOT
14 . ?. [] ?[] () ?Left (postfix) Access, call, try-propagate
D. Implementation Limits
The reference implementation (C, bytecode VM) has the following practical limits:
Limit Value
Integer range -263 to 263 -1 (64-bit signed)
Float precision IEEE 754 double (64-bit)
Max function parameters 255
Max local variables per scope 256
Max constants per chunk 256 (8-bit index)
Max call stack depth 256 frames
Max upvalues per closure 256
String length Limited by available memory
Array length Limited by available memory
← Previous: Standard Library