Skip to content

Commit

Permalink
Started cleaning up and refactoring workspace compilation code
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Dec 8, 2024
1 parent 202ada2 commit de6c7f5
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 193 deletions.
62 changes: 32 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,38 +121,40 @@ fn build_project(build_command: BuildCommand) {

if metadata.is_dir() {
compile_workspace(&mut compiler, filepath, None);
} else {
if filepath.extension().unwrap_or_default() == "h" {
let source_files = compiler.source_files;

let content = std::fs::read_to_string(filepath)
.map_err(|err| {
eprintln!("{}", err);
exit(1);
})
.unwrap();

let header_key = source_files.add(filepath.into(), content);

let header_contents = source_files
.get(header_key)
.content()
.chars()
.into_text_stream(header_key)
.into_text();

let preprocessed = exit_unless(
c::preprocessor::preprocess(header_contents, &diagnostics),
&source_files,
);

println!("{preprocessed:?}");
return;
}
return;
}

let project_folder = filepath.parent().unwrap();
compile_single_file_only(&mut compiler, project_folder, filepath);
// Experimental header parsing
if filepath.extension().unwrap_or_default() == "h" {
let source_files = compiler.source_files;

let content = std::fs::read_to_string(filepath)
.map_err(|err| {
eprintln!("{}", err);
exit(1);
})
.unwrap();

let header_key = source_files.add(filepath.into(), content);

let header_contents = source_files
.get(header_key)
.content()
.chars()
.into_text_stream(header_key)
.into_text();

let preprocessed = exit_unless(
c::preprocessor::preprocess(header_contents, &diagnostics),
&source_files,
);

println!("{preprocessed:?}");
return;
}

let project_folder = filepath.parent().unwrap();
compile_single_file_only(&mut compiler, project_folder, filepath);
}

fn exit_unless<T, E: Show>(result: Result<T, E>, source_files: &SourceFiles) -> T {
Expand Down
18 changes: 7 additions & 11 deletions src/show.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use crate::source_files::{Source, SourceFiles};
use crate::source_files::SourceFiles;

pub trait Show {
fn show(&self, w: &mut dyn std::fmt::Write, source_files: &SourceFiles) -> std::fmt::Result;

fn eprintln(self: &Self, source_files: &SourceFiles) {
let mut message = String::new();
self.show(&mut message, source_files).unwrap();
eprintln!("{}", message);
}
}

pub fn into_show<T: Show + 'static>(show: T) -> Box<dyn Show> {
Box::new(show)
}

pub fn error_println(message: &str, source: Source, source_files: &SourceFiles) {
eprintln!(
"{}:{}:{}: error: {}",
source_files.get(source.key).filename(),
source.location.line,
source.location.column,
message,
)
}
25 changes: 15 additions & 10 deletions src/workspace/compile/module/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
ast::Settings,
compiler::Compiler,
data_units::ByteUnits,
diagnostics::ErrorDiagnostic,
inflow::{Inflow, IntoInflow},
lexer::Lexer,
Expand All @@ -15,19 +16,18 @@ use crate::{
};
use std::path::Path;

pub struct CompiledModule<'a, I: Inflow<Token> + 'a> {
pub total_file_size: ByteUnits,
pub remaining_input: Input<'a, I>,
pub settings: Settings,
pub source_file: SourceFileKey,
}

pub fn compile_module_file<'a>(
compiler: &Compiler<'a>,
_fs: &Fs,
path: &Path,
) -> Result<
(
usize,
Input<'a, impl Inflow<Token> + 'a>,
Settings,
SourceFileKey,
),
Box<dyn Show + 'a>,
> {
) -> Result<CompiledModule<'a, impl Inflow<Token> + 'a>, Box<dyn Show + 'a>> {
let content = std::fs::read_to_string(path)
.map_err(ErrorDiagnostic::plain)
.map_err(into_show)?;
Expand Down Expand Up @@ -64,5 +64,10 @@ pub fn compile_module_file<'a>(
)));
};

Ok((content.len(), input, settings, key))
Ok(CompiledModule {
total_file_size: ByteUnits::of(content.len().try_into().unwrap()),
remaining_input: input,
settings,
source_file: key,
})
}
Loading

0 comments on commit de6c7f5

Please sign in to comment.