Compile and Run v4 (Capstone)

hard · compilers, bytecode, vm, capstone

Compile and Run v4 (Capstone)

Connect the full v4 compiler pipeline 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 to bytecode (Compile)
  5. Encode to BC2 text (Encode)
  6. Decode back to bytecode (Decode)
  7. Execute in the VM (Run on a new VM)

Return the final VM result.

This function is intentionally the integration layer.

It should do the following in order:

  1. call the stage
  2. inspect error
  3. abort immediately on failure
  4. move to next stage on success

Error handling

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

Notes

  • 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