Cassette Primitives

This is the reference of built-in primitives for the programming language Cassette.

Built-in functions are executed via the trap pseudo-function. These trap calls are usually wrapped in a module function at runtime for convenience. For example:

def print(value) trap(:print, value)

System Functions

trap(:panic!, message)

Halts the program in an error state, with the given message.

trap(:typeof, value)

Returns the type of the given value, as a symbol:

trap(:byte, num)

Returns a string of length 1, with the byte value of num (which must be between 0 and 255).

trap(:symbol_name, symbol)

Returns a string of the name of the given symbol, or nil if not present.

trap(:time, value)

Returns the current Unix timestamp, in seconds.

trap(:microtime, value)

Returns the current Unix timestamp, in microseconds.

trap(:random)

Returns a random integer between 0 and the maximum integer size.

trap(:random_between, min, max)

Returns a random integer between min and max.

trap(:seed, value)

Seeds the random number generator.

I/O

trap(:open, path, flags)

Wrapper for the Unix open function. Returns a file descriptor as an integer or {:error, reason}.

trap(:close, file)

Wrapper for the Unix close function. Returns :ok or {:error, reason}.

trap(:read, file, size)

Wrapper for the Unix read function. Returns a binary or {:error, reason}.

trap(:write, file, data, size)

Wrapper for the Unix write function. Returns the number of bytes written or {:error, reason}.

trap(:seek, file, offset, whence)

Wrapper for the Unix lseek function. Returns the position or {:error, reason}.

Graphics

trap(:sdl_new_window, width, height)

Creates a new window. Returns a window reference.

trap(:sdl_destroy_window, window)

Closes a window.

trap(:sdl_present, window)

Updates a window's contents.

trap(:sdl_clear, window)

Clears a window's contents.

trap(:sdl_line, x1, y1, x2, y2, window)

Draws a line in a window.

trap(:sdl_set_color, r, g, b, window)

Sets a window's draw color.

trap(:sdl_poll_event)

Returns an event record, a tuple with these fields:

trap(:sdl_get_ticks)

Returns the number of milliseconds since SDL was initialized.