Class fields
This commit is contained in:
+14
-1
@@ -94,7 +94,7 @@ pub enum TopLevelKind {
|
||||
visibility: Visibility,
|
||||
name: Identifier,
|
||||
generics: Generics,
|
||||
fields: Vec<VarDeclStmt>,
|
||||
fields: Vec<FieldDeclStmt>,
|
||||
constructor: ClassConstructor,
|
||||
methods: Vec<FunctionDecl>,
|
||||
},
|
||||
@@ -208,6 +208,19 @@ pub struct VarDeclStmt {
|
||||
pub init: Option<Expression>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct FieldDecl {
|
||||
pub visibility: Visibility,
|
||||
pub type_: TypeExpr,
|
||||
pub name: Identifier,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct FieldDeclStmt {
|
||||
pub decl: FieldDecl,
|
||||
pub init: Option<Expression>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct VarAssignStmt {
|
||||
pub target: Expression,
|
||||
|
||||
@@ -149,7 +149,6 @@ attributes = {
|
||||
// ======================================================
|
||||
// DECLARATION HELPERS
|
||||
// ======================================================
|
||||
|
||||
var_decl = {
|
||||
(var | type_expr) ~ mutable? ~ pattern
|
||||
}
|
||||
@@ -225,7 +224,11 @@ class_constructor = {
|
||||
}
|
||||
|
||||
class_fields = {
|
||||
var_decl_statement*
|
||||
class_field*
|
||||
}
|
||||
|
||||
class_field = {
|
||||
field ~ ("=" ~ expr)? ~ semicolon
|
||||
}
|
||||
|
||||
self_param = {
|
||||
|
||||
+41
-1
@@ -300,7 +300,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
.next()
|
||||
.unwrap()
|
||||
.into_inner()
|
||||
.map(VarDeclStmt::from)
|
||||
.map(FieldDeclStmt::from)
|
||||
.collect(),
|
||||
constructor: ClassConstructor::from(inner.next().unwrap()),
|
||||
methods: inner.into_iter().map(FunctionDecl::from).collect(),
|
||||
@@ -583,6 +583,24 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDeclStmt {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for FieldDeclStmt {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
Rule::class_field => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let decl = FieldDecl::from(inner.next().unwrap());
|
||||
|
||||
let init = inner.next().map(Expression::from);
|
||||
|
||||
FieldDeclStmt { decl, init }
|
||||
}
|
||||
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for Pattern {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
let rule = pair.as_rule();
|
||||
@@ -641,6 +659,28 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for FieldDecl {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
Rule::field => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let visibility = Visibility::from(&mut inner);
|
||||
let type_ = TypeExpr::from(inner.next().unwrap());
|
||||
let name = Identifier::from(inner.next().unwrap());
|
||||
|
||||
FieldDecl {
|
||||
visibility,
|
||||
type_,
|
||||
name,
|
||||
}
|
||||
}
|
||||
|
||||
_ => unimplemented!("{:?}", pair.as_rule()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
+1
-1
@@ -375,7 +375,7 @@ impl ToRust for TopLevelKind {
|
||||
cg.indent += 1;
|
||||
|
||||
for field in fields {
|
||||
let ty = field.decl.type_.clone().unwrap().get_rust();
|
||||
let ty = field.decl.type_.clone().get_rust();
|
||||
cg.add_indentedln(&format!("pub {}: {},", field.decl.name.get_rust(), ty));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user