castc output and the gameplay module (M6)
The contract between castc (libcast, the compiler) and the engine's module builder. The
language itself is defined by docs/DESIGN.md "Language spec" plus the test suite under
tests/.
Command
castc build GAME_DIR OUT_DIR [--dev | --release] [--no-entry]
- Compiles every
.castfile underGAME_DIR(in any folder exceptnative/,assets/and folders starting with.), following imports. - On success, exits 0 and leaves exactly these files in
OUT_DIR, rewriting only the files whose content changed so incremental tools see real changes:- one
.cfile per.castmodule, named after its path:behaviors/health.cast->behaviors__health.c; game_generated.h: the C declarations of every exported function, struct and component, for hand-written C;schema.c: oneMtComponentDescper@componentstruct, built from the struct layout and decorators;cast_module.c: definesvoid molten_cast_on_load(const MtApi* api), which registers every component, then every behavior;cast_files.txt: the list of.cfiles to compile, one per line.
- one
- Unless
--no-entryis given,cast_module.calso defines the module entry (MOLTEN_MODULE) withon_load = molten_cast_on_load. The builder passes--no-entrywhen a file innative/definesMOLTEN_MODULE; that hand-writtenon_loadmust then callmolten_cast_on_load(api)itself (declared ingame_generated.h). - On error, prints the diagnostics as
file:line:col: error: message(paths relative toGAME_DIR), exits 1, and leavesOUT_DIRuntouched, so the running module stays as it is. --devadds the dev checks: integer divide-by-zero logs throughapi->logwith the.castfile and line.--releaseomits them.
Generated C
- Hand-written C calls an exported Cast function using its declaration in
game_generated.h. For example,export function select(...)is called ascast_export_select(...). Thecast_export_prefix also applies to exported constant accessors and is reserved for generated declarations. Use the spelling in the header for names that require escaping, such as names containing$. The standalone Castmainis invoked by a generated C entry point. Adeclare functionuses the original C symbol name. - C11 that compiles warning-free with TinyCC, gcc and clang (
-Wall -Wextra) and MSVC (/W4). The output uses<molten.h>and the standard headers<stdint.h>,<stdbool.h>,<math.h>,<string.h>,<stdlib.h>and<stdio.h>. #linedirectives point back to the.castsource.- No statics hold state between frames. Component ids are looked up with
api->find_componentduring the call that uses them, or passed in by position: a behavior's columns are in the order of its component list. - A behavior from
behaviors/health.castis registered as"behaviors/health"(its path without the extension) and lowers to oneMtBehaviorFnthat loops overchunk->countrows.obj.destroy()becomesapi->destroy(chunk->handles[i]). - Built-in component types map to the engine's:
Transform->MtTransform(MT_TRANSFORM), and likewiseMesh,Camera,Light.vec2/vec3/dvec3/vec4/quatare layout-compatible withMtVec2/MtVec3/MtVec4/MtQuat;mat3/mat4are column-major float arrays. - Schema field types:
bool->MT_FIELD_BOOL,int/int32->INT32,uint32->UINT32,int64/uint64likewise,float->FLOAT,float64->FLOAT64,vec2/vec3/dvec3/vec4/quat-> the vector types.int8,int16,uint8anduint16fields in components are a compile error in M6, since the schema has no such field types yet.
The "molten" module (M6)
import { ... } from "molten" provides behavior, Transform, Mesh, Camera and
Light. Built-in functions (min, max, ..., print) need no import. The rest of the
engine API also exposes spawning, screens and input. Cast screen hooks use module-qualified
C symbols. Their adapters register the module path through the engine's screen API.
Module build (engine)
When a game folder has any .cast file, the module builder runs castc build with --dev
in Debug or --release in Release. It writes the output into
<work dir>/cast/, then compiles those files plus every native/**/*.c with TinyCC into the
gameplay module. Games without .cast files keep building with the platform compiler, as
before. Live reload watches .cast files as well as native/.
molten build --release consumes the same C output with release checks disabled. It
compiles the generated and native gameplay sources with clang and links the
molten_module entry into the shipped executable. See Shipping a Molten game.