generics codege
This commit is contained in:
+36
-4
@@ -1,6 +1,6 @@
|
||||
use parser::ast::{
|
||||
Attribute, BinaryOp, Block, EnumItem, Expression, FunctionDecl, Identifier, Literal, Path,
|
||||
Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr,
|
||||
Attribute, BinaryOp, Block, EnumItem, Expression, FunctionDecl, Generics, Identifier, Literal,
|
||||
Path, Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr,
|
||||
TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, Visibility,
|
||||
};
|
||||
|
||||
@@ -316,12 +316,14 @@ impl ToRust for TopLevelKind {
|
||||
Self::StructDecl {
|
||||
visibility,
|
||||
name,
|
||||
generics,
|
||||
fields,
|
||||
} => {
|
||||
cg.addln(&format!(
|
||||
"{}struct {} {{",
|
||||
"{}struct {}{} {{",
|
||||
visibility.get_rust(),
|
||||
name.get_rust()
|
||||
name.get_rust(),
|
||||
generics.get_rust()
|
||||
));
|
||||
cg.indent += 1;
|
||||
|
||||
@@ -703,6 +705,36 @@ impl GetRust for Pattern {
|
||||
}
|
||||
}
|
||||
|
||||
impl GetRust for Generics {
|
||||
fn get_rust(&self) -> String {
|
||||
if self.0.len() == 0 {
|
||||
String::new()
|
||||
} else {
|
||||
format!(
|
||||
"<{}>",
|
||||
self.0
|
||||
.iter()
|
||||
.map(|generic| generic.0.get_rust()
|
||||
+ &(if generic.1.len() == 0 {
|
||||
String::new()
|
||||
} else {
|
||||
format!(
|
||||
": {}",
|
||||
generic
|
||||
.1
|
||||
.iter()
|
||||
.map(Path::get_rust)
|
||||
.collect::<Vec<_>>()
|
||||
.join("+")
|
||||
)
|
||||
}))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_static_type_path(path: &Path) -> String {
|
||||
let rust_path = path.get_rust();
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ mod my_class;
|
||||
use <my_class::MyClass>;
|
||||
use <my_class::MyEnum>;
|
||||
|
||||
struct Test<T> {}
|
||||
|
||||
void main() {
|
||||
var my_class = new MyClass("World");
|
||||
my_class.greet();
|
||||
|
||||
Reference in New Issue
Block a user