Bytecode IO v3
Bytecode IO v3
Implement a text encoder/decoder for the BC2 bytecode format (closures + function IDs).
Function signatures
func Encode(bc Bytecode) string
func Decode(src string) (Bytecode, error)
BC2 format
BC2
FUNC 0 add a b
LOAD a
LOAD b
ADD
RETURN
END
MAIN
MAKE_CLOSURE 0
PUSH_NUM 1
PUSH_NUM 2
CALL 2
END
Rules:
- The first non-empty line must be
BC2. FUNC <id> <name> <param...>starts a function section.MAINstarts the main section.ENDends the current section.- Empty lines and lines starting with
#are ignored.
Instruction encoding
One instruction per line. Examples:
PUSH_NUM 42PUSH_STR "hello"PUSH_BOOL 0PUSH_NILLOAD nameSTORE nameDEFINE_VAR nameDEFINE_CONST nameCALL 2MAKE_CLOSURE 7MAKE_ARRAY 3MAKE_MAP 2GET_INDEXSET_INDEXJUMP 10JUMP_IF_FALSE 20RETURNENTER_SCOPEEXIT_SCOPEPOP
Strings use double quotes with escapes: \", \\, \n, \t.
Requirements
Encodemust produce a valid BC2 document.Decodemust parse the document into an equivalentBytecode.- On malformed input,
Decodereturns a non-nil error.
Notes
- Function parameter names are listed on the
FUNCline. - Keep the encoder/decoder readable.
Before implementation
Use the module’s worked reconstruction and independent debugging lab to predict an intermediate artifact first. Preserve one failing counterexample while implementing the stated API. The lesson’s transfer task uses a fresh case after the guided trace.
Run tests to see results
No issues detected