More generics
This commit is contained in:
@@ -77,6 +77,7 @@ pub enum TopLevelKind {
|
|||||||
EnumDecl {
|
EnumDecl {
|
||||||
visibility: Visibility,
|
visibility: Visibility,
|
||||||
name: Identifier,
|
name: Identifier,
|
||||||
|
generics: Generics,
|
||||||
fields: Vec<EnumItem>,
|
fields: Vec<EnumItem>,
|
||||||
},
|
},
|
||||||
StructDecl {
|
StructDecl {
|
||||||
@@ -89,6 +90,7 @@ pub enum TopLevelKind {
|
|||||||
ClassDecl {
|
ClassDecl {
|
||||||
visibility: Visibility,
|
visibility: Visibility,
|
||||||
name: Identifier,
|
name: Identifier,
|
||||||
|
generics: Generics,
|
||||||
fields: Vec<VarDeclStmt>,
|
fields: Vec<VarDeclStmt>,
|
||||||
constructor: ClassConstructor,
|
constructor: ClassConstructor,
|
||||||
methods: Vec<FunctionDecl>,
|
methods: Vec<FunctionDecl>,
|
||||||
@@ -126,6 +128,7 @@ pub struct ClassConstructor {
|
|||||||
pub struct FunctionDecl {
|
pub struct FunctionDecl {
|
||||||
pub visibility: Visibility,
|
pub visibility: Visibility,
|
||||||
pub name: Identifier,
|
pub name: Identifier,
|
||||||
|
pub generics: Generics,
|
||||||
pub params: ParamList,
|
pub params: ParamList,
|
||||||
pub return_type: TypeExpr,
|
pub return_type: TypeExpr,
|
||||||
pub body: Block,
|
pub body: Block,
|
||||||
|
|||||||
@@ -204,7 +204,7 @@ enum_fields = _{
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum_decl = {
|
enum_decl = {
|
||||||
export? ~ "enum" ~ identifier ~ "{" ~ enum_fields ~ "}"
|
export? ~ "enum" ~ identifier ~ generics? ~ "{" ~ enum_fields ~ "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
@@ -224,11 +224,11 @@ self_param = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
method = {
|
method = {
|
||||||
export? ~ type_expr ~ identifier ~ "(" ~ self_param? ~ param_list? ~ ")" ~ block
|
export? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ param_list? ~ ")" ~ block
|
||||||
}
|
}
|
||||||
|
|
||||||
class_decl = {
|
class_decl = {
|
||||||
export? ~ "class" ~ identifier ~ "{" ~ class_fields ~ class_constructor ~ method* ~ "}"
|
export? ~ "class" ~ identifier ~ generics? ~ "{" ~ class_fields ~ class_constructor ~ method* ~ "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
|||||||
+11
-3
@@ -267,6 +267,9 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
|||||||
Rule::class_decl => TopLevelKind::ClassDecl {
|
Rule::class_decl => TopLevelKind::ClassDecl {
|
||||||
visibility: Visibility::from(&mut inner),
|
visibility: Visibility::from(&mut inner),
|
||||||
name: Identifier::from(inner.next().unwrap()),
|
name: Identifier::from(inner.next().unwrap()),
|
||||||
|
generics: consume_rule(&mut inner, Rule::generics)
|
||||||
|
.map(Generics::from)
|
||||||
|
.unwrap_or_default(),
|
||||||
fields: inner
|
fields: inner
|
||||||
.next()
|
.next()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -280,6 +283,9 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
|||||||
Rule::enum_decl => TopLevelKind::EnumDecl {
|
Rule::enum_decl => TopLevelKind::EnumDecl {
|
||||||
visibility: Visibility::from(&mut inner),
|
visibility: Visibility::from(&mut inner),
|
||||||
name: Identifier::from(inner.next().unwrap()),
|
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(),
|
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 {
|
impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
|
||||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||||
let mut inner = pair.into_inner();
|
let mut inner = pair.into_inner();
|
||||||
|
|
||||||
let visibility = Visibility::from(&mut inner);
|
let visibility = Visibility::from(&mut inner);
|
||||||
|
|
||||||
let return_type = TypeExpr::from(inner.next().unwrap());
|
let return_type = TypeExpr::from(inner.next().unwrap());
|
||||||
|
|
||||||
let name = Identifier::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 self_param = consume_rule(&mut inner, Rule::self_param).map(|param| {
|
||||||
let mut param_inner = param.into_inner();
|
let mut param_inner = param.into_inner();
|
||||||
let name = Pattern::Id(Identifier(String::from("self")));
|
let name = Pattern::Id(Identifier(String::from("self")));
|
||||||
@@ -662,6 +669,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
|
|||||||
Self {
|
Self {
|
||||||
visibility,
|
visibility,
|
||||||
name,
|
name,
|
||||||
|
generics,
|
||||||
params,
|
params,
|
||||||
return_type,
|
return_type,
|
||||||
body,
|
body,
|
||||||
|
|||||||
+10
-5
@@ -338,12 +338,14 @@ impl ToRust for TopLevelKind {
|
|||||||
Self::EnumDecl {
|
Self::EnumDecl {
|
||||||
visibility,
|
visibility,
|
||||||
name,
|
name,
|
||||||
|
generics,
|
||||||
fields,
|
fields,
|
||||||
} => {
|
} => {
|
||||||
cg.addln(&format!(
|
cg.addln(&format!(
|
||||||
"{}enum {} {{",
|
"{}enum {}{} {{",
|
||||||
visibility.get_rust(),
|
visibility.get_rust(),
|
||||||
name.get_rust()
|
name.get_rust(),
|
||||||
|
generics.get_rust()
|
||||||
));
|
));
|
||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|
||||||
@@ -357,15 +359,17 @@ impl ToRust for TopLevelKind {
|
|||||||
Self::ClassDecl {
|
Self::ClassDecl {
|
||||||
visibility,
|
visibility,
|
||||||
name,
|
name,
|
||||||
|
generics,
|
||||||
fields,
|
fields,
|
||||||
constructor,
|
constructor,
|
||||||
methods,
|
methods,
|
||||||
} => {
|
} => {
|
||||||
// Struct decl
|
// Struct decl
|
||||||
cg.addln(&format!(
|
cg.addln(&format!(
|
||||||
"{}struct {} {{",
|
"{}struct {}{} {{",
|
||||||
visibility.get_rust(),
|
visibility.get_rust(),
|
||||||
name.get_rust()
|
name.get_rust(),
|
||||||
|
generics.get_rust()
|
||||||
));
|
));
|
||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|
||||||
@@ -581,9 +585,10 @@ impl ToRust for FunctionDecl {
|
|||||||
.join(", ");
|
.join(", ");
|
||||||
|
|
||||||
cg.add_indentedln(&format!(
|
cg.add_indentedln(&format!(
|
||||||
"{}fn {}({}) -> {} {{",
|
"{}fn {}{}({}) -> {} {{",
|
||||||
self.visibility.get_rust(),
|
self.visibility.get_rust(),
|
||||||
self.name.get_rust(),
|
self.name.get_rust(),
|
||||||
|
self.generics.get_rust(),
|
||||||
params_str,
|
params_str,
|
||||||
self.return_type.get_rust()
|
self.return_type.get_rust()
|
||||||
));
|
));
|
||||||
|
|||||||
Reference in New Issue
Block a user