From bbcc6393318771c25df5f960afb2f2a99da96b69 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 8 May 2026 17:09:01 +0200 Subject: [PATCH] More generics --- parser/src/ast.rs | 3 +++ parser/src/grammar.pest | 6 +++--- parser/src/lib.rs | 14 +++++++++++--- src/codegen.rs | 15 ++++++++++----- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index b82abc8..16a0968 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -77,6 +77,7 @@ pub enum TopLevelKind { EnumDecl { visibility: Visibility, name: Identifier, + generics: Generics, fields: Vec, }, StructDecl { @@ -89,6 +90,7 @@ pub enum TopLevelKind { ClassDecl { visibility: Visibility, name: Identifier, + generics: Generics, fields: Vec, constructor: ClassConstructor, methods: Vec, @@ -126,6 +128,7 @@ pub struct ClassConstructor { pub struct FunctionDecl { pub visibility: Visibility, pub name: Identifier, + pub generics: Generics, pub params: ParamList, pub return_type: TypeExpr, pub body: Block, diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 5d8b892..8ff673b 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -204,7 +204,7 @@ enum_fields = _{ } enum_decl = { - export? ~ "enum" ~ identifier ~ "{" ~ enum_fields ~ "}" + export? ~ "enum" ~ identifier ~ generics? ~ "{" ~ enum_fields ~ "}" } // ====================================================== @@ -224,11 +224,11 @@ self_param = { } method = { - export? ~ type_expr ~ identifier ~ "(" ~ self_param? ~ param_list? ~ ")" ~ block + export? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ param_list? ~ ")" ~ block } class_decl = { - export? ~ "class" ~ identifier ~ "{" ~ class_fields ~ class_constructor ~ method* ~ "}" + export? ~ "class" ~ identifier ~ generics? ~ "{" ~ class_fields ~ class_constructor ~ method* ~ "}" } // ====================================================== diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 062f0af..13113b1 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -267,6 +267,9 @@ impl From> for TopLevelKind { Rule::class_decl => TopLevelKind::ClassDecl { visibility: Visibility::from(&mut inner), name: Identifier::from(inner.next().unwrap()), + generics: consume_rule(&mut inner, Rule::generics) + .map(Generics::from) + .unwrap_or_default(), fields: inner .next() .unwrap() @@ -280,6 +283,9 @@ impl From> for TopLevelKind { Rule::enum_decl => TopLevelKind::EnumDecl { visibility: Visibility::from(&mut inner), name: Identifier::from(inner.next().unwrap()), + generics: consume_rule(&mut inner, Rule::generics) + .map(Generics::from) + .unwrap_or_default(), fields: inner.map(EnumItem::from).collect(), }, @@ -612,12 +618,13 @@ impl From> for VarDecl { impl From> for FunctionDecl { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { let mut inner = pair.into_inner(); - let visibility = Visibility::from(&mut inner); - let return_type = TypeExpr::from(inner.next().unwrap()); - let name = Identifier::from(inner.next().unwrap()); + let generics = consume_rule(&mut inner, Rule::generics) + .map(Generics::from) + .unwrap_or_default(); + let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| { let mut param_inner = param.into_inner(); let name = Pattern::Id(Identifier(String::from("self"))); @@ -662,6 +669,7 @@ impl From> for FunctionDecl { Self { visibility, name, + generics, params, return_type, body, diff --git a/src/codegen.rs b/src/codegen.rs index c4ebdc3..b8caf83 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -338,12 +338,14 @@ impl ToRust for TopLevelKind { Self::EnumDecl { visibility, name, + generics, fields, } => { cg.addln(&format!( - "{}enum {} {{", + "{}enum {}{} {{", visibility.get_rust(), - name.get_rust() + name.get_rust(), + generics.get_rust() )); cg.indent += 1; @@ -357,15 +359,17 @@ impl ToRust for TopLevelKind { Self::ClassDecl { visibility, name, + generics, fields, constructor, methods, } => { // Struct decl cg.addln(&format!( - "{}struct {} {{", + "{}struct {}{} {{", visibility.get_rust(), - name.get_rust() + name.get_rust(), + generics.get_rust() )); cg.indent += 1; @@ -581,9 +585,10 @@ impl ToRust for FunctionDecl { .join(", "); cg.add_indentedln(&format!( - "{}fn {}({}) -> {} {{", + "{}fn {}{}({}) -> {} {{", self.visibility.get_rust(), self.name.get_rust(), + self.generics.get_rust(), params_str, self.return_type.get_rust() ));