Cesium logo Cesium

The S0 subset

September 01, 2025

S0 is the subset of Cesium that stage 0 (cesium0) compiles. Its one job is to compile stage 1 (cesium1), which is written in S0. It is designed together with core SIR, so that almost every S0 construct maps onto one core SIR construct (#9). This chapter gives its grammar and its rules; the lexical structure is the chapter before, and the introduction has the notation and the sources rules give.

s0.subset (static, draft) S0 is a strict subset of Cesium: every S0 program is a Cesium program with the same meaning, and every later compiler compiles the frozen stage 1. Decided in #9, rule 1. See conf.subset.

s0.purpose (static, draft) A feature is added to S0 only when stage 1 needs it and implementing it in stage 0 is cheaper than writing around it; a feature that needs more than a near one-to-one mapping onto core SIR belongs in S1. Decided in #9, rules 2 and 3.

Lexical elements

s0.tokens (syntax, draft) S0 uses the lexical structure of the lexical chapter, with these literals only. Decided in #9 (“Literals”: integers, characters, strings):

ident        = identifier ;                       (* lex.ident *)
int_lit      = decimal_literal ;                  (* lex.lit.int.decimal *)
char_lit     = "'" character "'" ;                (* lex.lit.char *)
string_lit   = '"' { character } '"' ;            (* lex.lit.string *)

Decided in #9 (“Literals”: integers, characters, strings).

Open: the escape sequences of character (#39); integer literal forms other than decimal (#56).

Programs and files

s0.program (static, draft) A program is compiled from one entry file and the files its imports name, followed from the entry file; nothing scans a directory for files. The entry function is main. Direction in #9 and #47 (item 8), 2026-11-22, which replaces “multiple files compiled as one unit with shared globals”.

s0.build-script (static, draft) The compiler reads a build script written in Cesium, build.csm by default, and runs it while building. The script names the entry file with a built-in function (named something like set_entrypoint()), which only build scripts the compiler reads directly can call. Compiler settings, such as default arguments and the name of the build script, are in a JSONC file in the working directory or the root of the repository. Direction in #9 and #47 (item 8), 2026-11-22.

Open (#9, #47, #104): the subset of Cesium a build script may use in S0; the name and the form of the built-in; the name and discovery of the settings file; naming an entry function other than main (after S0).

s0.import (syntax, draft) A file imports a Cesium module with import(ns, "path/to/file.csm"): the namespace ns and the file that defines it, by a path relative to the importing file or absolute. In S0 a namespace is defined in exactly one file, and everything imported is linked statically. Imports may form cycles. Direction in #19, 2026-11-22.

s0.import.public (static, draft) An imported file exposes only what it marks public; everything else is private to its file. Direction in #19, 2026-11-22.

Open (#19, #13): where an import is written and how it ends; how a name of an imported namespace is written at the place it is used; how something is marked public, and what private means exactly.

s0.entry (static, draft) The entry function is the hosted main: i32 = main(i32 argc, ##u8 argv). Decided in #9.

s0.entry.hosted (accepts)

i32 = main(i32 argc, ##u8 argv) {
  return 0;
}

The top level

s0.unit (syntax, draft) A file is a sequence of top-level declarations. By #9 (“Data”, “Outside world”, “Globals”, “Functions”):

unit         = { top_decl } ;
top_decl     = struct_decl
             | extern_block
             | const_decl
             | global_decl
             | func_def ;

The imports of s0.import are not in top_decl yet: their form is open.

Types

s0.type (syntax, draft) A type is a base type after any number of sigils. By #9 (“Types”, “Functions”):

type         = { sigil } base_type ;
sigil        = "#" | "~" ;
base_type    = "u8" | "i32" | "u32" | "i64" | "u64" | "uword"
             | ident ;                            (* a struct name *)
ret_type     = "void" | type ;
declarator   = ident [ "[" int_lit "]" ] ;

s0.type.integers (static, draft) The integer types of S0 are u8, i32, u32, i64, u64 and uword. i32 and u32 are there for the C ABI: an i64 passed where C expects an int works on x86-64 and breaks on 32-bit targets. Decided in #9.

s0.type.pointer (static, draft) #T in a declaration is a pointer to T, through which the T is not modified, everywhere; ##T is a pointer to a pointer. Decided in #9 (amendment of 2026-11-16).

s0.type.borrow (static, draft) ~T is parsed, and stage 0 treats it as #T: it checks no borrows. Decided in #9.

s0.type.array (syntax, draft) A fixed array is declared with its length after the name, T name[N], where N is an integer literal. Decided in #9; the literal is provisional.

s0.type.array.local (accepts)

i32 = main(i32 argc, ##u8 argv) {
  u8 tmp[20];
  tmp[0] = 1;
  return 0;
}

Open: whether N may be a const global (#9); whether ~T, ~=, ~x, :#T and :~T join S0 (#74).

Declarations

s0.struct (syntax, draft) A struct has fields only: no methods. By #9 (“Data”):

struct_decl  = "struct" ident "{" { field_decl } "}" ;
field_decl   = type declarator ";" ;

s0.struct.no-literal (legality, draft) S0 has no struct literals; fields are assigned one at a time. Decided in #9.

s0.extern (syntax, draft) An extern block declares functions of a C library, with the C ABI. The library is named by a string literal. By #9 (“Outside world”); double quotes, direction in #19 and #39, 2026-11-22:

extern_block = "extern" ident "=" "import" "(" string_lit ")"
               "{" { extern_func } "}" ;
extern_func  = ret_type "=" ident "(" [ params ] ")" ";" ;

s0.extern.write (accepts)

extern libc = import("c") {
  i64 = write(i32 fd, #u8 buf, uword len);
}

i32 = main(i32 argc, ##u8 argv) {
  write(1, "hi\n", 3);
  return 0;
}

s0.extern.library (static, draft) The library name is looked up in the system’s library path. A dynamic library comes into a program only this way. Direction in #19, 2026-11-22.

Open: how a function of an extern block is named where it is called; stage 0 takes its bare name, and the reference writes windows.Sleep(ms) (#19).

s0.const (syntax, draft) A const global is a constant; constants also stand in for enums. By #9 (“Globals”):

const_decl   = "const" type ident "=" expr ";" ;
global_decl  = type declarator [ initializer ] ";" ;

s0.const.kinds (accepts)

const i64 TOK_IDENT = 1;
i64 count = 0;

i32 = main(i32 argc, ##u8 argv) {
  count = TOK_IDENT;
  return 0;
}

Open: whether a const global may be an array or a pointer, and which expressions may initialize a global (#9).

s0.func (syntax, draft) A function is defined with its return type, =, its name and its parameters. By #9 (“Functions”):

func_def     = ret_type "=" ident "(" [ params ] ")" block ;
params       = param { "," param } ;
param        = type ident ;

s0.func.where (syntax, draft) A function may be defined wherever a statement may be written. The parser tells a definition from an assignment by what follows the closing parenthesis: { starts a definition, ; ends an assignment. Direction in #27, 2026-11-22 (option 2).

Open (#27): whether S0 limits definitions to the top level, as the grammar above and stage 0 do; how far the parser looks ahead when the first name is a type the program defines.

s0.func.single-return (static, draft) A function returns one value, or none (void); functions may be recursive. Decided in #9.

s0.func.scalar (legality, draft) Parameters, return values and extern signatures have scalar and pointer types only: a struct is not passed or returned by value. Decided in #8 (“v0 restrictions”).

s0.initializer (syntax, draft) A declaration’s initializer is a plain value with =, or a pointer with := (ownership) or #= (borrow). By #9 (mapping, and amendment of 2026-11-16) and #11, rules 4 and 5:

initializer  = "=" expr                          (* plain values only *)
             | ":=" expr                         (* pointers only: ownership *)
             | "#=" expr ;                       (* pointers only: borrow *)

Statements

s0.statement (syntax, draft) The statements of S0. By #9 (“Control flow”, mapping) and #11, rule 2:

block        = "{" { statement } "}" ;
statement    = local_decl
             | assign_stmt
             | expr_stmt
             | if_stmt
             | while_stmt
             | "break" ";"
             | "continue" ";"
             | return_stmt ;

local_decl   = type declarator [ initializer ] ";" ;
assign_stmt  = place assign_op expr ";" ;
assign_op    = "=" | ":=" | "#=" ;
place        = ident { "[" expr "]" | "." ident } ;
expr_stmt    = expr ";" ;
if_stmt      = "if" "(" expr ")" block [ "else" ( block | if_stmt ) ] ;
while_stmt   = "while" "(" expr ")" block ;
return_stmt  = "return" [ expr ] ";" ;

s0.statement.braces (syntax, draft) The bodies of if, else and while are blocks, in braces. By use (control-flow).

s0.statement.braces.unbraced (rejects)

i32 = main(i32 argc, ##u8 argv) {
  if (argc == 0) return 1;
  return 0;
}

s0.statement.loops (static, draft) break and continue apply to the innermost while. Decided in #8.

s0.statement.loops.innermost (accepts)

i32 = main(i32 argc, ##u8 argv) {
  i32 i = 0;
  while (i < 10) {
    if (i == 5) {
      break;
    } else {
      i = i + 1;
      continue;
    }
  }
  return i;
}

s0.assign.pointer (static, draft) Plain = on a pointer assigns through it; it never initializes a pointer or makes it point elsewhere. A pointer gets its address with := or #=: #Token t := alloc(1);, t.start #= "hello";. Decided in #9 (amendment of 2026-11-16) and #11, rules 2 and 5.

s0.assign.pointer.fields (accepts)

struct Token {
  i64 kind;
  #u8 start;
  uword len;
}

i32 = main(i32 argc, ##u8 argv) {
  #Token t := alloc(1);
  t.kind = 1;
  t.start #= "hello";
  return 0;
}

s0.assign.pointer.plain (rejects)

i32 = main(i32 argc, ##u8 argv) {
  #u8 p = argv[0];
  return 0;
}

s0.condition (static, draft) The condition of an if or a while is an integer: nonzero is true. Direction in #38, 2026-11-22, which also allows a pointer (non-null is true) and no floating-point value or error union.

Open: ~= in S0 (#74); a block as a statement by itself, which S0 does not list; how a pointer in a condition means non-null under dereference by default (#38); compound assignment and ++/-- (#15) in S0.

Expressions

s0.expr (syntax, draft) The expressions of S0, from the lowest precedence to the highest. The operators are those of #9 (“Expressions”), at the levels of the reference’s precedence table (operators-expressions):

expr           = logical_or ;
logical_or     = logical_and { "||" logical_and } ;
logical_and    = bit_or { "&&" bit_or } ;
bit_or         = bit_xor { "|" bit_xor } ;
bit_xor        = bit_and { "><" bit_and } ;
bit_and        = equality { "&" equality } ;
equality       = relational { ( "==" | "!=" ) relational } ;
relational     = shift { ( "<" | ">" | "<=" | ">=" ) shift } ;
shift          = additive { ( "<<" | ">>" ) additive } ;
additive       = multiplicative { ( "+" | "-" ) multiplicative } ;
multiplicative = cast { ( "*" | "/" | "%" ) cast } ;
cast           = prefix { "as" type } ;
prefix         = postfix | "#" prefix | "!" prefix ;
postfix        = primary { "[" expr "]" | "." ident | "(" [ args ] ")" } ;
primary        = ident
               | int_lit
               | char_lit
               | string_lit
               | "sizeof" "(" type ")"
               | "(" expr ")" ;
args           = expr { "," expr } ;

The places of ! and as are provisional (#15): the reference’s table has no level for them. as binds tighter than every binary operator and looser than the prefix and postfix operators, so a + b as u8 converts only b.

s0.expr.left (static, draft) The binary operators group from left to right. Implied by the reference, which marks only exponentiation, not in S0, as grouping from the right.

s0.expr.deref (static, draft) There is no dereference operator: a pointer is read through by default, and p[i] and p.f reach through a pointer. Decided in #9 (amendment of 2026-11-16) and #11, rules 5 and 29.

s0.expr.expected (static, draft) Where the expected type is a pointer, a pointer name is the pointer; where no expected type says so, a bare pointer name is the value it points at, and its address is taken with #. Direction in #26, 2026-11-22.

s0.expr.typing (static, draft) An expression takes its type from its destination: in u8 k = i + j; the sum is computed as a u8. Where there is no destination, as in an argument or a comparison, the program says the type with a conversion. Direction in #33, 2026-11-22.

s0.expr.convert (static, draft) A number is converted to another numeric type by the type’s name used as a function: u8(x), u64(t.len). Narrowing keeps the low bits. as reinterprets the bits of a value as another type of the same size, and converts a pointer to another pointer type: q as #u8. Direction in #34, 2026-11-22.

s0.expr.arithmetic (dynamic, draft) The arithmetic operators have core SIR’s defined meaning: integers wrap, and shifts and division by zero have defined results. Decided in #9 and #8.

s0.expr.short-circuit (dynamic, draft) && and || evaluate their right operand only when the left one does not decide the result. Decided in #9 (mapping to land and lor).

s0.expr.short-circuit.guard (accepts)

i32 = main(i32 argc, ##u8 argv) {
  i32 i = 0;
  if (i != 0 && 10 / i > 1) {
    return 1;
  }
  return 0;
}

s0.expr.string (static, draft) In S0 a string literal is used as a #u8 to its first byte, NUL-terminated. Decided in #9; see lex.lit.string.type.

s0.expr.sizeof (static, draft) sizeof(T) is the size of T in bytes, as a uword. Decided in #9.

s0.expr.sizeof.struct (accepts)

struct S {
  i64 a;
  u8 b;
}

i32 = main(i32 argc, ##u8 argv) {
  uword n = sizeof(S);
  return 0;
}

s0.expr.alloc (static, draft) alloc(count) allocates count elements from an arena that is never freed; the element type is the one of the pointer it initializes. Decided in #9 (“Memory”).

Open: unary minus, which S0 source writes as 0 - x (#15); prefix ~x (#74); null, and #== and #!= in S0 (#26, #43); !. and true/false in S0; the result type of a comparison, u8 in core SIR (#38); T(x) told apart from a call (#34, #18).

What S0 source must satisfy

These are checked after parsing. Stage 0 checks only what keeps S0 source meaningful (#9: “minimal type checking”), so they say what S0 source must be, not what stage 0 reports.

s0.check.address-context (legality, draft) := and #= are used only with a pointer on the left; = initializes only a plain value, or assigns through a pointer. Decided in #11, rules 4 and 5.

s0.check.pointer-argument (legality, draft) A pointer is passed to a pointer parameter as it is; a plain value passed to a pointer parameter has its address taken with #. Decided in #11, rule 20.

s0.check.whole (legality, draft) A struct or an array is not assigned as a whole in S0. By #9 (no struct literals, fields one at a time); the rule for the full language is open (#22).

s0.check.whole.copy (rejects)

struct P {
  i64 x;
}

i32 = main(i32 argc, ##u8 argv) {
  P a;
  P b;
  a.x = 1;
  b = a;
  return 0;
}

Open (#74): s0.subset means that S0 source also satisfies the ownership rules stage 0 does not check, such as every owner released on every path (#11, rule 9) and writes only through ~T (#11, rule 1). S0 has no free, and the sample program of #9 writes through #T.

Mapping to core SIR

s0.sir (dynamic, draft) Each S0 construct means what its core SIR form means. Decided in #9:

S0Core SIR
T name = e; (local)local %name: T = e
x = e; (local)set %x = e
p[i] = e;, p.f = e;store index(p, i), e, store field(p, f), e
p[i], p.f (read)load(index(p, i)), load(field(p, f))
#xaddr(%x)
if / else, while, break, continue, returnthe same structured statements
a + b, a < b, a >< b, …add(a, b), lt(a, b), xor(a, b), …
a && b, a || b, !aland(a, b), lor(a, b), lnot(a)
e as Tas(T, e)
sizeof(T)sizeof(T) (resolved by the backend)
f(args)call @f(args)
"text"const @str.N: [len+1 x u8] = "text\00" and its address
alloc(n) (target #T)as(ptr<T>, call @cs_arena_alloc(mul(n, sizeof(T)), alignof(T)))
struct S { ... }struct S { ... }
extern ... import(...) { ... }extern func @name(...) -> T callconv(c)

Every statement also carries its source location, for debug line information. The mapping of a conversion T(x) (s0.expr.convert) is not recorded yet.

Not in S0

s0.excluded (legality, draft) Everything not given above is not S0, among it: for, ranges, switch and case, enum, unions and typeof, slice, list, dict and str, multiple return values, defer, error types, catch and throw, floating point, free and realloc, methods and this, generics, traits, inheritance, operator overloading, the math operators (^, ::, @, the * that depends on its operands, postfix ~), SIMD, comptime, string interpolation, with, inline asm, volatile and atomic. Decided in #9; modules and import of Cesium code left the list by the direction of 2026-11-22 (s0.import).

s0.excluded.transpose (legality, draft) Transpose and the decorated operators are not S0. Where ~ next to an operator could be either in S0 source, it is an error; (A~)*B says which. Direction in #28, 2026-11-22.

Stage 0 as implemented

This section describes cesium0 and decides nothing. It differs from the rules above in these places:

RuleStage 0
s0.program, s0.build-script, s0.importCompiles the files named on its command line as one unit with shared globals; no import, no build script.
s0.expr.convertConverts numbers with as (t.len as u64); u8(x) is an unknown function.
s0.func.whereFunctions at the top level only.
lex.op.not, lex.op.address-compare, lex.predeclared.truthNo !., #==, #!=, true or false.
lex.lit.int.typeRejects a literal whose type no context gives, such as if (1), instead of making it an i32.
s0.externAlso takes a library name in single quotes.
s0.type.borrowTakes ~=, prefix ~x, :#T and :~T, as #=, #x and the plain pointer type.

It also rejects, because the design is open: unary minus, literal forms other than decimal, a local that reuses a name in scope (#75), null, a name that is a keyword of full Cesium, and assigning a struct or array as a whole. It limits arguments and parameters to 64 and nesting to 128 levels, traps on division by zero and on the signed MIN / -1, and starts a local without an initializer at zero (#8). bootstrap/README.md has the details.

Cesium 0.2.0, in development (dev at 53dc38a) · GitHub