Overview
Entry Point
Every Maxon program must have a main() function that returns ExitCode:
function main() returns ExitCode return 0end 'main'The return value becomes the program’s exit code (0-255 on Windows).
File Structure
- One or more function, type, enum, or typealias declarations
- Namespace derived from file path
- Use
exportkeyword for cross-file visibility (applies to functions, types, enums, and typealiases) - Use
modulekeyword for directory-scoped visibility (visible to files in the same directory and subdirectories)
Conditional Compilation
Maxon supports #if, #else, and #endif directives for platform-conditional code. These are evaluated at parse time based on the compilation target.
Target OS:
#if os(Windows) let separator = "\\"#else let separator = "/"#endifTarget Architecture:
#if arch(x64) let pointerSize = 8#else let pointerSize = 8#endifSupported conditions:
os(Windows),os(Linux),os(Macos),os(Wasi)— match the target operating systemarch(x64),arch(arm64),arch(wasm32)— match the target CPU architecturetesting(true),testing(false)— match whether the code is compiled in test mode
Boolean operators (precedence: or < and < not), plus parentheses for grouping:
#if not os(Windows) // runs on non-Windows targets#endif
#if os(Linux) or os(Macos) // runs on Linux and macOS#endif
#if os(Linux) and arch(arm64) // runs on ARM Linux only#endif
#if (os(Linux) or os(Macos)) and arch(x64) // runs on Linux or macOS, but only on x64#endifConditional compilation directives can appear at:
- Top level (around functions, types, variables)
- Inside function bodies (around statements)
- Inside
type,enum,union,interface, orextensionbodies (around members)
Nested #if blocks are supported.
Lexical Elements
Comments
// Line comment
/* Block comment */
/* Multi-line block comment*/Identifiers
Identifiers name variables, functions, types, and other declarations.
- Starts with a letter or underscore: Cannot start with a digit, since the compiler uses the first character to distinguish names from number literals.
- Alphanumeric content: After the first character, letters (
a-z,A-Z), digits (0-9), and underscores (_) are allowed. - Case-sensitive:
myVarandMyVarare different identifiers. - Cannot be keywords: Reserved words like
if,for,returncannot be used as identifiers.
identifier = [a-zA-Z_][a-zA-Z0-9_]*Keywords
and, as, bool, break, byte, continue, cstring, default, else, end, enum, export,extends, extension, fallthrough, false, float, for, from, function, gives, if,ignore, implements, in, int, interface, is, let, match, not, of, or, otherwise,return, returns, self, Self, shl, shr, static, then, throw, throws, to,true, try, type, typealias, upto, uses, var, where, while, with, xormodule is a contextual keyword — it is recognised as a visibility modifier only when it appears immediately before a declaration token (function, type, enum, var, let, etc.). In any other position it is a regular identifier, so user code can still use module as a parameter or local variable name.
Literals
Integer Literals
Integer literals are 64-bit signed values (range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).
Decimal:
42-1709223372036854775807 // INT64_MAXHexadecimal (prefix 0x):
0xff0x1a2b0x0000000140000000 // Values above 32-bit rangeBinary (prefix 0b):
0b10100b11111111Octal (prefix 0o):
0o7770o52Underscore separators for readability:
1_000_0000xff_ff0b1111_00000x0000_0001_4000_0000 // Large hex with separatorsByte Values (cast through a ranged byte typealias)
typealias Octet = byte(0 to u8.max)42 as Octet0xff as OctetFloat Literals (must contain decimal point)
3.14-2.50.01.0Character Literals (grapheme clusters in single quotes)
'A' // ASCII character (1 byte)'é' // Latin with accent (2 bytes)'中' // CJK character (3 bytes)'🎉' // Emoji (4 bytes)'\n' // Escape sequence (newline)'\t' // Escape sequence (tab)'\\' // Escape sequence (backslash)'\'' // Escape sequence (single quote)'\x41' // Hex escape (character 'A')'\u00A0' // Unicode escape (NBSP)'\u03A3' // Unicode escape (Greek sigma Σ)Character literals create a character type value, which represents an Extended Grapheme Cluster (EGC).
The character type may contain multiple UTF-8 bytes.
When a single-codepoint character literal appears in a binary operation with an integer operand, the compiler automatically converts it to its Unicode codepoint value:
var cp = 45if cp == '-' 'check' // '-' is coerced to 45 var digit = cp - '0' // '0' is coerced to 48end 'check'String Literals (double-quoted, null-terminated)
"Hello, World!""Line1\nLine2""Tab\there""Quote: \"text\"""\x48\x69" // Hex escape ("Hi")"hello\u0021" // Unicode escape ("hello!")Escape sequences: \n \t \r \0 \\ \" \{ \} \xNN \uXXXX
String Interpolation (embed expressions in strings)
var name = "World"print("Hello, {name}!") // "Hello, World!"
var x = 5print("{x} * 2 = {x * 2}") // "5 * 2 = 10"
print("Pi: {3.14159}") // "Pi: 3.14159"print("Active: {true}") // "Active: true"
// Escape braces with backslashprint("Use \{expr\} syntax") // "Use {expr} syntax"Any expression can be embedded. Built-in types (int, float, bool) are automatically converted to strings. Custom types must implement the Stringable interface.
Format Specifiers (control output formatting)
Append a format specifier after a colon inside the interpolation braces: {expr:spec}
Integer format specifiers: [0][width][type]
0— pad with zeros instead of spaceswidth— minimum output width (right-aligned)type—ddecimal (default),xlowercase hex,Xuppercase hex,bbinary,ooctal
var n = 42print("{n:04}") // "0042" — zero-pad to width 4print("{n:6}") // " 42" — space-pad to width 6print("{n:x}") // "2a" — lowercase hexprint("{n:04X}") // "002A" — zero-padded uppercase hex
var neg = -42print("{neg:06}") // "-00042" — sign comes before paddingFloat format specifiers: [0][width][.precision]
0— pad with zeros instead of spaceswidth— minimum total output width (right-aligned).precision— number of decimal places (max 20)
var f = 3.14159print("{f:.2}") // "3.14" — 2 decimal placesprint("{f:.4}") // "3.1416" — 4 decimal places (rounded)print("{f:8.2}") // " 3.14" — width 8, 2 decimal placesCustom types can implement FormattedStringable to support format specifiers:
interface FormattedStringable function toString(format String) returns Stringend 'FormattedStringable'Byte String Literals (create a ByteArray from a string)
let bytes = b"hello" // ByteArray containing [104, 101, 108, 108, 111]let empty = b"" // Empty ByteArraylet escaped = b"line\n" // Supports escape sequenceslet raw = b"\xFF\x00" // Hex escape: raw bytes [255, 0]Byte string literals use the b"..." prefix to create a ByteArray (Array with Byte) directly from a string. They support the same escape sequences as regular string literals, including \xNN hex escapes for arbitrary byte values (0x00-0xFF) and \uXXXX Unicode escapes. This is useful when working with raw bytes or APIs that expect byte arrays.
Boolean Literals
truefalse