Skip to content

Where to go next

Three projects in, you have used most of what Katnip can currently compile. Here is what to read depending on what you want to do.

If you want to Read
Know every loop form and what it compiles to Control flow
Return values, overload, recurse Procedures
Understand public / private / temp properly Variables
Use dicts, or work around the missing methods Lists and dicts
Name your constants Enums and structs
Split a project across files (check only) Modules

Scratch custom blocks cannot return values, and a naive workaround breaks the moment a procedure calls itself. Katnip runs a Tarjan SCC pass over your call graph and gives recursive procedures a return stack instead of a return variable, so this produces correct answers in a real .sb3:

proc fib(n: num) -> num {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}

It is genuinely worth building once and watching run. Details in Returning values.

Remember that temp does not survive recursion — pass state through parameters.

You did this in project 2. It generalises:

proc random(@opcode = "operator_random", from: num, to: num) -> num {}
proc round(@opcode = "operator_round", value: num) -> num {}
proc mathop(@opcode = "operator_mathop", operator: str, num: num) -> num {}
proc x(@opcode = "motion_xposition") -> num {}
proc y(@opcode = "motion_yposition") -> num {}
temp root: num = mathop("sqrt", 16);

operator_mathop alone covers abs, floor, ceiling, sqrt, sin, cos, tan, asin, acos, atan, ln, log, e ^, and 10 ^.

This works for any opcode codegen has slot metadata for; unknown ones fail the build rather than emitting a broken project. See Decorators.

The compiler repository ships examples/codegen.knip — a deliberately exhaustive project exercising everything the pipeline lowers today, with commentary. It is the best single reference for “does this actually build”, and it is kept passing.

Terminal window
katnip build examples/codegen.knip

Known gaps is the page to read before planning anything ambitious. The short version:

  • imports do not survive codegen — one file per project
  • structs do not reach the .sb3
  • ** silently produces a wrong answer
  • console.*, the casts, typeof, zip/enumerate and every dict method fail the build
  • an enum slot takes a member or a matching literal, but never a computed value
  • no asset import

Feature status has the full per-feature table if you want the complete picture.

  • VS Code extension — diagnostics as you type, and a build command, which beats a terminal round trip.
  • CLItokenize, parse, lower let you see exactly what each stage did with your code.
  • Embedding the compilercheckSource and compileToSb3 are exported, with a pluggable import resolver, if you want to build a playground or a different editor integration.

The roadmap, in the maintainers’ own priority order, is at the bottom of Feature status. Lowering imports is item one, and it would unblock more real projects than anything else on the list.

github.com/Katnip-org/Katnip