Codegen
This commit is contained in:
+62
-7
@@ -1,8 +1,8 @@
|
|||||||
use mist_parser::ast::{
|
use mist_parser::ast::{
|
||||||
Attribute, BinaryOp, Block, ClassItem, EnumItem, Expression, FieldDecl, FunctionDecl, Generic,
|
Attribute, BinaryOp, Block, ClassItem, EnumItem, ExprPath, ExprPathSegment, Expression,
|
||||||
Generics, Identifier, ImplDecl, Literal, Path, Pattern, Postfix, Prefix, Statement,
|
FieldDecl, FunctionDecl, Generic, Generics, Identifier, ImplDecl, Literal, Path, Pattern,
|
||||||
StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarDecl,
|
Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind,
|
||||||
VarDeclStmt, Visibility,
|
TypePostfix, VarDecl, VarDeclStmt, Visibility,
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -137,6 +137,17 @@ impl GetRust for Literal {
|
|||||||
.join(", ")
|
.join(", ")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Self::Array(values) => format!(
|
||||||
|
"[{}]",
|
||||||
|
values
|
||||||
|
.iter()
|
||||||
|
.map(Expression::get_rust)
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
),
|
||||||
|
Self::ArrayRepeat(value, repeat) => {
|
||||||
|
format!("[{}; {}]", value.get_rust(), repeat.get_rust())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -172,6 +183,13 @@ impl GetRust for Expression {
|
|||||||
BinaryOp::GreaterThanOrEqual => ">=",
|
BinaryOp::GreaterThanOrEqual => ">=",
|
||||||
BinaryOp::And => "&&",
|
BinaryOp::And => "&&",
|
||||||
BinaryOp::Or => "||",
|
BinaryOp::Or => "||",
|
||||||
|
BinaryOp::ShiftLeft => "<<",
|
||||||
|
BinaryOp::ShiftRight => ">>",
|
||||||
|
BinaryOp::RangeInclusive => "..=",
|
||||||
|
BinaryOp::RangeExclusive => "..",
|
||||||
|
BinaryOp::BitAnd => "&",
|
||||||
|
BinaryOp::BitOr => "|",
|
||||||
|
BinaryOp::BitXor => "^",
|
||||||
};
|
};
|
||||||
format!("{} {} {}", lhs.get_rust(), op_str, rhs.get_rust())
|
format!("{} {} {}", lhs.get_rust(), op_str, rhs.get_rust())
|
||||||
}
|
}
|
||||||
@@ -187,6 +205,9 @@ impl GetRust for Prefix {
|
|||||||
Self::RefMut => "&mut ",
|
Self::RefMut => "&mut ",
|
||||||
Self::Not => "!",
|
Self::Not => "!",
|
||||||
Self::New => "",
|
Self::New => "",
|
||||||
|
Self::Neg => "-",
|
||||||
|
Self::RangeInclusive => "..=",
|
||||||
|
Self::RangeExclusive => "..",
|
||||||
}
|
}
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
@@ -198,6 +219,29 @@ impl GetRust for [Prefix] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl GetRust for ExprPath {
|
||||||
|
fn get_rust(&self) -> String {
|
||||||
|
self.segments
|
||||||
|
.iter()
|
||||||
|
.map(ExprPathSegment::get_rust)
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GetRust for ExprPathSegment {
|
||||||
|
fn get_rust(&self) -> String {
|
||||||
|
format!(
|
||||||
|
"{}{}",
|
||||||
|
self.ident.get_rust(),
|
||||||
|
self.generics
|
||||||
|
.as_ref()
|
||||||
|
.map(|v| format!("::{}", v.get_rust()))
|
||||||
|
.unwrap_or_default()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl GetRust for Option<&Vec<Prefix>> {
|
impl GetRust for Option<&Vec<Prefix>> {
|
||||||
fn get_rust(&self) -> String {
|
fn get_rust(&self) -> String {
|
||||||
self.map(|prefixes| {
|
self.map(|prefixes| {
|
||||||
@@ -218,7 +262,14 @@ impl GetRust for Option<&Vec<Prefix>> {
|
|||||||
impl GetRust for Postfix {
|
impl GetRust for Postfix {
|
||||||
fn get_rust(&self) -> String {
|
fn get_rust(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Postfix::FieldAccess(field) => format!(".{}", field.get_rust()),
|
Postfix::FieldAccess(field, generics) => format!(
|
||||||
|
".{}{}",
|
||||||
|
field.get_rust(),
|
||||||
|
generics
|
||||||
|
.as_ref()
|
||||||
|
.map(|v| format!("::{}", v.get_rust()))
|
||||||
|
.unwrap_or_default()
|
||||||
|
),
|
||||||
|
|
||||||
Postfix::Call(args) => {
|
Postfix::Call(args) => {
|
||||||
let args = args
|
let args = args
|
||||||
@@ -244,8 +295,12 @@ impl GetRust for Postfix {
|
|||||||
|
|
||||||
Postfix::Index(idx) => {
|
Postfix::Index(idx) => {
|
||||||
format!("[{}]", idx.get_rust())
|
format!("[{}]", idx.get_rust())
|
||||||
} // Note: Postfix::Binary variant logic has been completely transferred
|
}
|
||||||
// to Expression::Binary to match your updated AST configuration.
|
|
||||||
|
Postfix::As(ty) => format!("as {}", ty.get_rust()),
|
||||||
|
|
||||||
|
Postfix::RangeInclusive => String::from("..="),
|
||||||
|
Postfix::RangeExclusive => String::from(".."),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user