Files
aji/docs/reference/air-text.ebnf
T
dev 48bb50724f spec: accept RFC 0005 and integrate Phase 1 memory model (P1-1, P1-2)
- RFC 0005 Accepted (revision 2)
- spec 07 §8.8 types and syntax; spec 07a contracts for features
  ownership and arrays; spec 08 §11-§14 borrows, facts, initialization,
  stage order; spec 03 frame storage, second-class borrows, static
  region generations
- grammar: from-returns, multi-result, new types and opcodes
- feature registry: ownership and arrays published; heap added and
  regions moved to Phase 2; borrow_joins reserved and unscheduled
- codes E2105-E2108, T0008
- Q-009, Q-010 resolved; Phase 1 and Phase 2 plans updated; Own test
  moved to phase 2
2026-10-04 13:10:08 +03:00

124 lines
5.7 KiB
EBNF

(* Aji text AIR, format version 0.
Normative together with docs/spec/07-air.md §8 and docs/spec/07a-air-instructions.md.
Lexical conventions
-------------------
- A comment starts with ";" and runs to the end of the line.
- Line breaks are significant: each header line, declaration header,
block label and instruction ends with NL. Line breaks inside "(" ")",
"[" "]" and "{" "}" are ignored.
- Spaces and tabs separate tokens and are otherwise insignificant.
- Opcodes are written as one dotted word, e.g. "add.checked.u64".
Name spaces
-----------
- "%name" SSA value, scoped to its function.
- "@name" function, type or block label. Functions and types share the
module scope; block labels are scoped to their function. A label
reference appears only in branch targets, so it never clashes
with a function name.
- bare field names, variant names, module path segments, effect names.
*)
module = { NL } header { NL } { declaration { NL } } ;
header = "module" path NL
"air" int_lit NL (* AIR text format version; this file defines 0 *)
"lang" version_lit NL (* Aji semantic version, e.g. 0.1 *)
"features" "[" [ path_list ] "]" NL ; (* required feature names; empty in format 0 *)
declaration = type_decl | function ;
type_decl = "type" global "=" ( struct_type | enum_type ) NL ;
struct_type = "struct" "{" [ field { "," field } [ "," ] ] "}" ;
field = ident ":" type ;
enum_type = "enum" "{" variant { "," variant } [ "," ] "}" ;
variant = ident [ "(" [ type { "," type } ] ")" ] ;
function = "fn" global "(" [ param_list ] ")" "->" type [ "from" local ] NL (* from: feature ownership *)
"effects" "[" [ path_list ] "]" NL
block { block } ;
param_list = param { "," param } ;
param = local ":" type ;
block = ident [ "(" [ param_list ] ")" ] ":" NL { instruction NL } ;
(* The first block is the entry block and takes no parameters. *)
instruction = [ local { "," local } "=" ] operation ;
(* More than one result only for instructions specified with several results (slice.split). *)
operation = "const." int_type int_lit
| "const.bool" ( "true" | "false" )
| "copy" local
| arith_op local "," local
| "assert" local
| "cast." cast_mode "." int_type local
| "struct.make" global "(" [ local_list ] ")"
| "struct.get" global "." ident "," local
| "enum.make" global "." ident "(" [ local_list ] ")"
| "call" global "(" [ local_list ] ")"
| ownership_op
| arrays_op
| terminator ;
(* Feature ownership (spec 07a). *)
ownership_op = "local.alloc" type
| "local.store" local "," local
| "local.zero" local
| "local.load" local
| "borrow.ref" local
| "borrow.mut" local
| "borrow.field" global "." ident "," local
| "borrow.end" local
| "load" local
| "store" local "," local ;
(* Feature arrays (spec 07a). *)
arrays_op = "array.splat" type local
| ( "borrow.index" | "borrow.index.proven" ) local "," local
| "slice.make" local
| "slice.len" local
| "slice.sub" local "," local "," local
| "slice.split" local "," local
| "slice.copy" local "," local
| "guard" local ;
arith_op = ( "add" | "sub" | "mul" ) "." ( "checked" | "wrap" ) "." int_type
| ( "div" | "rem" ) ".checked." int_type
| ( "and" | "or" | "xor" ) "." ( int_type | "bool" )
| ( "shl" | "shr" ) "." int_type
| ( "eq" | "ne" ) "." ( int_type | "bool" )
| ( "lt" | "le" | "gt" | "ge" ) "." int_type ;
cast_mode = "checked" | "wrap" | "widen" ;
terminator = "br" target
| "cbr" local "," target "," target
| "switch." int_type local "," { int_lit "->" target "," } "default" "->" target
| "enum.switch" global local "," enum_arm { "," enum_arm } [ "," "default" "->" target ]
| "ret" [ local ]
| "trap" ;
enum_arm = ident "->" global ; (* target block receives the variant payload as parameters *)
target = global "(" [ local_list ] ")" ;
type = int_type | "bool" | "void" | "never" | global
| ( "local" | "ref" | "mut" ) "<" type ">" (* feature ownership *)
| "[" type ";" int_lit "]" (* feature arrays *)
| ( "slice" | "slice_mut" ) "<" type ">" ; (* feature arrays *)
int_type = "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
| "i8" | "i16" | "i32" | "i64" | "i128" | "isize" ;
(* "f32" and "f64" are reserved; see spec 07 §8.4. "byte" is not an AIR type: frontends lower it to u8. *)
local_list = local { "," local } ;
path_list = path { "," path } ;
path = ident { "." ident } ;
local = "%" name_char { name_char } ;
global = "@" ident ; (* no dots inside a global name *)
ident = ( letter | "_" ) { name_char } ;
name_char = letter | digit | "_" ;
int_lit = [ "-" ] ( dec_digits | "0x" hex_digits | "0b" bin_digits ) ;
(* "_" may separate digits; it carries no meaning. *)
version_lit = digit { digit } "." digit { digit } ;