Compile and Run v6 (Capstone)

hard · compilers, bytecode, vm, capstone, types

Compile and Run v6 (Capstone)

Connect the full v6 compiler pipeline (modules + static types) and execute the program.

Function signature

func CompileAndRun(src string) (Value, error)

Pipeline requirements

Your function must:

  1. Lex source into tokens (Lex)
  2. Parse tokens into a program AST (Parse)
  3. Run semantic analysis (Analyze)
  4. Compile modules to bytecode (CompileModules)
  5. Link modules into a single program (Link)
  6. Encode to BC2 text (Encode)
  7. Decode back to bytecode (Decode)
  8. Execute in the VM (Run on a new VM)

Return the final VM result.

Error handling

  • If lexing or parsing fails, return that error.
  • If semantic analysis returns errors, return the first error as error.
  • If linking, decoding, or execution fails, return that error.

Notes

  • Function parameter/return signatures and exported-variable annotations are required in v6. Local annotations are optional when inference succeeds.
  • Static acceptance does not remove runtime bounds, arithmetic, or nil-related checks; the checker does not prove all-path return coverage.
  • The helper functions and types are provided in this file.
  • Keep the implementation 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
    Join Discord