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
- Implement it in
crates/korrin/src/builtins.rsagainst theNativeFnPtrsignature,fn(&mut dyn Host, &[Value], Span) -> Exec<Value>. TheHostis whichever engine is running: take output, arguments, input, and re-entrant calls from it, and expect nothing else of it. - Add a row to the
BUILTINStable (name,Arity, function). - 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. - Add or extend a golden test under
crates/korrin/tests/programs/. - Add a
CHANGELOG.mdentry.
Adding a method
- Implement it in
crates/korrin/src/interpreter/methods.rsagainst theNativeMethodFnsignature — the receiver arrives as&Value, separate from the argument slice. - Add a
method(...)row toSTR_METHODS,LIST_METHODS, orMAP_METHODS. TheAritycounts arguments after the receiver. - 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. - Add or extend a golden test and an interpreter unit test.
- Add a
CHANGELOG.mdentry.
Adding to a standard-library module
A member of a built-in module (math, later io) — see
ADR 0019.
- Add the function to the module's file in
crates/korrin/src/stdlib/, against theNativeFnPtrsignature, and list it in that module's table (and itsnames()). - Document it in a table row in
../spec/11-modules.mdas`math.name(args)`.crates/korrin/tests/builtins.rschecks the module'snames()against that section, both ways. - Add an interpreter unit test and, where it fits, a line in
tests/programs/module_math.kor. - Add a
CHANGELOG.mdentry.
Adding a whole standard-library module
- New file
crates/korrin/src/stdlib/<name>.rswith apub(crate) fn module() -> Environmentand anames(). - Register it in
stdlib::loadandstdlib::module_names. - New section in
docs/spec/11-modules.md; a guide mention; an ADR if the module makes a design choice (e.g.ioand sandboxing). - A
tests/programs/golden or atests/modules.rscase.