From 5ee537763f0f52a565c48b385ddbb3a4e5b79e8d Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Fri, 2 Aug 2024 22:27:31 -0400 Subject: [PATCH] output per-item imports from single-level imports --- shaders/simple.wgsl | 1 - src/main.rs | 66 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/shaders/simple.wgsl b/shaders/simple.wgsl index 2314402..ac5fbfc 100644 --- a/shaders/simple.wgsl +++ b/shaders/simple.wgsl @@ -1,6 +1,5 @@ #define_module simple #import inner::some_include::{scalar, mult_some_nums} -#import outer fn do_something_cool(in: f32) -> f32 { return scalar * mult_some_nums(in, 2.0); diff --git a/src/main.rs b/src/main.rs index 49e7aa2..9b00ff0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,8 @@ pub enum PreprocessorError { FormatError(#[from] std::fmt::Error), #[error("unknown module import '{module}', in {from_path}")] 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}`")] ConflictingImport { from_module: String, name: String }, } @@ -87,8 +89,8 @@ fn main() { } } - /* let out = p.process_file("shaders/simple.wgsl").unwrap(); - fs::write("out.wgsl", out).unwrap(); */ + let out = p.process_file("shaders/simple.wgsl").unwrap(); + fs::write("out.wgsl", out).unwrap(); } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] @@ -174,7 +176,6 @@ impl Processor { let command_line = pairs.next().unwrap(); match command_line.as_rule() { - //Rule::import_command => (), Rule::import_types_command => { let mut inner = command_line.into_inner(); let import_module_command = inner.next().unwrap(); @@ -222,9 +223,9 @@ impl Processor { let fn_name = pairs.next().unwrap().as_str().to_string(); 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 fn_body_pair = pairs.next().unwrap(); + let fn_body_pair = pairs.next().unwrap(); */ let line_span = line.as_span(); let start_pos = line_span.start(); @@ -277,15 +278,13 @@ impl Processor { } Rule::cws => (), Rule::newline => (), - _ => { - unimplemented!("ran into unhandled rule: {:?}", line.as_span()); - } + _ => unimplemented!("ran into unhandled rule: {:?}", line.as_span()) } } } Rule::newline => (), Rule::EOI => (), - _ => unreachable!(), + _ => unimplemented!("ran into unhandled rule: {:?}", record.as_span()) } } @@ -378,17 +377,51 @@ impl Processor { out_string.write_str(&end_header)?; } Rule::import_types_command => { - let mut shader_file_pairs = command_line.into_inner(); - let module_path = shader_file_pairs.next().unwrap(); - let module_path = module_path.into_inner().next().unwrap(); + let mut import_command = command_line.into_inner(); + let import_module_command = import_command.next().unwrap(); + let module_path = import_module_command.into_inner().next().unwrap(); 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> = shader_file_pairs.map(|i| i.as_str()).collect(); println!("found module import: {}", module_path); println!("imports: {imports:?}"); - todo!(); + todo!(); */ /* let imported_mod = self.modules.get(shader_file).ok_or_else(|| { PreprocessorError::UnknownModule { from_path: path.as_ref().to_path_buf(), @@ -397,7 +430,7 @@ impl Processor { })?; */ } Rule::define_module_command => (), - _ => unreachable!(), + _ => unimplemented!("ran into unhandled rule: {:?}", command_line.as_span()), } } Rule::cws => (), @@ -459,7 +492,8 @@ impl Processor { let input = line.as_str(); 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)?; } Rule::EOI => (), - _ => unreachable!(), + _ => unimplemented!("ran into unhandled rule: {:?}", record.as_span()), } }