Standalone

Running Chasm scripts without a game engine, command-line tools, utilities, and the WASM target.

Chasm scripts don't need a game engine. Any script with a def main() function runs as a standalone program, useful for tools, data processing, build scripts, or learning the language.

Running a Script

chasm run script.chasm

The CLI compiles the script and runs the result immediately. Standard output goes to the terminal.

Compiling to C

chasm compile script.chasm

Produces script.c in the current directory. Compile it yourself:

cc -o my_program script.c -I$CHASM_HOME/runtime
./my_program

Example, Data Processing Tool

def main() do
  data = file_read("scores.txt")
  lines = split_lines(data)
  total = 0
  i = 0
  while i < lines.len do
    total = total + to_int(lines.get(i))
    i = i + 1
  end
  print("Total: #{total}")
  print("Count: #{lines.len}")
  print("Average: #{to_float(total) / to_float(lines.len)}")
end

The WASM Target

Compile to WebAssembly for running in browsers or WASM runtimes:

chasm compile --target wasm script.chasm

This produces a .wat file (WebAssembly text format). Assemble it with:

wat2wasm script.wat -o script.wasm

Load in a browser with the provided JavaScript runtime:

<script type="module">
  import { chasmLoad } from './runtime/chasm_wasm_rt.js'
  const chasm = await chasmLoad('./script.wasm')
  chasm.main()
</script>

WASM limitations

The WASM target supports strings, structs, and arrays. Engine-specific functions (draw_rectangle, key_down, etc.) are not available, WASM is for standalone logic only.

No Engine Globals

Without an engine, these are not available:

  • key_down, key_pressed, mouse_x, mouse_y
  • draw_*, clear_background
  • load_texture, load_font, load_sound
  • screen_width, screen_height, get_fps

Only the core built-ins work in standalone mode: math, strings, file I/O, print, assert.