Added other struct types #117
This commit is contained in:
@@ -266,6 +266,33 @@ impl GenRust for TopLevelKind {
|
|||||||
cg.indent -= 1;
|
cg.indent -= 1;
|
||||||
cg.addln("}\n");
|
cg.addln("}\n");
|
||||||
}
|
}
|
||||||
|
Self::StructDeclTupleUnit {
|
||||||
|
visibility,
|
||||||
|
name,
|
||||||
|
generics,
|
||||||
|
unit,
|
||||||
|
} => {
|
||||||
|
cg.addln(&format!(
|
||||||
|
"{}struct {}{}",
|
||||||
|
visibility.get_rust(),
|
||||||
|
name.get_rust(),
|
||||||
|
generics.get_rust()
|
||||||
|
));
|
||||||
|
cg.indent += 1;
|
||||||
|
|
||||||
|
if let Some(unit) = unit {
|
||||||
|
cg.add(" (");
|
||||||
|
for unit in unit {
|
||||||
|
cg.add_indented("");
|
||||||
|
unit.gen_rust(ctx, cg);
|
||||||
|
cg.add(", ");
|
||||||
|
}
|
||||||
|
cg.add(")");
|
||||||
|
}
|
||||||
|
|
||||||
|
cg.indent -= 1;
|
||||||
|
cg.addln(";\n");
|
||||||
|
}
|
||||||
Self::EnumDecl {
|
Self::EnumDecl {
|
||||||
visibility,
|
visibility,
|
||||||
name,
|
name,
|
||||||
|
|||||||
@@ -59,6 +59,12 @@ pub enum TopLevelKind {
|
|||||||
generics: GenericsDecl,
|
generics: GenericsDecl,
|
||||||
fields: Vec<Spanned<FieldDecl>>,
|
fields: Vec<Spanned<FieldDecl>>,
|
||||||
},
|
},
|
||||||
|
StructDeclTupleUnit {
|
||||||
|
visibility: Visibility,
|
||||||
|
name: Identifier,
|
||||||
|
generics: GenericsDecl,
|
||||||
|
unit: Option<Vec<Spanned<TypeExpr>>>,
|
||||||
|
},
|
||||||
FunctionDecl(FunctionDecl),
|
FunctionDecl(FunctionDecl),
|
||||||
ClassDecl {
|
ClassDecl {
|
||||||
visibility: Visibility,
|
visibility: Visibility,
|
||||||
|
|||||||
@@ -288,11 +288,7 @@ struct_decl = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tuple_struct_decl = {
|
tuple_struct_decl = {
|
||||||
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ tuple_type ~ ";"
|
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ tuple_type? ~ ";"
|
||||||
}
|
|
||||||
|
|
||||||
empty_struct_decl = {
|
|
||||||
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ ";"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
@@ -407,7 +403,7 @@ type_alias = { "type" ~ identifier ~ generics_decl? ~ "=" ~ type_expr ~ ";" }
|
|||||||
|
|
||||||
top_level = {
|
top_level = {
|
||||||
mod_attribute
|
mod_attribute
|
||||||
| (attributes ~ (include_global | include_local | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | tuple_struct_decl | empty_struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias))
|
| (attributes ~ (include_global | include_local | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | tuple_struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
|||||||
@@ -57,6 +57,21 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
|||||||
fields: collect_recovered(inner.next().unwrap().into_inner())?,
|
fields: collect_recovered(inner.next().unwrap().into_inner())?,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
Rule::tuple_struct_decl => Ok(TopLevelKind::StructDeclTupleUnit {
|
||||||
|
visibility: Visibility::try_from(&mut inner)?,
|
||||||
|
|
||||||
|
name: inner.next().unwrap().try_into()?,
|
||||||
|
|
||||||
|
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||||
|
.map(GenericsDecl::try_from)
|
||||||
|
.transpose()
|
||||||
|
.map(|v| v.unwrap_or_default())?,
|
||||||
|
|
||||||
|
unit: consume_rule(&mut inner, Rule::tuple_type)
|
||||||
|
.map(|v| collect_recovered(v.into_inner()))
|
||||||
|
.transpose()?,
|
||||||
|
}),
|
||||||
|
|
||||||
Rule::class_decl => Ok(TopLevelKind::ClassDecl {
|
Rule::class_decl => Ok(TopLevelKind::ClassDecl {
|
||||||
visibility: Visibility::try_from(&mut inner)?,
|
visibility: Visibility::try_from(&mut inner)?,
|
||||||
|
|
||||||
@@ -147,7 +162,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.to_string(),
|
.to_string(),
|
||||||
)),
|
)),
|
||||||
|
|
||||||
_ => AstError::bug_unimplemented(pair),
|
_ => AstError::bug_unimplemented(pair),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,6 +100,9 @@ pub void test_struct_destructure_rename()
|
|||||||
assert_eq!(b, 15);
|
assert_eq!(b, 15);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct TuplePoint(i32, i32);
|
||||||
|
struct Unit;
|
||||||
|
|
||||||
pub void run_tests()
|
pub void run_tests()
|
||||||
{
|
{
|
||||||
test_struct_create_access();
|
test_struct_create_access();
|
||||||
|
|||||||
Reference in New Issue
Block a user