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 properly Variables
Use dicts, or work around the missing methods Lists and dicts
Name your constants Enums and structs
Split a project across files 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 a variable declared inside a procedure does not survive recursion — pass state through parameters.

An empty body plus @opcode says “this procedure is that Scratch block”. The stdlib is written this way, as .knip files rather than compiler special-cases, so you can do it too:

proc x(@opcode = "motion_xposition") -> num {}
proc y(@opcode = "motion_yposition") -> num {}
private atX: num = x();

Check the stdlib first. math.random, math.round, math.sqrt and the trig functions are already there. See math.

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 two files that are kept building, and they are the best single reference for “does this actually build”. examples/all.knip uses every feature that lowers, exactly once each, with a comment on every line; examples/codegen.knip is a fuller project, including a recursive fib.

Terminal window
katnip build examples/all.knip
katnip build examples/codegen.knip

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

  • a struct works through its fields; a whole struct can’t be printed or compared
  • console.*, typeof, list.merge, and dict.contains fail the build
  • zip/enumerate work in a for header, but fail the build used as a plain value
  • imports build: procedures and literal values cross, module variables do not
  • an enum slot takes a member or a matching literal, but never a computed value

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.
  • CLI: tokenize, parse, lower let you see exactly what each stage did with your code, and decompile turns an existing .sb3 into source.
  • Embedding the compiler — checkSource 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.

github.com/Katnip-org/Katnip