From c717a57f99f947a0cb6aa803425a8d96e6762566 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Mon, 4 May 2026 19:29:10 +0200 Subject: [PATCH] Importing --- parser/src/ast.rs | 2 +- parser/src/grammar.pest | 9 +++++---- parser/src/lib.rs | 8 ++++---- src/codegen.rs | 5 ++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 8ba217a..d33557a 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -47,7 +47,7 @@ pub enum BinaryOp { #[derive(Debug, Clone, Serialize)] pub enum TopLevel { - Import(String), + Include(StaticPath), StructDecl { export: bool, name: String, diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index e60b11b..fc0110c 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -12,15 +12,16 @@ string_lit = { "\"" ~ inner_str ~ "\"" } inner_str = @{ (!"\"" ~ ANY)* } static_path = { identifier ~ ("::" ~ identifier)* } -export = { "public" } -mutable = { "mut" } -var_decl = { type_expr ~ mutable? ~ identifier } +export = { "public" } +mutable = { "mut" } +sharp = _{ "#" } +var_decl = { type_expr ~ mutable? ~ identifier } // ================= TOP LEVEL ================= top_level = _{ import | function_decl | struct_decl } -import = { "import" ~ string_lit ~ ";" } +import = { sharp ~ "include" ~ "<" ~ static_path ~ ">" } field_list = { field ~ ("," ~ field)* ~ ","? } field = { export? ~ type_expr ~ identifier } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 584c832..e8d2340 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -123,10 +123,10 @@ impl TryFrom> for TopLevel { type Error = (); fn try_from(pair: pest::iterators::Pair) -> Result { match pair.as_rule() { - Rule::import => { - let path = pair.into_inner().next().unwrap().as_str().to_string(); - Ok(TopLevel::Import(path)) - } + Rule::import => Ok(TopLevel::Include(StaticPath::from( + pair.into_inner().next().unwrap(), + ))), + Rule::function_decl => { let mut inner = pair.into_inner(); diff --git a/src/codegen.rs b/src/codegen.rs index 0830a5f..34b9571 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -195,9 +195,8 @@ impl ToRust for Block { impl ToRust for TopLevel { fn to_rust(&self, cg: &mut RustCodegen) { match self { - TopLevel::Import(path) => { - let path = path.replace('"', ""); - cg.addln(&format!("use {};", path)); + TopLevel::Include(path) => { + cg.addln(&format!("use {};", path.get_rust())); } TopLevel::StructDecl {