Bypass rendering and parsing assembly

This commit is contained in:
Andrew Morris
2023-03-06 20:06:21 +11:00
parent bab3ada9b8
commit 3565d2464f
9 changed files with 44 additions and 30 deletions

View File

@@ -1,12 +0,0 @@
use std::rc::Rc;
use crate::assembler::assemble_module;
use crate::assembly_parser::parse_module;
pub fn assemble(content: &str) -> Rc<Vec<u8>> {
let module = parse_module(content);
let output = assemble_module(&module);
// TODO: Don't use Rc
return Rc::new(output);
}

View File

@@ -1,11 +1,14 @@
use std::collections::{HashMap, HashSet};
use std::{
collections::{HashMap, HashSet},
rc::Rc,
};
use crate::asm::{
Array, Builtin, Class, Definition, DefinitionContent, Function, Instruction, InstructionOrLabel,
Label, LabelRef, Module, Object, Pointer, Register, Value,
};
pub fn assemble_module(module: &Module) -> Vec<u8> {
pub fn assemble(module: &Module) -> Rc<Vec<u8>> {
let mut assembler = Assembler {
output: Vec::new(),
fn_data: Default::default(),
@@ -17,7 +20,8 @@ pub fn assemble_module(module: &Module) -> Vec<u8> {
assembler.module(module);
return assembler.output;
// TODO: Don't use Rc
return Rc::new(assembler.output);
}
struct Assembler {

View File

@@ -64,10 +64,10 @@ pub fn parse(source: &str) -> (Option<swc_ecma_ast::Program>, Vec<Diagnostic>) {
return (result.ok(), diagnostics);
}
#[derive(Default, serde::Serialize)]
#[derive(Default)]
pub struct CompilerOutput {
pub diagnostics: Vec<Diagnostic>,
pub assembly: Vec<String>,
pub module: Module,
}
pub fn compile_program(program: &swc_ecma_ast::Program) -> CompilerOutput {
@@ -79,7 +79,7 @@ pub fn compile_program(program: &swc_ecma_ast::Program) -> CompilerOutput {
return CompilerOutput {
diagnostics: compiler.diagnostics,
assembly: module.as_lines(),
module,
};
}

View File

@@ -1,5 +1,4 @@
mod asm;
mod assemble;
mod assembler;
mod assembly_parser;
mod capture_finder;
@@ -11,7 +10,9 @@ mod name_allocator;
mod scope;
mod scope_analysis;
pub use assemble::assemble;
pub use assembler::assemble;
pub use assembly_parser::parse_module;
pub use compile::compile;
pub use compile::CompilerOutput;
pub use diagnostic::Diagnostic;
pub use diagnostic::DiagnosticLevel;

View File

@@ -1,6 +1,6 @@
use wasm_bindgen::prelude::*;
use valuescript_compiler::DiagnosticLevel;
use valuescript_compiler::{CompilerOutput, Diagnostic, DiagnosticLevel};
use valuescript_vm::ValTrait;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
@@ -35,7 +35,7 @@ fn run_to_result(source: &str) -> RunResult {
};
}
let bytecode = valuescript_compiler::assemble(compiler_output.assembly.join("\n").as_str());
let bytecode = valuescript_compiler::assemble(&compiler_output.module);
let mut vm = valuescript_vm::VirtualMachine::new();
let result = vm.run(&bytecode, &[]);
@@ -46,10 +46,27 @@ fn run_to_result(source: &str) -> RunResult {
};
}
#[derive(serde::Serialize)]
struct CompilerOutputWasm {
diagnostics: Vec<Diagnostic>,
assembly: Vec<String>,
}
impl CompilerOutputWasm {
fn from_compiler_output(output: CompilerOutput) -> CompilerOutputWasm {
CompilerOutputWasm {
diagnostics: output.diagnostics,
assembly: output.module.as_lines(),
}
}
}
#[wasm_bindgen]
pub fn compile(source: &str) -> String {
let output = valuescript_compiler::compile(source);
return serde_json::to_string(&output).expect("Failed json serialization");
return serde_json::to_string(&CompilerOutputWasm::from_compiler_output(output))
.expect("Failed json serialization");
}
#[wasm_bindgen]

View File

@@ -1,6 +1,6 @@
use std::process::exit;
use valuescript_compiler::assemble;
use valuescript_compiler::{assemble, parse_module};
pub fn assemble_command(args: &Vec<String>) {
if args.len() != 3 {
@@ -23,7 +23,9 @@ pub fn assemble_command(args: &Vec<String>) {
let content = read_result.expect("");
let output_filename = "out.vsb";
let bytecode = assemble(&content);
let module = parse_module(&content);
let bytecode = assemble(&module);
let write_result = std::fs::write(output_filename, &*bytecode);

View File

@@ -19,7 +19,7 @@ pub fn compile_command(args: &Vec<String>) {
let mut file = File::create("out.vsm").expect("Couldn't create out.vsm");
for line in compiler_output.assembly {
for line in compiler_output.module.as_lines() {
file
.write_all(line.as_bytes())
.expect("Failed to write line");

View File

@@ -1,7 +1,7 @@
use std::rc::Rc;
use std::{ffi::OsStr, path::Path, process::exit};
use valuescript_compiler::{assemble, compile};
use valuescript_compiler::{assemble, compile, parse_module};
use valuescript_vm::VirtualMachine;
use super::handle_diagnostics_cli::handle_diagnostics_cli;
@@ -79,14 +79,15 @@ fn to_bytecode(format: RunFormat, file_path: &String) -> Rc<Vec<u8>> {
let compiler_output = compile(&source);
handle_diagnostics_cli(file_path, &compiler_output.diagnostics);
return assemble(&compiler_output.assembly.join("\n"));
return assemble(&compiler_output.module);
}
RunFormat::Assembly => {
let file_content = std::fs::read_to_string(&file_path)
.expect(&std::format!("Failed to read file {}", file_path));
assemble(&file_content)
let module = parse_module(&file_content);
assemble(&module)
}
RunFormat::Bytecode => {

View File

@@ -57,7 +57,8 @@ mod tests {
}
}
let bytecode = assemble(&compiler_output.assembly.join("\n"));
// TODO: Also test rendering and parsing assembly
let bytecode = assemble(&compiler_output.module);
let mut vm = VirtualMachine::new();
let result = vm.run(&bytecode, &[]);