Skip to main content

Language reference

A concept-by-concept index of the Mochi language. Denser than the manual, and assumes you have already written a small program. Each page focuses on syntax, types, and rules.

Pages

File structure

A Mochi source file has these optional sections, in this order:

#SectionNotes
1package <name>Required only for multi-file packages.
2import declarationsPull in packages or FFI modules.
3Top-level declarationstype, model, fun, agent, stream.
4Top-level statementsExecuted top to bottom when the file runs.
5test blocksRun only by mochi test.

Grammar

Program = { Statement }
Statement = LetDecl | VarDecl | FunDecl | TypeDecl | AgentDecl
| StreamDecl | ModelDecl | ImportDecl | PackageDecl
| ExportDecl | TestDecl | ExpressionStatement

LetDecl = "let" Identifier [ ":" Type ] "=" Expression
VarDecl = "var" Identifier [ ":" Type ] "=" Expression
FunDecl = "fun" Identifier "(" Params ")" [ ":" Type ] Block
TypeDecl = "type" Identifier ( StructBody | "=" Union )
StreamDecl = "stream" Identifier "{" Fields "}"
AgentDecl = "agent" Identifier "{" AgentBody "}"
ModelDecl = "model" Identifier "{" ModelFields "}"
TestDecl = "test" StringLit Block
ImportDecl = "import" [ Lang ] StringLit [ "as" Identifier ]

Block = "{" { Statement } "}"

Execution model

Mochi programs compile to bytecode and run on the bundled VM, a stack machine with a small set of opcodes. The architecture notes in the source repo cover the details.

CommandAction
mochi run <file>Compile and execute. Bytecode is cached under ~/.cache/mochi.
mochi build <file> -o <out>Produce a static binary that embeds the runtime.
mochi test <file-or-dir>Compile and run test blocks only.
mochi replOpen an interactive session.
mochi serveStart the MCP server.
mochi transpile <file> --to <lang>Emit Go, Python, or TypeScript source.

Naming conventions

ConstructConventionExample
Local bindingssnake_casecurrent_user, total_pages
Constants (let at module level)SCREAMING_CASEMAX_CONNECTIONS
Type namesPascalCaseUser, OrderItem
Stream and union variantsPascalCaseSensor, Leaf, Node
Package namessnake_casereading_list, mathutils

Next

  • Manual covers each concept in tutorial form.
  • Examples holds hundreds of small, runnable programs across algorithms, agents, datasets, and AI usage.
  • Issue tracker for reference pages that are wrong or incomplete.