Heather syntax¶
Info
Work in progress.
How a "hello world" looks like in Heather (main.hat):
1 | |
Heather has no need for ; to separate between statements or , to separate between items. , \t, \n, ; and , are all treated as whitespaces and are ignored. You may use them if you prefer though. Because of Heather's regular syntax, statements and expressions can be organized in what fits better the programmer's or a particular project style. We expect that it can provide some freedom for people to experiment and define what works better for a particular situation, rather than enforcing that without a real reason. Comments are: // comment goes here for line comments, and /* comment body goes inside here */ for multiline comments.
1. Constants¶
Constants are defined inside consts.hat files. They have the following syntax:
1 | |
Examples¶
math/consts.hat:
1 2 | |
io/consts.hat:
1 | |
Importing¶
You can import constants in functions kinds and other constants files.
For single imports:
1 | |
For multiple imports:
1 2 3 4 | |
For multiple imports inside a single const:
1 2 3 4 5 6 | |
2. Types¶
Types are defined inside custom files located on src/hat_types/ path. There are basically two types of user types: structs and enums. A quantum type can contain classical types as its members/named values, but the opposite is not valid.
Structs¶
Definition: a composite data structure, holding a collection of members (or fields) that may have different data types.
Syntax¶
1 | |
Examples¶
Defining struct type:
1 | |
Using struct type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Importing:
1 2 | |
1 2 3 4 5 | |
1 2 3 4 5 6 7 8 9 10 | |
Enums¶
Definition: a tagged union (or enumerated) data structure that can contain a collection of named values, those being simple identifiers or structs, but only one can be in use at any one time. By convention, identifiers are all caps.
Syntax¶
1 | |
Examples:¶
Defining enum type:
1 2 3 4 5 6 7 8 | |
Using enum type:
1 2 3 4 5 6 7 8 9 10 11 12 | |
3. Functions¶
Syntax:¶
1 | |
Return has a special syntax sugar: :: prefixed on an expression.
Examples¶
Defining a type:
1 | |
1 2 3 4 5 6 | |
4. Meta-funcions¶
Definition: a function that defines the code behavior assigned to it. There are three kinds of meta-functions: option (or cases), body (or blocks), and option-body (or case-blocks). They have the types associated with them as optn_t, bdn_t and optbdn_t respectively.
Syntax:¶
1 | |
Examples¶
if meta-function (option type):
Defining:
1 | |
Calling:
1 | |
pipe meta-function (body type):
Defining:
1 | |
Calling:
1 | |
match meta-function (option-body type):
Defining:
1 | |
Calling:
1 2 3 4 | |
5. Modifiers¶
Definition: a function that can change the semantics and properties of its holder.
Syntax¶
1 | |
the <type> must be the same of self.
Examples¶
& (reference) modifier
1 2 3 | |
1 2 | |
6. Cast¶
Definition: a type of reflective function and semantics that convert one data into another type. If data is eagerly evaluated (strict), it should convert to target type calling a given function with given cast name and the appropriate argument types signature. If data is lazily evaluated (lazy), it will evoke its evaluation and then the result value to be cast into the target type. It has a syntax sugar * when called, as in: data * type, meaning "cast data into type".
Syntax¶
1 | |
Examples¶
Casting strict data:
1 2 3 | |
Casting lazy data:
1 2 3 4 5 6 | |