Enum codegen
This commit is contained in:
+65
-6
@@ -1,6 +1,6 @@
|
|||||||
use parser::ast::{
|
use parser::ast::{
|
||||||
Attribute, BinaryOp, Block, Expression, FunctionDecl, Identifier, Literal, Path, Postfix,
|
Attribute, BinaryOp, Block, EnumItem, Expression, FunctionDecl, Identifier, Literal, Path,
|
||||||
Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind,
|
Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind,
|
||||||
TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, Visibility,
|
TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, Visibility,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -311,12 +311,35 @@ impl ToRust for TopLevelKind {
|
|||||||
Self::Import(path) => cg.addln(&format!("use {};", path.get_rust())),
|
Self::Import(path) => cg.addln(&format!("use {};", path.get_rust())),
|
||||||
Self::Mod(id) => cg.addln(&format!("mod {};", id.get_rust())),
|
Self::Mod(id) => cg.addln(&format!("mod {};", id.get_rust())),
|
||||||
Self::FunctionDecl(decl) => decl.to_rust(cg),
|
Self::FunctionDecl(decl) => decl.to_rust(cg),
|
||||||
|
Self::EnumDecl {
|
||||||
|
visibility,
|
||||||
|
name,
|
||||||
|
fields,
|
||||||
|
} => {
|
||||||
|
cg.addln(&format!(
|
||||||
|
"{}struct {} {{",
|
||||||
|
visibility.get_rust(),
|
||||||
|
name.get_rust()
|
||||||
|
));
|
||||||
|
cg.indent += 1;
|
||||||
|
|
||||||
|
for field in fields {
|
||||||
|
cg.add_indentedln(&(format!("{}", field.get_rust()) + ","));
|
||||||
|
}
|
||||||
|
|
||||||
|
cg.indent -= 1;
|
||||||
|
cg.addln("}\n");
|
||||||
|
}
|
||||||
Self::StructDecl {
|
Self::StructDecl {
|
||||||
visibility,
|
visibility,
|
||||||
name,
|
name,
|
||||||
fields,
|
fields,
|
||||||
} => {
|
} => {
|
||||||
cg.addln(&format!("{}struct {} {{", visibility.get_rust(), name.get_rust()));
|
cg.addln(&format!(
|
||||||
|
"{}struct {} {{",
|
||||||
|
visibility.get_rust(),
|
||||||
|
name.get_rust()
|
||||||
|
));
|
||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|
||||||
for (field_name, _, ty) in &fields.0 {
|
for (field_name, _, ty) in &fields.0 {
|
||||||
@@ -335,7 +358,11 @@ impl ToRust for TopLevelKind {
|
|||||||
methods,
|
methods,
|
||||||
} => {
|
} => {
|
||||||
// Struct decl
|
// Struct decl
|
||||||
cg.addln(&format!("{}struct {} {{", visibility.get_rust(), name.get_rust()));
|
cg.addln(&format!(
|
||||||
|
"{}struct {} {{",
|
||||||
|
visibility.get_rust(),
|
||||||
|
name.get_rust()
|
||||||
|
));
|
||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|
||||||
for field in fields {
|
for field in fields {
|
||||||
@@ -501,7 +528,11 @@ impl ToRust for Statement {
|
|||||||
iterator,
|
iterator,
|
||||||
body,
|
body,
|
||||||
} => {
|
} => {
|
||||||
cg.add_indentedln(&format!("for {} in {}", pattern.get_rust(), iterator.get_rust()));
|
cg.add_indentedln(&format!(
|
||||||
|
"for {} in {}",
|
||||||
|
pattern.get_rust(),
|
||||||
|
iterator.get_rust()
|
||||||
|
));
|
||||||
cg.ensure_brackets(body);
|
cg.ensure_brackets(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -556,7 +587,11 @@ impl GetRust for VarDecl {
|
|||||||
|
|
||||||
impl GetRust for Path {
|
impl GetRust for Path {
|
||||||
fn get_rust(&self) -> String {
|
fn get_rust(&self) -> String {
|
||||||
self.0.iter().map(Identifier::get_rust).collect::<Vec<String>>().join("::")
|
self.0
|
||||||
|
.iter()
|
||||||
|
.map(Identifier::get_rust)
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("::")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -585,6 +620,30 @@ impl GetRust for Identifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl GetRust for EnumItem {
|
||||||
|
fn get_rust(&self) -> String {
|
||||||
|
match self {
|
||||||
|
Self::Named(id) => id.get_rust(),
|
||||||
|
Self::Struct(id, s) => format!(
|
||||||
|
"{} {{{}}}",
|
||||||
|
id.get_rust(),
|
||||||
|
s.0.iter()
|
||||||
|
.map(|(id, _, ty)| format!("{}: {}", id.get_rust(), ty.get_rust()))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
Self::Tuple(id, t) => format!(
|
||||||
|
"{} ({})",
|
||||||
|
id.get_rust(),
|
||||||
|
t.iter()
|
||||||
|
.map(TypeExpr::get_rust)
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_static_type_path(path: &Path) -> String {
|
pub fn get_static_type_path(path: &Path) -> String {
|
||||||
let rust_path = path.get_rust();
|
let rust_path = path.get_rust();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user