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.
Fill in the language
Section titled “Fill in the language”| 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 |
Recursion works — try it
Section titled “Recursion works — try it”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.
Reach blocks the stdlib has not wrapped
Section titled “Reach blocks the stdlib has not wrapped”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.
Read the real examples
Section titled “Read the real examples”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.
katnip build examples/all.knipkatnip build examples/codegen.knipKnow the edges
Section titled “Know the edges”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, anddict.containsfail the buildzip/enumeratework in aforheader, 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.
Improve the workflow
Section titled “Improve the workflow”- VS Code extension — diagnostics as you type, and a build command, which beats a terminal round trip.
- CLI:
tokenize,parse,lowerlet you see exactly what each stage did with your code, anddecompileturns an existing.sb3into source. - Embedding the compiler —
checkSourceandcompileToSb3are exported, with a pluggable import resolver, if you want to build a playground or a different editor integration.
Contribute
Section titled “Contribute”The roadmap, in the maintainers’ own priority order, is at the bottom of Feature status.