Standard Library

The standard library documentation for the Occult programming language.

Overview

The standard library lives under std/ next to the compiler. Each module is a .occ file; include the ones you need.

occult
import stdio from std;
import memory from std;
import vec from std;
import strlib from std;

// Alternatively:
include "../std/stdio.occ"
include "../std/memory.occ"
include "../std/vec.occ"
include "../std/strlib.occ"

Everything lives in the std module. Calls are written std::name(...).

Signatures here reflect the current source and may still move.

stdio

Printing, character I/O, and a printf with common formatters.

Functions

Function Behavior
std::print_char(i64 c) Write one character to stdout
std::print_string(string s) Write a string to stdout
std::print_newline() Write '\n'
std::print_integer(i64 n) Write an integer in base 10
std::printf(string fmt, ...) Formatted print; variadic. Supports %d / %i, %s, %c, %x, %f

memory

Heap allocation. Untyped i64-returning form and a typed generic form.

Functions

Function Behavior
std::malloc(u64 len) Allocate len bytes, return the address as u64
std::free(u64 ptr) Free a block previously returned by std::malloc
std::realloc(u64 ptr, u64 new_size) Resize a block in place or copy it to a new address
std::__malloc_generic(u64 len) T* Typed allocation returning T*
std::__free_generic(T* ptr) Typed free
std::__realloc_generic(T* ptr, u64 new_size) T* Typed resize

strlib

String primitives.

Functions

Function Behavior
std::strlen_fast(string s) Length in O(1) by reading the stored size 8 bytes before the string (see Language Spec, "Strings in Memory")
std::strlen(string s) Length by walking the string until the terminator
std::strcmp(string a, string b) i64 0 when equal, otherwise the signed difference of the first non-matching byte
std::str_empty(string s) True when the string has zero length
std::str_char_at(string s, u64 index) char Byte at index

vec

A generic growable vector. vector_new returns a heap-allocated Vector* that owns its backing storage.

Type

occult
generic<T>
struct Vector {
    T*  data;
    u64 size;
    u64 capacity;
    u64 data_size;
}

Functions

Function Behavior
std::vector_new(u64 data_size, u64 capacity) Vector* Allocate a new vector with the given element size and initial capacity
std::vector_free(Vector* v) Free the storage and the vector itself
std::vector_push_back(Vector* v, T data) Append; grows capacity as needed
std::vector_pop_back(Vector* v) T Remove and return the last element
std::vector_push_front(Vector* v, T data) Insert at index 0
std::vector_pop_front(Vector* v) T Remove and return index 0
std::vector_insert(Vector* v, u64 i, T data) Insert at i, shifting elements right
std::vector_at(Vector* v, u64 i) T Read element at i
std::vector_empty(Vector* v) True when size is 0
std::vector_clear(Vector* v) Set size to 0 without freeing the storage

Example

occult
include "../std/vec.occ"

fn main() i64 {
    Vector<i64>* v = std::vector_new<i64>(sizeof(i64), 4);

    std::vector_push_back<i64>(v, 10);
    std::vector_push_back<i64>(v, 20);
    std::vector_push_back<i64>(v, 30);

    i64 total = 0;
    for i64 i = 0 when i < v.size do i += 1 {
        total += std::vector_at<i64>(v, i);
    }

    std::vector_free<i64>(v);
    return total; // 60
}

Where Each Module Lives

occult
std/
    memory.occ   // allocator
    stdio.occ    // print_*, printf
    strlib.occ   // string helpers
    vec.occ      // generic Vector
    sys/         // platform-specific internals
×