Added global include

This commit is contained in:
2026-07-08 10:11:26 +02:00
parent 4b69bbaed0
commit bb006c1ba2
5 changed files with 40 additions and 2 deletions
+26 -1
View File
@@ -3,7 +3,7 @@ pub mod expr;
pub mod statement; pub mod statement;
pub mod top_level; pub mod top_level;
use std::path::PathBuf; use std::{collections::HashMap, path::PathBuf};
use mist_parser::{ use mist_parser::{
ast::*, ast::*,
@@ -29,6 +29,7 @@ pub trait GetRust {
pub struct RustCodegen { pub struct RustCodegen {
output: String, output: String,
indent: usize, indent: usize,
pub crates: HashMap<Identifier, Vec<Path>>,
pub mapping: Mapping, pub mapping: Mapping,
position: RustMap, position: RustMap,
} }
@@ -38,6 +39,7 @@ impl RustCodegen {
Self { Self {
output: String::new(), output: String::new(),
indent: 0, indent: 0,
crates: HashMap::new(),
mapping: Mapping::new(mist_path), mapping: Mapping::new(mist_path),
position: RustMap(1, 0), position: RustMap(1, 0),
} }
@@ -84,6 +86,29 @@ impl RustCodegen {
tl.gen_rust(&mut ctx, self); 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() self.output.clone()
} }
+6
View File
@@ -326,6 +326,12 @@ impl GenRust for TopLevelKind {
constructor, constructor,
items, items,
), ),
Self::IncludeGlobal(incl) => {
cg.crates
.entry(incl.0[0].clone())
.or_insert(Vec::new())
.push(incl.clone());
}
} }
} }
} }
+1
View File
@@ -28,6 +28,7 @@ pub struct TopLevel(pub Spanned<TopLevelKind>, pub Vec<Attribute>);
pub enum TopLevelKind { pub enum TopLevelKind {
ModAttribute, ModAttribute,
Import(Visibility, Path), Import(Visibility, Path),
IncludeGlobal(Path),
DeclareModule(Visibility, Identifier), DeclareModule(Visibility, Identifier),
ImplDecl(ImplDecl), ImplDecl(ImplDecl),
StaticDecl(VarDeclStmt), StaticDecl(VarDeclStmt),
+5 -1
View File
@@ -258,6 +258,10 @@ param_list = {
// IMPORTS / MODULES // IMPORTS / MODULES
// ====================================================== // ======================================================
include_global = {
"#" ~ "include" ~ "<" ~ identifier ~ ("/" ~ identifier)* ~ ">"
}
import = { import = {
visibility? ~ "use" ~ static_path ~ semicolon visibility? ~ "use" ~ static_path ~ semicolon
} }
@@ -384,7 +388,7 @@ type_alias = { "type" ~ identifier ~ generics_decl? ~ "=" ~ type_expr ~ ";" }
top_level = { top_level = {
mod_attribute 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))
} }
// ====================================================== // ======================================================
+2
View File
@@ -129,6 +129,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
ty: inner.next().unwrap().try_into()?, ty: inner.next().unwrap().try_into()?,
}), }),
Rule::include_global => Ok(TopLevelKind::IncludeGlobal(Path(collect_recovered(&mut inner)?))),
_ => AstError::bug_unimplemented(pair), _ => AstError::bug_unimplemented(pair),
} }
} }