From bb006c1ba2cbf8798ce4d909984c8fdd3ae41ba8 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Wed, 8 Jul 2026 10:11:26 +0200 Subject: [PATCH 1/4] Added global include --- crates/codegen/src/lib.rs | 27 ++++++++++++++++++++++++++- crates/codegen/src/top_level.rs | 6 ++++++ crates/parser/src/ast/top_level.rs | 1 + crates/parser/src/grammar.pest | 6 +++++- crates/parser/src/parser/items/mod.rs | 2 ++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 6d9b007..75ae198 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -3,7 +3,7 @@ pub mod expr; pub mod statement; pub mod top_level; -use std::path::PathBuf; +use std::{collections::HashMap, path::PathBuf}; use mist_parser::{ ast::*, @@ -29,6 +29,7 @@ pub trait GetRust { pub struct RustCodegen { output: String, indent: usize, + pub crates: HashMap>, pub mapping: Mapping, position: RustMap, } @@ -38,6 +39,7 @@ impl RustCodegen { Self { output: String::new(), indent: 0, + crates: HashMap::new(), mapping: Mapping::new(mist_path), position: RustMap(1, 0), } @@ -84,6 +86,29 @@ impl RustCodegen { tl.gen_rust(&mut ctx, self); } + let mut crates = HashMap::new(); + std::mem::swap(&mut self.crates, &mut crates); + + for (c, items) in crates { + self.add("mod "); + c.gen_rust(&mut ctx, self); + self.addln(" {"); + self.indent += 1; + + self.add_indented("extern crate "); + c.gen_rust(&mut ctx, self); + self.addln(";"); + + for item in items { + self.add_indented("pub use "); + item.gen_rust(&mut ctx, self); + self.addln("::*;"); + } + + self.indent -= 1; + self.addln("}"); + } + self.output.clone() } diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index 004a42b..b65190c 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -326,6 +326,12 @@ impl GenRust for TopLevelKind { constructor, items, ), + Self::IncludeGlobal(incl) => { + cg.crates + .entry(incl.0[0].clone()) + .or_insert(Vec::new()) + .push(incl.clone()); + } } } } diff --git a/crates/parser/src/ast/top_level.rs b/crates/parser/src/ast/top_level.rs index 1762de9..a759d33 100644 --- a/crates/parser/src/ast/top_level.rs +++ b/crates/parser/src/ast/top_level.rs @@ -28,6 +28,7 @@ pub struct TopLevel(pub Spanned, pub Vec); pub enum TopLevelKind { ModAttribute, Import(Visibility, Path), + IncludeGlobal(Path), DeclareModule(Visibility, Identifier), ImplDecl(ImplDecl), StaticDecl(VarDeclStmt), diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 8ed88cb..6eb3973 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -258,6 +258,10 @@ param_list = { // IMPORTS / MODULES // ====================================================== +include_global = { + "#" ~ "include" ~ "<" ~ identifier ~ ("/" ~ identifier)* ~ ">" +} + import = { visibility? ~ "use" ~ static_path ~ semicolon } @@ -384,7 +388,7 @@ type_alias = { "type" ~ identifier ~ generics_decl? ~ "=" ~ type_expr ~ ";" } top_level = { mod_attribute - | (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias)) + | (attributes ~ (include_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias)) } // ====================================================== diff --git a/crates/parser/src/parser/items/mod.rs b/crates/parser/src/parser/items/mod.rs index 57e8bee..2b27e99 100644 --- a/crates/parser/src/parser/items/mod.rs +++ b/crates/parser/src/parser/items/mod.rs @@ -129,6 +129,8 @@ impl<'a> TryFrom> for TopLevelKind { ty: inner.next().unwrap().try_into()?, }), + Rule::include_global => Ok(TopLevelKind::IncludeGlobal(Path(collect_recovered(&mut inner)?))), + _ => AstError::bug_unimplemented(pair), } } From c564fc3e904770f56c21c14867d6b9d4b78aa214 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Wed, 8 Jul 2026 10:44:09 +0200 Subject: [PATCH 2/4] Added use include --- crates/codegen/src/lib.rs | 20 +++++++++++++++++--- crates/codegen/src/top_level.rs | 8 +++++++- crates/parser/src/ast/top_level.rs | 1 + crates/parser/src/grammar.pest | 6 +++++- crates/parser/src/parser/items/mod.rs | 9 ++++++++- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 75ae198..a678124 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -26,10 +26,15 @@ pub trait GetRust { } } +pub enum Include { + Glob(Path), + Use(Visibility, Path), +} + pub struct RustCodegen { output: String, indent: usize, - pub crates: HashMap>, + pub crates: HashMap>, pub mapping: Mapping, position: RustMap, } @@ -101,8 +106,17 @@ impl RustCodegen { for item in items { self.add_indented("pub use "); - item.gen_rust(&mut ctx, self); - self.addln("::*;"); + match item { + Include::Glob(item) => { + item.gen_rust(&mut ctx, self); + self.add("::*"); + } + Include::Use(vis, item) => { + vis.gen_rust(&mut ctx, self); + item.gen_rust(&mut ctx, self); + } + } + self.addln(";"); } self.indent -= 1; diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index b65190c..9927a11 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -330,7 +330,13 @@ impl GenRust for TopLevelKind { cg.crates .entry(incl.0[0].clone()) .or_insert(Vec::new()) - .push(incl.clone()); + .push(crate::Include::Glob(incl.clone())); + } + Self::IncludeUse(vis, incl) => { + cg.crates + .entry(incl.0[0].clone()) + .or_insert(Vec::new()) + .push(crate::Include::Use(vis.clone(), incl.clone())); } } } diff --git a/crates/parser/src/ast/top_level.rs b/crates/parser/src/ast/top_level.rs index a759d33..707e8c9 100644 --- a/crates/parser/src/ast/top_level.rs +++ b/crates/parser/src/ast/top_level.rs @@ -29,6 +29,7 @@ pub enum TopLevelKind { ModAttribute, Import(Visibility, Path), IncludeGlobal(Path), + IncludeUse(Visibility, Path), DeclareModule(Visibility, Identifier), ImplDecl(ImplDecl), StaticDecl(VarDeclStmt), diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 6eb3973..8e7eeaf 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -262,6 +262,10 @@ include_global = { "#" ~ "include" ~ "<" ~ identifier ~ ("/" ~ identifier)* ~ ">" } +include_use_global = { + visibility? ~ "#" ~ "use" ~ static_path ~ semicolon +} + import = { visibility? ~ "use" ~ static_path ~ semicolon } @@ -388,7 +392,7 @@ type_alias = { "type" ~ identifier ~ generics_decl? ~ "=" ~ type_expr ~ ";" } top_level = { mod_attribute - | (attributes ~ (include_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias)) + | (attributes ~ (include_global | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias)) } // ====================================================== diff --git a/crates/parser/src/parser/items/mod.rs b/crates/parser/src/parser/items/mod.rs index 2b27e99..b0c1b38 100644 --- a/crates/parser/src/parser/items/mod.rs +++ b/crates/parser/src/parser/items/mod.rs @@ -129,7 +129,14 @@ impl<'a> TryFrom> for TopLevelKind { ty: inner.next().unwrap().try_into()?, }), - Rule::include_global => Ok(TopLevelKind::IncludeGlobal(Path(collect_recovered(&mut inner)?))), + Rule::include_global => Ok(TopLevelKind::IncludeGlobal(Path(collect_recovered( + &mut inner, + )?))), + + Rule::include_use_global => Ok(TopLevelKind::IncludeUse( + Visibility::try_from(&mut inner)?, + Path::try_from(inner.next().unwrap())?, + )), _ => AstError::bug_unimplemented(pair), } From 44ac3c365d8b33b095f9f2ede82f4f7734c896f0 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Wed, 8 Jul 2026 10:52:11 +0200 Subject: [PATCH 3/4] Inline doc gen --- crates/codegen/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index a678124..7ca04a5 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -105,6 +105,7 @@ impl RustCodegen { self.addln(";"); for item in items { + self.add_indentedln("#[doc(inline)]"); self.add_indented("pub use "); match item { Include::Glob(item) => { From 8198b8d5cb88ad0273346fa041b2937da2ec5105 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Wed, 8 Jul 2026 11:23:36 +0200 Subject: [PATCH 4/4] Local include --- crates/codegen/src/top_level.rs | 5 +++++ crates/parser/src/ast/top_level.rs | 1 + crates/parser/src/grammar.pest | 6 +++++- crates/parser/src/parser/items/mod.rs | 10 ++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index 9927a11..bbe1490 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -338,6 +338,11 @@ impl GenRust for TopLevelKind { .or_insert(Vec::new()) .push(crate::Include::Use(vis.clone(), incl.clone())); } + Self::IncludeLocal(path) => { + cg.add("include!(\""); + cg.add(&path.replace(".mist", ".rs")); + cg.add("\");"); + } } } } diff --git a/crates/parser/src/ast/top_level.rs b/crates/parser/src/ast/top_level.rs index 707e8c9..2e95aed 100644 --- a/crates/parser/src/ast/top_level.rs +++ b/crates/parser/src/ast/top_level.rs @@ -30,6 +30,7 @@ pub enum TopLevelKind { Import(Visibility, Path), IncludeGlobal(Path), IncludeUse(Visibility, Path), + IncludeLocal(String), DeclareModule(Visibility, Identifier), ImplDecl(ImplDecl), StaticDecl(VarDeclStmt), diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 8e7eeaf..24b188b 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -266,6 +266,10 @@ include_use_global = { visibility? ~ "#" ~ "use" ~ static_path ~ semicolon } +include_local = { + "#" ~ "include" ~ string_lit +} + import = { visibility? ~ "use" ~ static_path ~ semicolon } @@ -392,7 +396,7 @@ type_alias = { "type" ~ identifier ~ generics_decl? ~ "=" ~ type_expr ~ ";" } top_level = { mod_attribute - | (attributes ~ (include_global | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias)) + | (attributes ~ (include_global | include_local | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias)) } // ====================================================== diff --git a/crates/parser/src/parser/items/mod.rs b/crates/parser/src/parser/items/mod.rs index b0c1b38..4022024 100644 --- a/crates/parser/src/parser/items/mod.rs +++ b/crates/parser/src/parser/items/mod.rs @@ -138,6 +138,16 @@ impl<'a> TryFrom> for TopLevelKind { Path::try_from(inner.next().unwrap())?, )), + Rule::include_local => Ok(TopLevelKind::IncludeLocal( + inner + .next() + .unwrap() + .into_inner() + .next() + .unwrap() + .to_string(), + )), + _ => AstError::bug_unimplemented(pair), } }