Compile to Bytecode v5
Compile to Bytecode v5
Lower v5 modules into stack-based bytecode with module initialization and export maps.
Function signature
func CompileModules(prog *Program) []ModuleBytecode
Types
Use the provided types:
ModuleBytecodewithName,Imports,Exports, andBytecodeBytecodewithMain []InstrandFunctions []FunctionFunctionwithID,Name,Params, andCode []InstrInstrwithOp,Int, andStr
Module initialization
Each module compiles to init code:
ENTER_SCOPE
(import aliases)
(module statements)
PUSH_STR "exportName"
LOAD exportName
MAKE_MAP N
EXIT_SCOPE
DEFINE_CONST moduleName
Imports
For each import X as Y emit:
LOAD X
DEFINE_CONST Y
Member access
m.add compiles to:
LOAD m
PUSH_STR "add"
GET_INDEX
Notes
- Assume the AST is already valid.
- Preserve module order from the program.
Exportsshould list exported names in the order discovered.- Keep the compiler readable and straightforward.
Run tests to see results
No issues detected