$declare(name_of_the_variable)
my_var1 : the_type; // undefined value
my_var2 : the_type := the_init_value; // well-defined init value, explicitly-typed
my_var3 ::= the_init_value; // well-defined init value, type exactly matches that of the init value
NOTE: You must declare a function only once throughout the entire program. Pre-declarations are unnecessary.
// function declaration of 'my_func_name'
my_func_name : func(param0 : type0, param1 : type1 -> return_type)
{
// function pointer local variable. zero-initialised (better not call it!)
my_function_ptr : func(-> v0) := zero;
};
Integer literals have intuitive syntax. They are of type s64 weak. Example:
my_favourite_number : s64 weak := 862;
Floating-point literals have intuitive syntax. They are of type f64 weak. Example:
the_funny_number : f64 weak := 69.420;
String literals in Sy are not nul-terminated by default. If you want a nul-terminating string, you must add the terminator yourself. For example:
my_assert(my_zstrlen("foo") == 3); // false
my_assert(my_zstrlen("foo\0") == 3); // true
Sy is a strongly-typed language, because data type rules are strictly enforced. All implicit conversions are banned by default, though there is a type qualifier weak you can use to opt-into some.
A type consists of a base type (this is either a primitive, enum, struct, array or pointer) and zero or more qualifiers.
| Sy Type | C Equivalent |
|---|---|
v0 |
void |
u64 |
uint64_t |
u32 |
uint32_t |
u16 |
uint16_t |
u8 |
uint8_t |
s64 |
int64_t |
s32 |
int32_t |
s16 |
int16_t |
s8 |
int8_t |
f64 |
double |
f32 |
float |
bool |
bool |
bool values are guaranteed to be 1-byte in size on all platforms.Enum types represent a fixed-size set of integral constants. All enum types are compiled down to 64-bit signed integers (i.e s64 under-the-hood). An example enum declaration might look like this:
colour : enum
{
.blue := 0;
.red := 1;
.green := 2;
};
my_favourite_colour ::= colour.red; // type: colour
Struct types represent a group of related data types into one place for easy usage. Memory layout rules of a struct are ABI-specific, but they are largely identical to that of C-based languages. An example struct declaration might look like this:
wallet : struct
{
total_pennies : u64;
card_count : u64;
card_values : u64 mut?;
appearance : colour;
};
my_wallet ::= wallet
{
.total_pennies := 10000;
.card_count := 0;
.card_values := zero;
.appearance := colour.green;
};
Array types consist of a base-type (which could itself be an array type) followed by an array length. The syntax is extremely similar to C-based languages. An example array might look like this:
fibonacci_numbers : u64 mut[8];
[fibonacci_numbers # 0] = 1;
[fibonacci_numbers # 1] = 1;
[fibonacci_numbers # 2] = 2;
// and so on...
Pointer types consist of a base-type (which could itself be an array type) followed by a ? symbol. An example array might look like this:
a_number : u64 := 80;
the_const_pointer : u64? := ref a_number;
the_mutable_pointer : u64 mut? := ref a_number;
[the_mutable_pointer] = 81;
Any type can have any permutation/combination of qualifiers.
| Sy Qualifier | Description | C Equivalent |
|---|---|---|
mut |
value can be changed (mutable) | not-const (everything is const by default) |
static |
value is a compile-time constant (statically-known) | constexpr |
weak |
type undergoes implicit conversions | N/A (all types in C are equivalent to weak) |
abi_sysv |
function type will use the System V ABI | N/A |
Some example of qualified types look like:
a : u64 mut;
b : u64 mut weak?;
b : u8 mut? mut? mut[100];
s : s64 static;