Class method AST and grammar

This commit is contained in:
2026-05-05 20:30:15 +02:00
parent 9e5d030b0c
commit 4837d39ab7
3 changed files with 22 additions and 3 deletions
+9
View File
@@ -89,6 +89,15 @@ pub struct ClassConstructor {
pub body: Block,
}
#[derive(Debug, Clone, Serialize)]
pub struct ClassMethod {
export: bool,
name: String,
params: ParamList,
return_type: TypeExpr,
body: Block,
}
#[derive(Debug, Clone, Serialize)]
pub enum Postfix {
FieldAccess(String),
+8 -2
View File
@@ -26,7 +26,13 @@ class_constructor = {
class_fields = { var_decl_statement* }
class_methods = { function_decl* }
class_methods = { method* }
self_param = { "self" ~ mutable? ~ deref_px? }
method = {
export? ~ type_expr ~ identifier ~ "(" ~ self_param? ~ param_list? ~ ")" ~ block
}
class_decl = {
"class" ~ identifier ~ "{" ~ class_fields ~ class_constructor ~ class_methods ~ "}"
@@ -167,7 +173,7 @@ prefix = {
deref_px
| mut_ref_px
| ref_px
| new_px
| new_px
}
deref_px = { "*" }
+5 -1
View File
@@ -4,9 +4,13 @@ class MyClass {
public constructor(str* name) {
self.message = format!("Hello {}!", name);
}
public void greet(self*) {
println!("{}", self.message);
}
}
void main() {
var my_class = new MyClass("World");
println!("{}", my_class.message);
my_class.greet();
}