Implement imports of structs and type aliases #2

Merged
SeanOMik merged 3 commits from feat/struct-imports into main 2024-08-30 23:57:14 +00:00
2 changed files with 95 additions and 0 deletions
Showing only changes of commit b570c7789d - Show all commits

View File

@ -293,4 +293,89 @@ fn main() -> vec4<f32> {
assert!(out.contains("var<storage, read> light_indices"), "missing light_indices binding: {}", out);
assert!(out.contains("var<storage, read_write> some_buffer"), "missing some_buffer binding: {}", out);
}
#[test]
fn import_struct() {
const BINDINGS_MODULE: &'static str =
r#"#define_module engine::lights
struct Light {
intensity: f32
}"#;
const IMPORTING_MODULE: &'static str =
r#"#define_module engine::main
#import engine::lights::{Light}
fn main() -> vec4<f32> {
// imports must be used to be generated
let base_light = Light(1.0);
return vec4<f32>(1.0);
}"#;
tracing_subscriber::fmt()
// enable everything
.with_max_level(tracing::Level::TRACE)
// sets this to be the default, global collector for this application.
.init();
let mut p = Processor::new();
p.parse_module(BINDINGS_MODULE).unwrap()
.expect("failed to find bindings module def");
let importing_mod = p.parse_module(IMPORTING_MODULE).unwrap()
.expect("failed to find main module def");
let out = p.process_file(&importing_mod, &IMPORTING_MODULE).unwrap();
assert!(out.contains("struct Light"), "missing `Light` struct definition: {}", out);
}
/* #[test]
fn import_type_aliases() {
const BINDINGS_MODULE: &'static str =
r#"#define_module engine::aliases
struct Light {
intensity: f32
}
struct Lights {
num: u32,
data: array<Light>
}
alias LightArray = Lights;"#;
const IMPORTING_MODULE: &'static str =
r#"#define_module engine::main
#import engine::aliases::{LightArray}
@group(0) @binding(0) var<uniform> lights: LightArray;
fn main() -> vec4<f32> {
// imports must be used to be generated
let light_num = lights.num;
return vec4<f32>(1.0);
}"#;
tracing_subscriber::fmt()
// enable everything
.with_max_level(tracing::Level::TRACE)
// sets this to be the default, global collector for this application.
.init();
let mut p = Processor::new();
p.parse_module(BINDINGS_MODULE).unwrap()
.expect("failed to find bindings module def");
let importing_mod = p.parse_module(IMPORTING_MODULE).unwrap()
.expect("failed to find main module def");
let out = p.process_file(&importing_mod, &IMPORTING_MODULE).unwrap();
assert!(out.contains("alias LightArray = Lights"), "missing use_anyway binding: {}", out);
assert!(out.contains("struct Light"), "missing `Light` struct definition: {}", out);
assert!(out.contains("struct Lights"), "missing `Lights` struct definition: {}", out);
} */
}

View File

@ -37,6 +37,14 @@ shader_value = { shader_value_bool | shader_value_num }
shader_var_type = { ":" ~ ws* ~ (shader_external_variable | shader_type) }
shader_const_def = { "const" ~ ws ~ shader_ident ~ (ws* ~ shader_var_type)? ~ ws* ~ "=" ~ ws* ~ shader_value ~ ";" }
shader_struct_def = {
"struct" ~ ws ~ shader_ident ~ ws* ~ "{" ~ NEWLINE ~
ws* ~ shader_ident ~ shader_var_type ~
("," ~ NEWLINE ~ ws* ~ shader_ident ~ shader_var_type)* ~
("," ~ NEWLINE)? ~
"}"
}
shader_type_alias_def = { "alias" ~ ws ~ shader_ident ~ ws* ~ "=" ~ ws* ~ (shader_external_variable | shader_type) ~ ";" }
shader_group_binding = { "@group(" ~ NUMBER ~ ") @binding(" ~ NUMBER ~ ")" }
shader_binding_var_constraint = {
@ -58,8 +66,10 @@ shader_fn_def = {
// a line of shader code, including white space
shader_code_line = {
shader_fn_def |
(shader_struct_def ~ newline?) |
(shader_const_def ~ newline?) |
(shader_binding_def ~ newline?) |
(shader_type_alias_def ~ newline?) |
ws* ~ newline
}
//shader_code_line = { shader_fn_def | shader_const_def | (ws* ~ (shader_external_code | shader_code)* ~ ws*) }