diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 88aa6d8..ca4ca2a 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -68,6 +68,7 @@ pub struct TopLevel(pub TopLevelKind, pub Vec); pub enum TopLevelKind { ModAttribute, Import(Path), + Mod(String), StructDecl { visibility: Visibility, name: String, diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 311b5bc..8d0d291 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -37,7 +37,7 @@ class_decl = { // ================= TOP LEVEL ================= -top_level = { (attributes ~ (import | function_decl | struct_decl | class_decl)) | mod_attribute } +top_level = { (attributes ~ (import | function_decl | struct_decl | class_decl | mod_package)) | mod_attribute } attribute = { "#" ~ "[" ~ meta ~ "]" @@ -59,6 +59,8 @@ attributes = { attribute* } import = { "use" ~ "<" ~ static_path ~ ">" ~ semicolon } +mod_package = { "mod" ~ identifier ~ semicolon } + field_list = { field ~ ("," ~ field)* ~ ","? } field = { export? ~ type_expr ~ identifier } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index b6dc717..50f90e7 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -259,6 +259,8 @@ impl From> for TopLevelKind { methods: inner.into_iter().map(FunctionDecl::from).collect(), }, + Rule::mod_package => TopLevelKind::Mod(inner.next().unwrap().as_str().to_string()), + _ => unimplemented!("{rule:#?}"), } } diff --git a/src/codegen.rs b/src/codegen.rs index 1f4eab4..66f8ad0 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -308,9 +308,8 @@ impl ToRust for TopLevelKind { fn to_rust(&self, cg: &mut RustCodegen) { match self { Self::ModAttribute => {} - Self::Import(path) => { - cg.addln(&format!("use {};", path.get_rust())); - } + Self::Import(path) => cg.addln(&format!("use {};", path.get_rust())), + Self::Mod(id) => cg.addln(&format!("mod {};", id)), Self::FunctionDecl(decl) => decl.to_rust(cg), Self::StructDecl { visibility, diff --git a/test/src/main.mist b/test/src/main.mist index 811a162..30ef530 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -1,14 +1,5 @@ -class MyClass { - String message; - - public constructor(str* name) { - self.message = format!("Hello {}!", name); - } - - public void greet(self*) { - println!("{}", self.message); - } -} +mod my_class; +use ; void main() { var my_class = new MyClass("World");