dogamak / ttk91-rs

Crate for reading, writing, compiling and executing TTK91 code.
0 stars 0 forks source link

Add support for generating source maps #10

Closed dogamak closed 4 years ago

dogamak commented 4 years ago

This PR adds a Program::compile_with_sourcemap method which in addition to creating the bytecode program generates a source map. This source map contains mappings from memory locations to assembly lines for instructions and variables.

Source map generation works as of now, but the API is ugly at best. Documentation is also missing.

This feature required an additional dependency nom_locate which keeps track of the input line numbers. Line numbers are stored in symbolic::Program::instructions at the time of parsing. It would be nice to have a way of disabling this for additional performance.

A new abstraction was created for compilation targets, the CompilationTarget trait. It is currently implemented for bytecode::Program and (T, SourceMap<T::Location>) where T is any type implementing CompilationTarget. Additional targets could include Vec<u32>.

dogamak commented 4 years ago

The API was changed in 4cc7f9d so that SourceMap<T> where T: CompileTarget itself implements the CompileTarget trait. The SourceMap struct contains both the compiled program and the generated sourcemap. Example below:

let program = Program::parse(source)?;
let result: SourceMap<bytecode::Program> = compile(program);
let program = result.program;
let source_map = result.source_map;