Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Built-in Functions

Built-ins are compiled to EXEC <index>: the arguments are pushed left to right, the VM pops them, runs the native function and pushes the result if there is one. The compiler checks the argument count (argument count mismatch for 'name': expected N, got M).

IndexFunctionArgsReturnsDescription
0x01println(value)1–Print a value and a newline.
0x02print(value)1–Print a value.
0x03inputInt()0integerRead a whitespace-delimited integer; 0 and a message if invalid.
0x04inputStr()0stringRead one whitespace-delimited word.
0x05str2int(text)1integerParse an integer; Function error if invalid.
0x06int2str(n)1stringInteger to text; Function error for a float.
0x07str2float(text)1floatParse a float; 0 if invalid.
0x08float2str(x)1stringNumber to text with six decimals.
0xA0assertCapability(name)1–Stop with an error if the host lacks the capability.
0xA1openFile(path)1integerOpen (create if missing, keep contents) for read/write; returns a handle.
0xA2writeFile(text, handle)2–Append text to the file and flush.
0xA3readFile(handle)1stringWhole file contents from the start.
0xA4closeFile(handle)1–Close and release the handle.
0xA5randomSeed(seed)1–Seed the generator.
0xA6random()0floatFloat in [0, 1).
0xA7randomRange(min, max)2integerInteger in [min, max].
0xA8httpRequest(method, url, headers, body, &response)5integerHTTP request; returns status or -1.
0xA9strlen(text)1integerLength.
0xAAsubstr(text, start, length)3stringSubstring.
0xABstrfind(text, needle)2integerIndex of first match or -1.
0xACstrcase(text, upper)2stringUpper case if upper is non-zero, else lower.
0xADtrim(text)1stringStrip leading and trailing whitespace.

Failures inside a native function are caught by the VM: it prints Function error, then the message, and halts.

Adding a built-in

  1. Implement it in src/vm/vmfuncmap.cpp under a new index. Pop the arguments in reverse order (last argument on top) and push the result if there is one.
  2. Register it in funcList in src/compiler/compiler_tables.cpp with {index, argCount, returnable}.

The disassembler and debug symbols pick the name up from funcList automatically.