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

How to add a builtin or a method

Korrin's core vocabulary is a set of functions in the global scope (print, range, str, …) plus methods on the built-in types str / list / map (xs.push(v), text.trim()). The rule for which to add (ADR 0016): a function constructs or converts a value or does I/O; a method operates on its receiver.

Adding a function

  1. Implement it in crates/korrin/src/builtins.rs against the NativeFnPtr signature, fn(&mut dyn Host, &[Value], Span) -> Exec<Value>. The Host is whichever engine is running: take output, arguments, input, and re-entrant calls from it, and expect nothing else of it.
  2. Add a row to the BUILTINS table (name, Arity, function).
  3. Document it as a ### heading in ../spec/08-builtins.md §8.1 — the coverage test (crates/korrin/tests/builtins.rs) fails if you skip this, and fails again if the name and the table disagree.
  4. Add or extend a golden test under crates/korrin/tests/programs/.
  5. Add a CHANGELOG.md entry.

Adding a method

  1. Implement it in crates/korrin/src/interpreter/methods.rs against the NativeMethodFn signature — the receiver arrives as &Value, separate from the argument slice.
  2. Add a method(...) row to STR_METHODS, LIST_METHODS, or MAP_METHODS. The Arity counts arguments after the receiver.
  3. Document it in a table row in ../spec/08-builtins.md §8.2–8.4 as `type.name(args)`. The coverage test matches that form against the method tables both ways.
  4. Add or extend a golden test and an interpreter unit test.
  5. Add a CHANGELOG.md entry.

Adding to a standard-library module

A member of a built-in module (math, later io) — see ADR 0019.

  1. Add the function to the module's file in crates/korrin/src/stdlib/, against the NativeFnPtr signature, and list it in that module's table (and its names()).
  2. Document it in a table row in ../spec/11-modules.md as `math.name(args)`. crates/korrin/tests/builtins.rs checks the module's names() against that section, both ways.
  3. Add an interpreter unit test and, where it fits, a line in tests/programs/module_math.kor.
  4. Add a CHANGELOG.md entry.

Adding a whole standard-library module

  1. New file crates/korrin/src/stdlib/<name>.rs with a pub(crate) fn module() -> Environment and a names().
  2. Register it in stdlib::load and stdlib::module_names.
  3. New section in docs/spec/11-modules.md; a guide mention; an ADR if the module makes a design choice (e.g. io and sandboxing).
  4. A tests/programs/ golden or a tests/modules.rs case.