mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
New compile command using linking
This commit is contained in:
29
valuescript_compiler/src/compile.rs
Normal file
29
valuescript_compiler/src/compile.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{asm::Module, gather_modules, link_module, Diagnostic, ResolvedPath};
|
||||
|
||||
pub struct CompileResult {
|
||||
pub module: Option<Module>,
|
||||
pub diagnostics: HashMap<ResolvedPath, Vec<Diagnostic>>,
|
||||
}
|
||||
|
||||
pub fn compile<ReadFile>(entry_point: ResolvedPath, read_file: ReadFile) -> CompileResult
|
||||
where
|
||||
ReadFile: Fn(&str) -> Result<String, String>,
|
||||
{
|
||||
let gm = gather_modules(entry_point.clone(), read_file);
|
||||
let mut link_module_result = link_module(&gm.entry_point, &gm.modules);
|
||||
|
||||
let mut result = CompileResult {
|
||||
module: link_module_result.module,
|
||||
diagnostics: gm.diagnostics,
|
||||
};
|
||||
|
||||
result
|
||||
.diagnostics
|
||||
.entry(entry_point.clone())
|
||||
.or_default()
|
||||
.append(&mut link_module_result.diagnostics);
|
||||
|
||||
result
|
||||
}
|
||||
@@ -2,6 +2,7 @@ mod asm;
|
||||
mod assembler;
|
||||
mod assembly_parser;
|
||||
mod capture_finder;
|
||||
mod compile;
|
||||
mod diagnostic;
|
||||
mod expression_compiler;
|
||||
mod function_compiler;
|
||||
@@ -16,6 +17,7 @@ mod scope_analysis;
|
||||
|
||||
pub use assembler::assemble;
|
||||
pub use assembly_parser::parse_module;
|
||||
pub use compile::compile;
|
||||
pub use diagnostic::Diagnostic;
|
||||
pub use diagnostic::DiagnosticLevel;
|
||||
pub use gather_modules::gather_modules;
|
||||
|
||||
@@ -12,7 +12,7 @@ use crate::{asm::Module, Diagnostic};
|
||||
|
||||
pub struct LinkModuleResult {
|
||||
pub module: Option<Module>,
|
||||
pub diagnostics: Vec<Diagnostic>,
|
||||
pub diagnostics: Vec<Diagnostic>, // TODO: Associate paths/spans properly
|
||||
}
|
||||
|
||||
pub fn link_module(
|
||||
|
||||
@@ -3,8 +3,7 @@ use std::io::Write;
|
||||
use std::process::exit;
|
||||
|
||||
use super::handle_diagnostics_cli::handle_diagnostics_cli;
|
||||
use valuescript_compiler::gather_modules;
|
||||
use valuescript_compiler::link_module;
|
||||
use valuescript_compiler::compile;
|
||||
use valuescript_compiler::resolve_path;
|
||||
use valuescript_compiler::ResolvedPath;
|
||||
|
||||
@@ -31,20 +30,15 @@ pub fn compile_command(args: &Vec<String>) {
|
||||
|
||||
let resolved_entry_path = resolve_path(&cwd_file, entry_path);
|
||||
|
||||
let gm = gather_modules(resolved_entry_path, |path| {
|
||||
let compile_result = compile(resolved_entry_path, |path| {
|
||||
std::fs::read_to_string(path).map_err(|err| err.to_string())
|
||||
});
|
||||
|
||||
for (path, diagnostics) in gm.diagnostics.iter() {
|
||||
for (path, diagnostics) in compile_result.diagnostics.iter() {
|
||||
handle_diagnostics_cli(&path.path, diagnostics);
|
||||
}
|
||||
|
||||
let link_module_result = link_module(&gm.entry_point, &gm.modules);
|
||||
|
||||
// FIXME: Diagnostics from link_module should have paths associated
|
||||
handle_diagnostics_cli(&gm.entry_point.path, &link_module_result.diagnostics);
|
||||
|
||||
let module = link_module_result
|
||||
let module = compile_result
|
||||
.module
|
||||
.expect("Should have exited if module is None");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user