This commit is contained in:
2026-05-07 18:15:48 +02:00
parent 2ccec652da
commit 27c6eb74c3
5 changed files with 10 additions and 15 deletions
+1
View File
@@ -68,6 +68,7 @@ pub struct TopLevel(pub TopLevelKind, pub Vec<Attribute>);
pub enum TopLevelKind { pub enum TopLevelKind {
ModAttribute, ModAttribute,
Import(Path), Import(Path),
Mod(String),
StructDecl { StructDecl {
visibility: Visibility, visibility: Visibility,
name: String, name: String,
+3 -1
View File
@@ -37,7 +37,7 @@ class_decl = {
// ================= TOP LEVEL ================= // ================= 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 = { attribute = {
"#" ~ "[" ~ meta ~ "]" "#" ~ "[" ~ meta ~ "]"
@@ -59,6 +59,8 @@ attributes = { attribute* }
import = { "use" ~ "<" ~ static_path ~ ">" ~ semicolon } import = { "use" ~ "<" ~ static_path ~ ">" ~ semicolon }
mod_package = { "mod" ~ identifier ~ semicolon }
field_list = { field ~ ("," ~ field)* ~ ","? } field_list = { field ~ ("," ~ field)* ~ ","? }
field = { export? ~ type_expr ~ identifier } field = { export? ~ type_expr ~ identifier }
+2
View File
@@ -259,6 +259,8 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
methods: inner.into_iter().map(FunctionDecl::from).collect(), methods: inner.into_iter().map(FunctionDecl::from).collect(),
}, },
Rule::mod_package => TopLevelKind::Mod(inner.next().unwrap().as_str().to_string()),
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
} }
} }
+2 -3
View File
@@ -308,9 +308,8 @@ impl ToRust for TopLevelKind {
fn to_rust(&self, cg: &mut RustCodegen) { fn to_rust(&self, cg: &mut RustCodegen) {
match self { match self {
Self::ModAttribute => {} Self::ModAttribute => {}
Self::Import(path) => { Self::Import(path) => cg.addln(&format!("use {};", path.get_rust())),
cg.addln(&format!("use {};", path.get_rust())); Self::Mod(id) => cg.addln(&format!("mod {};", id)),
}
Self::FunctionDecl(decl) => decl.to_rust(cg), Self::FunctionDecl(decl) => decl.to_rust(cg),
Self::StructDecl { Self::StructDecl {
visibility, visibility,
+2 -11
View File
@@ -1,14 +1,5 @@
class MyClass { mod my_class;
String message; use <my_class::MyClass>;
public constructor(str* name) {
self.message = format!("Hello {}!", name);
}
public void greet(self*) {
println!("{}", self.message);
}
}
void main() { void main() {
var my_class = new MyClass("World"); var my_class = new MyClass("World");