Cassette Primitives
This is the reference of built-in primitives for the programming language Cassette.
Built-in functions are executed via the trap
keyword. These trap calls are usually wrapped in a module function at runtime for convenience. For example:
def print(value) trap print(value)
System Functions
panic!(message)
Halts the program in an error state, with the given message.
typeof(value)
Returns the type of the given value, as a symbol:
:integer
:symbol
:pair
lists, pairs, andnil
:tuple
:binary
char(num)
Returns a string of length 1, with the byte value of num
(which must be between 0 and 255).
format(iodata)
Renders iodata
into a binary. iodata
is a binary, an integer between 0–255 (representing a byte), or a list of other iodata
items.
symbol_name(symbol)
Returns a string of the name of the given symbol, or nil
if not present.
hash(value)
Returns a hash of a value.
popcount(num)
Returns the number of bits set in an integer.
max_int()
Returns the maximum integer.
min_int()
Returns the minimum integer.
time(value)
Returns the current Unix timestamp, in seconds.
random()
Returns a random integer between the minimum and the maximum integer size.
seed(value)
Seeds the random number generator.
I/O
open(path, flags)
Wrapper for the Unix open
function. Returns a file descriptor as an integer or {:error, reason}
.
open_serial(device, speed, opts)
Opens a serial device. opts
is an integer that is set to the c_cflag
property of the termios. Returns a file descriptor as an integer or {:error, reason}
.
close(file)
Wrapper for the Unix close
function. Returns :ok
or {:error, reason}
.
read(file, size)
Wrapper for the Unix read
function. Returns a binary or {:error, reason}
.
write(file, data, size)
Wrapper for the Unix write
function. Returns the number of bytes written or {:error, reason}
.
seek(file, offset, whence)
Wrapper for the Unix lseek
function. Returns the position or {:error, reason}
.
listen(port)
Returns a passive socket listening on a given port (as a string).
accept(socket)
Waits for a new connection on a passive socket.
connect(host, port)
Returns an active socket connected to a host and port.
Graphics
open_window(title, width, height)
Creates a new window. Returns a window reference.
close_window(window)
Closes a window.
update_window(window)
Updates a window's contents.
next_event()
Returns an event record with these fields:
what
: Event type, one of:keyDown
,:keyUp
,:autoKey
,:mouseDown
,:mouseUp
, or:quit
.when
: Timestamp of when the event occured.where
: The mouse position of the event, as a point record withx
andy
fields.message
: The event message. For keyboard events, this is the ASCII character in the low byte and the scan code in the second byte. For mouse events, this is a window reference.modifiers
: A bitmap of keyboard modifiers.15 13 12 11 10 9 8 7 6 0 ┌───────┬──┬──┬──┬──┬──┬──┬───────────┐ │ │ │ │ │ │ │ │ │ └───────┴─┬┴─┬┴─┬┴─┬┴─┬┴─┬┴───────────┘ Control───┘ │ │ │ │ │ Option───────┘ │ │ │ │ Caps Lock───────┘ │ │ │ Shift──────────────┘ │ │ Command───────────────┘ │ Mouse────────────────────┘
write_pixel(x, y, color, window)
Writes a pixel into a window. Color is a 24-bit 0x00RRGGBB
integer.
blit(data, width, height, x, y, window)
Copies pixel data into a window. data
is a binary of pixel values, width
and height
are the size of the source image, and x
and y
are the destination in the window.