v5 Bytecode: compiling modules
Lesson, slides, and applied problem sets.
View SlidesLesson
v5 Bytecode: compiling modules
Module initialization
Each module compiles to init code:
- Enter a module scope.
- Define all locals and functions.
- Build a map of exports.
- Exit scope.
- Define a global const named after the module.
Pseudo-emission:
ENTER_SCOPE
... module statements ...
PUSH_STR "add"
LOAD add
MAKE_MAP 1
EXIT_SCOPE
DEFINE_CONST math
Imports
An import becomes a const alias in the module scope:
LOAD math
DEFINE_CONST m
Member access
m.add compiles as map indexing:
LOAD m
PUSH_STR "add"
GET_INDEX
No new opcodes needed.