output per-item imports from single-level imports

This commit is contained in:
SeanOMik 2024-08-02 22:27:31 -04:00
parent 6e4efa973b
commit 5ee537763f
2 changed files with 50 additions and 17 deletions

View File

@ -1,6 +1,5 @@
#define_module simple #define_module simple
#import inner::some_include::{scalar, mult_some_nums} #import inner::some_include::{scalar, mult_some_nums}
#import outer
fn do_something_cool(in: f32) -> f32 { fn do_something_cool(in: f32) -> f32 {
return scalar * mult_some_nums(in, 2.0); return scalar * mult_some_nums(in, 2.0);

View File

@ -26,6 +26,8 @@ pub enum PreprocessorError {
FormatError(#[from] std::fmt::Error), FormatError(#[from] std::fmt::Error),
#[error("unknown module import '{module}', in {from_path}")] #[error("unknown module import '{module}', in {from_path}")]
UnknownModule { from_path: PathBuf, module: String }, UnknownModule { from_path: PathBuf, module: String },
#[error("in {from_path}: unknown import from '{module}': `{item}`")]
UnknownImport { from_path: PathBuf, module: String, item: String },
#[error("import usage from `{from_module}` conflicts with local variable/function: `{name}`")] #[error("import usage from `{from_module}` conflicts with local variable/function: `{name}`")]
ConflictingImport { from_module: String, name: String }, ConflictingImport { from_module: String, name: String },
} }
@ -87,8 +89,8 @@ fn main() {
} }
} }
/* let out = p.process_file("shaders/simple.wgsl").unwrap(); let out = p.process_file("shaders/simple.wgsl").unwrap();
fs::write("out.wgsl", out).unwrap(); */ fs::write("out.wgsl", out).unwrap();
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
@ -174,7 +176,6 @@ impl Processor {
let command_line = pairs.next().unwrap(); let command_line = pairs.next().unwrap();
match command_line.as_rule() { match command_line.as_rule() {
//Rule::import_command => (),
Rule::import_types_command => { Rule::import_types_command => {
let mut inner = command_line.into_inner(); let mut inner = command_line.into_inner();
let import_module_command = inner.next().unwrap(); let import_module_command = inner.next().unwrap();
@ -222,9 +223,9 @@ impl Processor {
let fn_name = pairs.next().unwrap().as_str().to_string(); let fn_name = pairs.next().unwrap().as_str().to_string();
println!("fn: {fn_name:?}"); println!("fn: {fn_name:?}");
let fn_args = pairs.next().unwrap().as_str(); /* let fn_args = pairs.next().unwrap().as_str();
let ret_type = pairs.next().unwrap().as_str(); let ret_type = pairs.next().unwrap().as_str();
let fn_body_pair = pairs.next().unwrap(); let fn_body_pair = pairs.next().unwrap(); */
let line_span = line.as_span(); let line_span = line.as_span();
let start_pos = line_span.start(); let start_pos = line_span.start();
@ -277,15 +278,13 @@ impl Processor {
} }
Rule::cws => (), Rule::cws => (),
Rule::newline => (), Rule::newline => (),
_ => { _ => unimplemented!("ran into unhandled rule: {:?}", line.as_span())
unimplemented!("ran into unhandled rule: {:?}", line.as_span());
}
} }
} }
} }
Rule::newline => (), Rule::newline => (),
Rule::EOI => (), Rule::EOI => (),
_ => unreachable!(), _ => unimplemented!("ran into unhandled rule: {:?}", record.as_span())
} }
} }
@ -378,17 +377,51 @@ impl Processor {
out_string.write_str(&end_header)?; out_string.write_str(&end_header)?;
} }
Rule::import_types_command => { Rule::import_types_command => {
let mut shader_file_pairs = command_line.into_inner(); let mut import_command = command_line.into_inner();
let module_path = shader_file_pairs.next().unwrap(); let import_module_command = import_command.next().unwrap();
let module_path = module_path.into_inner().next().unwrap(); let module_path = import_module_command.into_inner().next().unwrap();
let module_path = module_path.as_str(); let module_path = module_path.as_str();
let importing_from_mod = self.modules.get(module_path).ok_or_else(|| {
PreprocessorError::UnknownModule {
from_path: path.as_ref().to_path_buf(),
module: module_path.into(),
}
})?;
let module_raw_src = fs::read_to_string(&importing_from_mod.path)?;
for import in import_command {
let import_ident = import.as_str();
let def = importing_from_mod.functions.get(import_ident)
.or_else(|| importing_from_mod.constants.get(import_ident))
.ok_or_else(|| {
PreprocessorError::UnknownImport {
from_path: path.as_ref().to_path_buf(),
module: module_path.into(),
item: import_ident.to_string(),
}
})?;
let import_text = &module_raw_src[def.start_pos..def.end_pos];
//println!("must add:\n{import_text}");
out_string.write_fmt(format_args!("// START OF IMPORT ITEM {} FROM {}\n", import_ident, module_path))?;
out_string.write_str(import_text)?;
out_string.write_fmt(format_args!("\n// END OF IMPORT ITEM {} FROM {}\n\n", import_ident, module_path))?;
}
//todo!();
/* importing_from_mod.functions.get()
let imports: Vec<&str> = let imports: Vec<&str> =
shader_file_pairs.map(|i| i.as_str()).collect(); shader_file_pairs.map(|i| i.as_str()).collect();
println!("found module import: {}", module_path); println!("found module import: {}", module_path);
println!("imports: {imports:?}"); println!("imports: {imports:?}");
todo!(); todo!(); */
/* let imported_mod = self.modules.get(shader_file).ok_or_else(|| { /* let imported_mod = self.modules.get(shader_file).ok_or_else(|| {
PreprocessorError::UnknownModule { PreprocessorError::UnknownModule {
from_path: path.as_ref().to_path_buf(), from_path: path.as_ref().to_path_buf(),
@ -397,7 +430,7 @@ impl Processor {
})?; */ })?; */
} }
Rule::define_module_command => (), Rule::define_module_command => (),
_ => unreachable!(), _ => unimplemented!("ran into unhandled rule: {:?}", command_line.as_span()),
} }
} }
Rule::cws => (), Rule::cws => (),
@ -459,7 +492,8 @@ impl Processor {
let input = line.as_str(); let input = line.as_str();
out_string.write_str(&input)?; out_string.write_str(&input)?;
} }
_ => unreachable!(), Rule::newline => (),
_ => unimplemented!("ran into unhandled rule: {:?}", line.as_span()),
} }
} }
} }
@ -468,7 +502,7 @@ impl Processor {
out_string.write_str(&input)?; out_string.write_str(&input)?;
} }
Rule::EOI => (), Rule::EOI => (),
_ => unreachable!(), _ => unimplemented!("ran into unhandled rule: {:?}", record.as_span()),
} }
} }