Grammar

This section provides the formal grammar of AodhML in Extended Backus-Naur Form (EBNF).

Program Structure

program        ::= import* declaration* EOF

import         ::= "import" string_literal ( "{" identifier_list "}" )?
                |  "import" string_literal "as" identifier

declaration    ::= function_decl
                |  type_decl
                |  trait_decl
                |  impl_decl

function_decl  ::= "fn" identifier generic_params? "(" params? ")" ( "->" type )? block

type_decl      ::= "type" identifier generic_params? "=" type_expr

trait_decl     ::= "trait" identifier generic_params? "{" trait_member* "}"

impl_decl      ::= "impl" identifier "for" type "{" function_decl* "}"

Types

type           ::= primitive_type
                |  identifier generic_args?
                |  "[" type "]"
                |  "(" type_list? ")"
                |  "fn" "(" type_list? ")" "->" type
                |  "struct" "{" field_decl* "}"
                |  "enum" "{" variant_decl* "}"

primitive_type ::= "i8" | "i16" | "i32" | "i64"
                |  "u8" | "u16" | "u32" | "u64"
                |  "f32" | "f64"
                |  "bool" | "String" | "void"

generic_params ::= "<" identifier_list ">"
generic_args   ::= "<" type_list ">"

field_decl     ::= identifier ":" type ","
variant_decl   ::= identifier ( "(" type_list ")" )? ","

Expressions

expression     ::= assignment

assignment     ::= logical_or ( "=" assignment )?

logical_or     ::= logical_and ( "||" logical_and )*
logical_and    ::= equality ( "&&" equality )*
equality       ::= comparison ( ( "==" | "!=" ) comparison )*
comparison     ::= additive ( ( "<" | ">" | "<=" | ">=" ) additive )*
additive       ::= multiplicative ( ( "+" | "-" ) multiplicative )*
multiplicative ::= unary ( ( "*" | "/" | "%" | "@" ) unary )*

unary          ::= ( "!" | "-" ) unary | postfix

postfix        ::= primary ( "(" args? ")" | "." identifier | "[" expression "]" | "::" identifier )*

primary        ::= literal
                |  identifier
                |  "(" expression ")"
                |  block
                |  "if" expression block ( "else" ( block | "if" expression block ) )?
                |  "for" identifier "in" expression block
                |  "while" expression block
                |  "match" expression "{" match_arm* "}"
                |  "return" expression?
                |  array_literal
                |  struct_literal

literal        ::= integer | float | string | boolean
array_literal  ::= "[" expression_list? "]"
struct_literal ::= identifier "{" field_init* "}"

Statements

statement      ::= expression_statement
                |  let_statement
                |  return_statement

expression_stmt ::= expression

let_statement  ::= "let" "mut"? identifier ( ":" type )? "=" expression

return_stmt    ::= "return" expression?