More generics

This commit is contained in:
2026-05-08 17:09:01 +02:00
parent ed4a505a6a
commit bbcc639331
4 changed files with 27 additions and 11 deletions
+3
View File
@@ -77,6 +77,7 @@ pub enum TopLevelKind {
EnumDecl {
visibility: Visibility,
name: Identifier,
generics: Generics,
fields: Vec<EnumItem>,
},
StructDecl {
@@ -89,6 +90,7 @@ pub enum TopLevelKind {
ClassDecl {
visibility: Visibility,
name: Identifier,
generics: Generics,
fields: Vec<VarDeclStmt>,
constructor: ClassConstructor,
methods: Vec<FunctionDecl>,
@@ -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,
+3 -3
View File
@@ -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* ~ "}"
}
// ======================================================
+11 -3
View File
@@ -267,6 +267,9 @@ impl From<pest::iterators::Pair<'_, Rule>> 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<pest::iterators::Pair<'_, Rule>> 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<pest::iterators::Pair<'_, Rule>> for VarDecl {
impl From<pest::iterators::Pair<'_, Rule>> 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<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
Self {
visibility,
name,
generics,
params,
return_type,
body,
+10 -5
View File
@@ -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()
));