Refactored codegen
This commit is contained in:
+1
-1
@@ -366,7 +366,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
|
|||||||
|
|
||||||
VarDecl {
|
VarDecl {
|
||||||
mutable,
|
mutable,
|
||||||
name: name.as_str().to_string(),
|
name,
|
||||||
type_,
|
type_,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-42
@@ -1,6 +1,6 @@
|
|||||||
use parser::ast::{
|
use parser::ast::{
|
||||||
BinaryOp, Block, Expression, IfStmt, Postfix, Statement, TopLevel, TypeExpr, VarAssignStmt,
|
BinaryOp, Block, Expression, IfStmt, Postfix, Statement, TopLevel, TypeExpr, VarAssignStmt,
|
||||||
VarDeclStmt, WhileStmt,
|
VarDecl, VarDeclStmt, WhileStmt,
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -16,7 +16,7 @@ pub trait ToRust {
|
|||||||
/// Implemented by nodes that *produce* a `String` without mutating the codegen.
|
/// Implemented by nodes that *produce* a `String` without mutating the codegen.
|
||||||
/// Only needs `&RustCodegen` (e.g. for indent level or helper access).
|
/// Only needs `&RustCodegen` (e.g. for indent level or helper access).
|
||||||
pub trait GetRust {
|
pub trait GetRust {
|
||||||
fn get_rust(&self, cg: &RustCodegen) -> String;
|
fn get_rust(&self) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -73,7 +73,7 @@ impl Default for RustCodegen {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
impl GetRust for TypeExpr {
|
impl GetRust for TypeExpr {
|
||||||
fn get_rust(&self, _cg: &RustCodegen) -> String {
|
fn get_rust(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
TypeExpr::Identifier(name) => match name.as_str() {
|
TypeExpr::Identifier(name) => match name.as_str() {
|
||||||
"int" => "i32".into(),
|
"int" => "i32".into(),
|
||||||
@@ -88,7 +88,7 @@ impl GetRust for TypeExpr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GetRust for Expression {
|
impl GetRust for Expression {
|
||||||
fn get_rust(&self, cg: &RustCodegen) -> String {
|
fn get_rust(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Expression::Identifier(name) => name.clone(),
|
Expression::Identifier(name) => name.clone(),
|
||||||
Expression::IntLiteral(n) => n.to_string(),
|
Expression::IntLiteral(n) => n.to_string(),
|
||||||
@@ -97,8 +97,8 @@ impl GetRust for Expression {
|
|||||||
Expression::StringLiteral(s) => format!("\"{}\".to_string()", s),
|
Expression::StringLiteral(s) => format!("\"{}\".to_string()", s),
|
||||||
|
|
||||||
Expression::Postfix { initial, postfixes } => {
|
Expression::Postfix { initial, postfixes } => {
|
||||||
let base = initial.get_rust(cg);
|
let base = initial.get_rust();
|
||||||
postfixes.get_rust_with_base(cg, &base)
|
postfixes.get_rust_with_base(&base)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,11 +106,11 @@ impl GetRust for Expression {
|
|||||||
|
|
||||||
/// Helper — applies a slice of postfixes onto an already-rendered base string.
|
/// Helper — applies a slice of postfixes onto an already-rendered base string.
|
||||||
trait PostfixChain {
|
trait PostfixChain {
|
||||||
fn get_rust_with_base(&self, cg: &RustCodegen, base: &str) -> String;
|
fn get_rust_with_base(&self, base: &str) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PostfixChain for [Postfix] {
|
impl PostfixChain for [Postfix] {
|
||||||
fn get_rust_with_base(&self, cg: &RustCodegen, base: &str) -> String {
|
fn get_rust_with_base(&self, base: &str) -> String {
|
||||||
let mut result = base.to_string();
|
let mut result = base.to_string();
|
||||||
|
|
||||||
for postfix in self {
|
for postfix in self {
|
||||||
@@ -120,7 +120,7 @@ impl PostfixChain for [Postfix] {
|
|||||||
Postfix::Call(args) => {
|
Postfix::Call(args) => {
|
||||||
let args = args
|
let args = args
|
||||||
.iter()
|
.iter()
|
||||||
.map(|a| a.get_rust(cg))
|
.map(|a| a.get_rust())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", ");
|
.join(", ");
|
||||||
format!("{}({})", result, args)
|
format!("{}({})", result, args)
|
||||||
@@ -129,14 +129,14 @@ impl PostfixChain for [Postfix] {
|
|||||||
Postfix::StructCall(fields) => {
|
Postfix::StructCall(fields) => {
|
||||||
let fields = fields
|
let fields = fields
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(k, v)| format!("{}: {}", k, v.get_rust(cg)))
|
.map(|(k, v)| format!("{}: {}", k, v.get_rust()))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", ");
|
.join(", ");
|
||||||
format!("{} {{ {} }}", result, fields)
|
format!("{} {{ {} }}", result, fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
Postfix::Index(idx) => {
|
Postfix::Index(idx) => {
|
||||||
format!("{}[{}]", result, idx.get_rust(cg))
|
format!("{}[{}]", result, idx.get_rust())
|
||||||
}
|
}
|
||||||
|
|
||||||
Postfix::Binary(op, rhs) => {
|
Postfix::Binary(op, rhs) => {
|
||||||
@@ -153,7 +153,7 @@ impl PostfixChain for [Postfix] {
|
|||||||
BinaryOp::LessThanOrEqual => "<=",
|
BinaryOp::LessThanOrEqual => "<=",
|
||||||
BinaryOp::GreaterThanOrEqual => ">=",
|
BinaryOp::GreaterThanOrEqual => ">=",
|
||||||
};
|
};
|
||||||
format!("{} {} {}", result, op_str, rhs.get_rust(cg))
|
format!("{} {} {}", result, op_str, rhs.get_rust())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -192,7 +192,7 @@ impl ToRust for TopLevel {
|
|||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|
||||||
for (field_name, (_, ty)) in &fields.0 {
|
for (field_name, (_, ty)) in &fields.0 {
|
||||||
let ty = ty.get_rust(cg);
|
let ty = ty.get_rust();
|
||||||
cg.add_indentedln(&format!("pub {}: {},", field_name, ty));
|
cg.add_indentedln(&format!("pub {}: {},", field_name, ty));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,21 +212,13 @@ impl ToRust for TopLevel {
|
|||||||
let params_str = params
|
let params_str = params
|
||||||
.0
|
.0
|
||||||
.iter()
|
.iter()
|
||||||
.map(|v| {
|
.map(VarDecl::get_rust)
|
||||||
format!(
|
|
||||||
"{name}{}",
|
|
||||||
v.type_
|
|
||||||
.as_ref()
|
|
||||||
.map(|t| format!(": {}", t.get_rust(cg)))
|
|
||||||
.unwrap_or_default()
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", ");
|
.join(", ");
|
||||||
|
|
||||||
let ret = return_type
|
let ret = return_type
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|t| format!(" -> {}", t.get_rust(cg)))
|
.map(|t| format!(" -> {}", t.get_rust()))
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
cg.addln(&format!("{}fn {}({}){} {{", vis, name, params_str, ret));
|
cg.addln(&format!("{}fn {}({}){} {{", vis, name, params_str, ret));
|
||||||
@@ -243,7 +235,7 @@ impl ToRust for Statement {
|
|||||||
fn to_rust(&self, cg: &mut RustCodegen) {
|
fn to_rust(&self, cg: &mut RustCodegen) {
|
||||||
match self {
|
match self {
|
||||||
Statement::Expression(expr) => {
|
Statement::Expression(expr) => {
|
||||||
cg.add_indentedln(&format!("{};", expr.get_rust(cg)));
|
cg.add_indentedln(&format!("{};", expr.get_rust()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Statement::Block(block) => {
|
Statement::Block(block) => {
|
||||||
@@ -255,28 +247,16 @@ impl ToRust for Statement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Statement::VarDecl(VarDeclStmt { decl, init }) => {
|
Statement::VarDecl(VarDeclStmt { decl, init }) => {
|
||||||
let mutability = if decl.mutable { "mut " } else { "" };
|
|
||||||
|
|
||||||
let ty = decl
|
|
||||||
.type_
|
|
||||||
.as_ref()
|
|
||||||
.map(|t| format!(": {}", t.get_rust(cg)))
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
let init = init
|
let init = init
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|e| format!(" = {}", e.get_rust(cg)))
|
.map(|e| format!(" = {}", e.get_rust()))
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
cg.add_indentedln(&format!("let {}{}{}{};", mutability, decl.name, ty, init));
|
cg.add_indentedln(&format!("let {}{};", decl.get_rust(), init));
|
||||||
}
|
}
|
||||||
|
|
||||||
Statement::VarAssign(VarAssignStmt { target, value }) => {
|
Statement::VarAssign(VarAssignStmt { target, value }) => {
|
||||||
cg.add_indentedln(&format!(
|
cg.add_indentedln(&format!("{} = {};", target.get_rust(), value.get_rust(),));
|
||||||
"{} = {};",
|
|
||||||
target.get_rust(cg),
|
|
||||||
value.get_rust(cg),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Statement::If(IfStmt {
|
Statement::If(IfStmt {
|
||||||
@@ -284,7 +264,7 @@ impl ToRust for Statement {
|
|||||||
then_branch,
|
then_branch,
|
||||||
else_branch,
|
else_branch,
|
||||||
}) => {
|
}) => {
|
||||||
cg.add_indentedln(&format!("if {} {{", condition.get_rust(cg)));
|
cg.add_indentedln(&format!("if {} {{", condition.get_rust()));
|
||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
then_branch.to_rust(cg);
|
then_branch.to_rust(cg);
|
||||||
cg.indent -= 1;
|
cg.indent -= 1;
|
||||||
@@ -300,7 +280,7 @@ impl ToRust for Statement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Statement::While(WhileStmt { condition, body }) => {
|
Statement::While(WhileStmt { condition, body }) => {
|
||||||
cg.add_indentedln(&format!("while {} {{", condition.get_rust(cg)));
|
cg.add_indentedln(&format!("while {} {{", condition.get_rust()));
|
||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
body.to_rust(cg);
|
body.to_rust(cg);
|
||||||
cg.indent -= 1;
|
cg.indent -= 1;
|
||||||
@@ -312,7 +292,7 @@ impl ToRust for Statement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Statement::Return(expr) => {
|
Statement::Return(expr) => {
|
||||||
let val = expr.as_ref().map(|e| e.get_rust(cg)).unwrap_or_default();
|
let val = expr.as_ref().map(|e| e.get_rust()).unwrap_or_default();
|
||||||
cg.add_indentedln(&format!("return {};", val));
|
cg.add_indentedln(&format!("return {};", val));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,3 +301,17 @@ impl ToRust for Statement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl GetRust for VarDecl {
|
||||||
|
fn get_rust(&self) -> String {
|
||||||
|
let mutability = if self.mutable { "mut " } else { "" };
|
||||||
|
|
||||||
|
let ty = self
|
||||||
|
.type_
|
||||||
|
.as_ref()
|
||||||
|
.map(|t| format!(": {}", t.get_rust()))
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
format!("{}{}{}", mutability, self.name, ty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user