Types¶
SLang is statically typed. Every declaration states its type, and the semantic analyzer requires exact type agreement for initialization, assignment, function arguments, and returns.
| Type | Example | Purpose |
|---|---|---|
int |
42 |
integer values |
double |
3.14 |
floating-point values |
boolean |
true |
truth values |
character |
'A' |
one character (the empty literal '' is also accepted) |
string |
"hello" |
text |
void |
— | functions with no result |
Declarations and assignment¶
int count = 7;
double ratio = 2.5;
boolean ready = true;
character grade = 'A';
string message = "hello";
Scalar variables must be initialized. Multiple scalar declarators of the same type may share a declaration.
int left = 10, right = 20;
There are no implicit conversions. Assigning an int to a double is rejected:
double value = 1; # error
double value = (double) 1; # valid
Explicit casts¶
| Source | Targets |
|---|---|
int |
double, character, string |
character |
int, string |
double |
string |
boolean |
string |
Cast syntax is (target_type) value. The operand is currently restricted to an identifier, integer literal, double literal, or character literal, and casts are accepted in scalar initializers.
int code = 65;
character letter = (character) code;
string text = (string) code;
Arrays¶
Arrays are one-dimensional and have an element type:
int size = 4;
int values[size];
values[0] = 10;
values[1] = values[0] + 5;
The size must be a positive int literal or identifier. An initializer list may be supplied:
int values[3] = {10, 20, 30};
Array access uses an integer literal or identifier as its index. Multidimensional arrays and general index expressions such as values[i + 1] are not in the current grammar.