ilusm.dev

comp

ilusm AST compiler - recursively walks an AST node tree dispatching on numeric node type tags and emits ilusm source code for all node kinds: number/string/bool/nil literals, identifiers, lists, objects, function calls, binary operators (+, -, *, /, %, comparisons), unary operators, if/else, while, assignment, named and anonymous functions, blocks, use, ret, and try. Provides a top-level cmplt entry point that wraps the emitted code with a generated-file header.

Load with: use comp

What this module does

comp is a code-generation backend that takes an ilusm AST (as produced by the parser) and emits ilusm source text. This is useful for AST transformations that want to output runnable code, for pretty-printing parsed programs, or as a stepping stone toward other target backends. Node types are identified by integer constants (1–18) matching the parser's NODE_* values.

Quick example

use comp
use prs   # or any AST-producing parser

ast = prs.parse("x = 1 + 2\nprn(x)")

# Emit ilusm source from the AST
src = cmplt(ast, "example.ilu")
prn(src)
# # Generated from example.ilu
#
# x = 1 + 2
# prn(x)

Functions

Code generation

genc(node, ctx)

Recursively emits ilusm source code for an AST node. Dispatches on node.type integer: 1=number, 2=string, 3=identifier, 4=bool, 5=nil, 6=list, 7=object, 8=call, 9=binary op, 10=unary, 11=if, 12=while, 13=assignment, 14=function, 15=block, 16=use, 17=return, 18=try. Returns "/* unknown node */" for unrecognised types.

cmplt(ast, filename)

Top-level compile entry: calls genc on the AST with a fresh context, prepends a # Generated from filename header, and returns the full source string.

Notes

  • Requires txt.
  • The ctx object passed to genc is currently unused - reserved for future scope/symbol-table tracking.