Syntax
Note
The syntax is in development and will have more features added continuously.
The H-hat's Heather dialect syntax works as follows:
1. Main¶
There is a main file that will be used for program execution. Its name can be anything, but it must contain a main keyword with brackets:
1 | |
Code to be executed must live inside main body, e.g. anything inside the brackets will be executed.
2. Comments¶
1 2 3 4 5 6 | |
3. Variables¶
-
Variable declaration:
All quantum literals, types, functions, variables (and so on) start with1 2 3
var:some-type // for classical data @var:@some-type // for quantum data@. This is the universal identifier for quantum-related things. -
Variable assignment:
1 2 3 4 5 6 7
// classical var1:some-type = value // declare+assign var2 = value // assign // quantum @var1:@some-type = @value // declare+assign @var2 = @value // assign
4. Calls¶
1 2 3 4 | |
-
Multiple-argument call arguments can be separated by any Heather-defined whitespaces
-
Calls with named argument will have the
argument-namefollowed by colon:and its value, e.g.arg:val -
Classical variable assignment:
1 2
var:some-type = data // assign value var = other-data // assign a new data- Assigning data more than once to a classical variable may be possible if it is mutable. More on that at the language core system page. If the variable is immutable, an error will happen.
-
Quantum variable assignment:
1 2 3
@var:@some-type = @first_value // assign the first value @fn(@var) // @fn will be appended to @var data @other-fn(@var params) // @other-fn will be appended next- A quantum data is an appendable data container, that is a data container that appends instructions applied to it in order. In the case above, the content of
@varwill be an array of elements:[first_value, @fn(%self), @other-fn(%self params)]that will be transformed and executed in order. More on what appendable data container is at the language core system page.
- A quantum data is an appendable data container, that is a data container that appends instructions applied to it in order. In the case above, the content of
5. Casting¶
1 2 3 4 5 | |
literal*type or variable*type. In a similar fashion when declaring a variable one uses variable:type, with a different syntax sugar, *, to connect the data with the type.
6. Types¶
There are 4 types of type definitions: single, struct, enum and union. Below you can find how to define them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Some key aspects of each type:
-
Single types can be thought as a "label" for a given type, but it has its own properties and checks
-
Structs are always defined by members with a name and type
-
Enums can be either identifiers or structs
-
Unions have the
unionkeyword before the body and can hold members with name and type, structs or enums; they behave like usual unions in C, for instance
7. Functions¶
1 | |
-
The
fnkeyword followed by the function name,sum, followed by the arguments between parenthesis,aandbof typeu32, followed by the function type,u32, followed by the function body between brackets,=add(a b). If the function has no return value, thenulltype can be left empty. -
The last expression to be return must contain a
=syntax sugar to indicate it is the "return" expression.
8. Conditional statements (if)¶
1 2 3 4 5 | |
An if statement is a call with options: each option is one condition with its result. If the result contains multiple expressions, it must be inside a bracket body. The last condition can be a true option, representing the else clause in other programming languages, or a fallback default.
9. Pattern matching (match)¶
1 2 3 4 5 | |
In a similar fashion to if, pattern matching match will have options containing their respective instructions. However, match requires a variable, function call, literal, etc. to be matched against. This something is placed inside parenthesis after match and a bracket body is defined with the options.
10. Modifiers¶
1 2 3 4 5 | |
Modifiers provide an extensive way to complement, define or modify the data it is attached to, a literal, variable, type, function call, etc.