Variables & types

Variables are created by assignment and their type is inferred from the value. Types are static: once a name holds an int it stays an int.

Declaring variables

#include <libstd/stdio>

name = "TryC"            // str
year = 2025              // int
big = 200000000000 as long   // long: literals are int, widen explicitly
ratio = 3.5              // float (IEEE-754 double)
initial = 'T'            // char
flag = 1                 // int used as a boolean

print(name, " ", year, " ", ratio, "\n")
TryC 2025 3.500000

A name assigned at the top level of the file is a global (stored in .data/.bss); a name first assigned inside a function is a local of that function. Reassigning a global from inside a function writes the global, as long as the global is declared earlier in the same file (or was read before being written); otherwise the assignment creates a new local.

Scalar types

TypeSizeLiteralNotes
int4 bytes, signed42, -7, 0x1FThe default integer type.
long8 bytes, signed42 as longNever a pointer outside unsafe:.
short2 bytes, signed—Used in class fields that mirror C structs.
char1 byte, unsigned'a', '\n', '\0'The element type of a string. print shows a char as the character.
bool1 byte0 / 1There are no true/false keywords; comparisons produce 0 or 1.
float8 bytes3.14, 1.0, -2.25Always a double; there is no 32-bit float type.
ref8 bytes0 as refAn opaque pointer to any allocation; the element type of List. See Pointers.
str8 bytes (pointer)"text\n"A pointer to a NUL-terminated byte string. See Strings.
integer literals are int

Every integer literal is a 32-bit int, even when it does not fit: big = 200000000000 silently keeps the low 32 bits. Write 200000000000 as long, or assign to something already typed long.

Integer types widen implicitly when mixed: int + long is a long, int + float is a float. bool, char and short convert freely to and from int in both directions, because every value is loaded sign- or zero-extended into a 64-bit register before being used.

Compound types

TypeExamplePage
Fixed arraynew int[5], parameter type int[3], field valori int[5]Arrays
Dynamic arraynew int[n][m], parameter type int[]Arrays
Classp = new PointClasses
Pointer&x, ptr int, ptr NodePointers
Function pointerop: &(int, int) -> int = sommaFunctions
List, dict[10, 20, 30], {"a": 1}Lists & dicts

Casting with as

expr as T converts a value to type T. Between pointer types it is a reinterpret cast: no instruction is generated, only the type label changes (ref as str, str as ptr char), except ref as Class and str as Class, which check the class id in the header. Between a pointer and an integer the cast is allowed only inside unsafe: (see Pointers); the literal 0 is the exception, a null of any pointer type. Widening an int to a long is the one integer cast that changes the value representation.

n = 5
big = n as long          // int -> long (sign extension)
addr = &n
unsafe:
    raw = addr as long   // pointer -> integer, same bits
    p = raw as ptr int   // integer -> pointer, same bits

Casts involving float are real value conversions, as in C: x as int truncates toward zero, n as float converts the integer.

x = 10.0 / 4.0           // 2.5
print((x as int), "\n")   // 2
print((3 + 2.5), "\n")    // 5.500000: int widened to float
as never truncates integers

long_value as int is refused: the compiler does not narrow integers with as. When you really want the low 32 bits (a value read from a C struct, for example), libstd/type has the conversions that do: long_to_int keeps the low 32 bits, int_to_char the low 8.

Declaring without a value

An opaque handle coming from a C library (an extern class) has nothing to assign yet; name =: Type declares it so that its address can be passed to the library:

#include <libstd/sqlite3>

db =: sqlite3
rc = sqlite3_open("./test.db", &db)

Increment and decrement

i = 0
i++
i--
z = 10
while z > 0:
    z--

x++ and x-- are statements, not expressions, and also work on class fields (node.count++) and array elements.

Constants

There is no const: a named constant is a #define, substituted by the precompiler before the compiler sees the code. Numeric and string values both work.

#define MAX_USERS 64
#define BANNER "== report ==\n"

users = new int[MAX_USERS]
print(BANNER)