Documentation
¶
Overview ¶
Package compiler lowers Statement IR (internal/gen/stmt) into bytecode images runnable by the register VM (internal/vm).
Per M-BYTECODE-VM §10 Phase 2C, this package:
- reads from internal/gen/stmt (read-only)
- writes to internal/bytecode (mutates the image)
- does NOT import internal/vm, internal/eval, internal/core, internal/lower
The compiler is intentionally dumb: a single linear pass per function with a bump-style register allocator. No SSA, no graph coloring, no liveness intervals, no spilling. The corpus targeted by Phase 2C (12 golden tests) is well within the 256-register limit.
Milestone scope:
M1 (this file) : skeleton, literals, arithmetic, locals M2 : control flow, comparisons, short-circuit M3 : calls, recursion, TCO M4 : lambdas, closures, free-variable analysis M5 : collections, ADTs, pattern matching M6 : builtins + golden parity gate
Index ¶
Constants ¶
This section is empty.
Variables ¶
var BuiltinTable = []string{
"_show",
"_len",
"_list_get",
"_list_tail",
"_concat_String",
"_record_get",
"_not_Bool",
"_intToFloat",
"__list_length",
"_concat_List",
"__str_len",
"__str_compare",
"__str_eq",
"__str_find",
"__str_slice",
"__str_trim",
"__str_upper",
"__str_lower",
"__str_split",
"__str_chars",
"__str_startsWith",
"__str_endsWith",
"__str_join",
"__str_words",
"__str_splitAny",
"__str_replace",
"__str_replaceMany",
"__str_startsWithIC",
"__str_charAt",
"__str_charCode",
"__str_decodeQP",
"__escapeXml",
"__string_intToStr",
"__string_floatToStr",
"__stringToInt",
"__stringToFloat",
"__math_sin",
"__math_cos",
"__math_tan",
"__math_asin",
"__math_acos",
"__math_atan",
"__math_atan2",
"__math_sqrt",
"__math_pow",
"__math_exp",
"__math_log",
"__math_log10",
"__math_floor",
"__math_ceil",
"__math_round",
"__math_abs_Float",
"__math_abs_Int",
"__math_PI",
"__math_E",
"_floatToInt",
"_mod_Int",
"__float_to_int",
"__int_to_float",
"__list_nth",
"__list_member",
"__list_dedup",
"__list_difference",
"__list_intersect",
"__list_union",
"__json_encode",
"__json_decode",
"__json_repair",
"__xmlElement",
"__xmlText",
"__xmlComment",
"__xml_getText",
"__xml_getTag",
"__xml_serialize",
"__xml_serializeWithDecl",
"__xml_parse",
"__xml_parseElements",
"__xml_parseWithLimit",
"__xml_findAll",
"__xml_findFirst",
"__xml_getAttr",
"__xml_getChildren",
"__xml_findAllTexts",
"__xml_findAllAttrs",
}
BuiltinTable is the canonical, source-ordered table of pure builtins reachable from the Phase 2C golden corpus. Indices in this table become the B field of OpBuiltinCall and are interpreted by the VM's builtin dispatch table (internal/vm/builtins.go).
Adding a builtin is a two-step process:
- Append its name here.
- Add a matching entry in vm.BuiltinTable.
The two tables MUST stay in lockstep — there is a runtime sanity check at VM startup that the lengths agree.
var HOFBuiltinTable = []string{
"__list_map",
"__list_filter",
"__list_foldl",
"__str_foldChars",
"__str_foldSlices",
"__str_mapSlicesJoin",
"__xml_parseFold",
}
HOFBuiltinTable lists builtins that take closure arguments. These are dispatched via OpBuiltinCallHOF, which passes the VM as a ClosureCaller so the builtin can invoke its closure arguments. Order MUST match vm.HOFBuiltinTable.
Functions ¶
func Compile ¶
func Compile(prog *stmt.Program) (*bytecode.BytecodeImage, error)
Compile lowers a Statement IR program into a bytecode image. The first exported function in prog.FuncDecls is set as the entry point if any function is exported; otherwise the first function is used.
On success, the returned image has been validated via BytecodeImage.Validate(). A validation failure is treated as a compiler bug and surfaces as an error.
Types ¶
This section is empty.