Backus-Naur form
Details on the BNF
Program Structure
A Frost program consists of the following components:
<comment_block>
: Comments enclosed in%%
markers. These are ignored by the parser and provide documentation or notes.<import_block>
: Statements to import external modules or dependencies.<function_block>
: Function definitions that include the function signature, optional arguments, and a block of statements.
Example:
Include Statements
You can include external modules using import
statements:
<import_block>
: Specifies the inclusion of an external module.<string_literal>
: The name of the module, enclosed in quotes.
Example:
Functions
Functions in Frost are defined as follows:
<function_block>
: A function definition that includes:<function_signature>
: Declares the function name, parameter types, and return type.Optional arguments: A space-separated list of parameter names.
<block>
: A code block containing the function's statements.
<function_signature>
: Specifies the name, parameter types, and return type of the function.<identifier>
: The name of the function or its parameters.
Example:
Data Types
Frost supports a variety of data types, including primitives, pointers, and composite types:
<type>
:Primitive types:
int
,float
,double
,char
,byte
,bool
,never
.Pointer types: Denoted as
*<type>
.Struct types: Defined using
struct
with fields.User-defined types: Custom types defined by the user.
<struct_field>
: Fields in a struct, defined as<identifier> -> <type>
.
Example:
Statements
A Frost program consists of individual statements that define the program's behavior. These include:
<variable_declaration>
: Declares and initializes variables.<loop>
: Iterative constructs.<if_statement>
: Conditional constructs.<function_call>
: Calls to functions.<expression>
: Mathematical, logical, or function-related expressions.<return_statement>
: Specifies the value returned by a function.
Example:
Expressions
Expressions in Frost represent calculations, operations, or data manipulation. They include:
<literal>
: Constants like numbers, strings, booleans, or null.<binary_expression>
: Expressions involving binary operators (e.g.,a + b
).<unary_expression>
: Expressions with prefix or postfix operators.<function_call>
: Invocations of functions.<type_conversion>
: Explicit type conversions.<array_access>
: Accessing elements of arrays.<struct_access>
: Accessing fields of structs.<assignment>
: Assigning values to variables.
Example:
Literals
Literals represent fixed values in Frost, such as numbers, strings, and booleans:
<int_literal>
: Integer values, e.g.,42
.<float_literal>
: Floating-point values, e.g.,3.14
.<byte_literal>
: A single byte enclosed in single quotes, e.g.,'A'
.<bool_literal>
: Boolean values,true
orfalse
.<array_literal>
: Arrays of literals or strings.<string_literal>
: Strings enclosed in double quotes.<literal_array_literal>
: Arrays enclosed in square brackets.
Example:
Control Flow
Control flow constructs allow conditional and iterative execution:
<if_statement>
: Conditional statements with optionalelse
clauses.<loop>
: Repeatedly executes a block of code while a condition holds true.<for_loop>
: Structured iteration over a range with a specific step size. Following the format:from
<start>
to<end>
by<step>
: Specifies the range and step size.|<var>: <type>|
: Declares the loop variable and its type.<block>
: Contains the statements executed in each iteration.
Example:
Comments
Comments in Frost are enclosed in %%
markers. They are ignored by the parser and provide documentation for the code.
Example:
Last updated