Skip to content

CLI

Terminal window
npm install -g @katnip-org/compiler
katnip help

From a source checkout, substitute node packages/compiler/build/cli.js for katnip.

Terminal window
katnip build <source> [output]

Compiles a .knip to a .sb3. Output defaults to the source path with the extension swapped.

Terminal window
katnip build game.knip # writes game.sb3
katnip build game.knip dist/g.sb3
katnip build game.knip --compression 1 # faster build, larger file

--compression is the zip deflate level, 0 to 9, and defaults to 6.

Imports, costumes, sounds and import() data files are resolved relative to the source file, and declared assets are packed into the .sb3. Runs the whole pipeline, so it type-checks too. On any error it prints the diagnostics and exits non-zero without writing a file — a build either produces a project that loads, or produces nothing.

Terminal window
katnip check <source> [logger]

Lex, parse, and analyze. Prints No semantic errors found. or the diagnostics. Faster than build because it skips IR and codegen.

Terminal window
katnip lower <source> [output]

Lowers to IR and prints it as JSON, to stdout unless you give a path. The fastest way to answer “what did the compiler actually do with that loop”.

Terminal window
katnip parse <source> <output>

Writes the AST as JSON. Aborts if the lexer or parser reported errors.

Terminal window
katnip tokenize <source> <output>

Writes the token stream as JSON, each token with its line and column.

Terminal window
katnip decompile <source> [output] [--split]

Turns a .sb3 into a Katnip project. Stage variables become top-level globals, the stage’s scripts land in a stage { } block, and each sprite becomes a sprite block.

The output is a .kpkg: a zip holding main.knip and every costume and sound under assets/, already declared with costume and sound lines, so it builds as-is. Output defaults to the source path with a .kpkg extension. Give an output path that doesn’t end in .kpkg and the files are unpacked into that folder instead:

Terminal window
katnip decompile game.sb3 # game.kpkg
katnip decompile game.sb3 game/ # game/main.knip, game/assets/...
katnip decompile game.sb3 game/ --split # one file per sprite

--split writes globals.knip with the stage variables, one <Sprite>.knip per sprite, and a main.knip that imports them all with as *.

The result is a starting point, not a perfect round trip:

  • Types come back as any, and names Katnip cannot use are rewritten, so my var becomes my_var. Where Scratch looks a name up as text, it is kept with as "...": cloud variables, sprite names, variables read by [var] of [sprite], and costume and sound names.
  • A costume or sound listed in the project but missing from the zip is skipped, with a # missing from the sb3 comment.
  • Anything compiled from Katnip decompiles to what it became. A returning procedure comes back as a void one writing to a name_ret variable, plus the compiler’s helper lists.
  • A block with no Katnip form is left out of the code and marked with a # unsupported: <opcode> comment, so search for that marker before rebuilding.
Terminal window
katnip help

tokenize, parse and check take an optional trailing logger positional. Pass true to write a verbose per-character, per-state trace:

Terminal window
katnip check game.knip true

The log goes to examples/log.txt relative to your current working directory — so run it from a directory that has an examples/ folder, or the write fails. This is a compiler debugging aid, not a user-facing feature.

Diagnostics carry a source span with line and column, and are printed with the offending line and a caret:

Semantic Error: No overload of 'looks.say' matches these arguments
at line 3, column 9
|
3 | looks.say("Hello from Katnip!", "two");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Multiple errors are reported per run. The parser recovers into error nodes, so semantic errors surface alongside syntax errors rather than hiding behind the first one — you get the whole list.

Each diagnostic is followed by an internal stack trace into the compiler. That is for compiler development; the first few lines are the part meant for you.

Code Meaning
0 success
1 the build failed — diagnostics printed

Suitable for CI:

Terminal window
katnip check src/game.knip || exit 1