Cassette Primitives
This is the reference of built-in primitives for the programming language Cassette.
Built-in functions are accessed from the Host
module. Some of these calls are wrapped in more descriptive modules.
Basic 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
ifvalue
is an integer that corresponds to a symbol:pair
lists, pairs, andnil
:tuple
:binary
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.
make_tuple(list)
Converts a list into a tuple.
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, 536,870,911.
min_int()
Returns the minimum integer, -536,870,912.
System
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.
args()
Returns a list of command-line arguments invoked with the program.
env(name)
Returns the value of a system environment variable, or nil
.
shell(cmd)
Executes a command in a shell. Returns the integer result code.
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 a tuple record with these fields:
- Event type, one of
:keyDown
,:keyUp
,:autoKey
,:mouseDown
,:mouseUp
, or:quit
. - Timestamp of when the event occured.
- The mouse position of the event, as a tuple with
x
andy
fields. - 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.
- 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.