Cast at a glance
Cast is Molten's gameplay language. It reads like TypeScript and behaves like C: static
types, fixed memory layouts, value types, operator overloading, no garbage collector. The
compiler, castc, turns each .cast file into readable C11, which TinyCC compiles in a few
hundred milliseconds while you develop and Clang optimizes when you ship.
struct Wallet {
coins: int = 0;
add(amount: int): void { this.coins += amount; }
}
export function main(): void {
let wallet = Wallet();
wallet.add(5);
const message = `I have ${wallet.coins} coins`;
print(message);
}
I have 5 coins
Three ideas to keep in mind
- If TypeScript can write it and it compiles to efficient C, Cast writes it the same
way.
let,const, arrow functions,?.,??, template strings, unions, generics andfor...ofall work as you expect. - New ideas get full-word keywords.
struct,operator,defer;floatinstead off32. - Anything that needs a runtime is left out: no
any, no exceptions, noundefined, no garbage collection, no closures over mutable variables. The compiler's error message tells you what to use instead.
Trying code without a game
A file with export function main(): void is a standalone program:
castc run hello.cast
print(a, b, ...) needs no import; it prints the values on one line separated by spaces.
In a game it writes to the log (prefixed [game]).
The examples in these chapters are complete programs. Their output, shown under each one,
is what castc run printed.
The chapters
| Chapter | Covers |
|---|---|
| Basics | Variables, functions, control flow, operators, conditions |
| Numbers | Number types, literals, conversions, overflow, division |
| Vectors | vec2-vec4, quat, mat3, mat4, swizzles, math functions |
| Structs and classes | Value and reference types, constructors, static members |
| Interfaces, operators and equality | interface, implements, operator, == |
| Null safety | T | null, narrowing, ?., ?? |
| Unions and switch | String literal unions, discriminated unions, exhaustive switch |
| Generics | Generic functions and types, constraints |
| Strings | UTF-8 strings, templates, slicing |
| Arrays, slices and maps | T[N], Array<T>, Slice<T>, Map<K, V>, callbacks |
| Function values | Function types, arrow functions, captures |
| defer and calling C | defer, declare function, declare struct |
| Components and behaviors | @component, decorators, behavior, object types in code |
| Modules | import, export, the "molten" module |
| Memory and arenas | frame, level, game, screen, heap, new, free |
| Removed from TypeScript | What's missing and what to use instead |
The language is defined by the spec together with the
compiler test suite in tests/ (coverage table).