refactored AST statements
This commit is contained in:
+40
-38
@@ -56,53 +56,55 @@ pub enum Postfix {
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Statement {
|
||||
// expr;
|
||||
Expression(Expression),
|
||||
|
||||
// { ... }
|
||||
Block(Block),
|
||||
|
||||
// let/const/var x = ...
|
||||
VarDecl {
|
||||
mutable: bool,
|
||||
name: String,
|
||||
init: Option<Expression>,
|
||||
type_: Option<TypeExpr>,
|
||||
},
|
||||
VarDecl(VarDeclStmt),
|
||||
VarAssign(VarAssignStmt),
|
||||
If(IfStmt),
|
||||
While(WhileStmt),
|
||||
For(ForStmt),
|
||||
|
||||
VarAssign {
|
||||
target: Expression,
|
||||
value: Expression,
|
||||
},
|
||||
|
||||
// if (...) stmt else stmt
|
||||
If {
|
||||
condition: Expression,
|
||||
then_branch: Box<Statement>,
|
||||
else_branch: Option<Box<Statement>>,
|
||||
},
|
||||
|
||||
// while (...) stmt
|
||||
While {
|
||||
condition: Expression,
|
||||
body: Box<Statement>,
|
||||
},
|
||||
|
||||
// for (...) stmt
|
||||
For {
|
||||
init: (bool, String, Option<Expression>),
|
||||
condition: Option<Expression>,
|
||||
update: Option<Box<Statement>>,
|
||||
body: Box<Statement>,
|
||||
},
|
||||
|
||||
// return expr?;
|
||||
Return(Option<Expression>),
|
||||
|
||||
Break,
|
||||
Continue,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct VarDeclStmt {
|
||||
pub mutable: bool,
|
||||
pub name: String,
|
||||
pub init: Option<Expression>,
|
||||
pub type_: Option<TypeExpr>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct VarAssignStmt {
|
||||
pub target: Expression,
|
||||
pub value: Expression,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct IfStmt {
|
||||
pub condition: Expression,
|
||||
pub then_branch: Box<Statement>,
|
||||
pub else_branch: Option<Box<Statement>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct WhileStmt {
|
||||
pub condition: Expression,
|
||||
pub body: Box<Statement>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ForStmt {
|
||||
pub init: (bool, String, Option<Expression>),
|
||||
pub condition: Option<Expression>,
|
||||
pub update: Option<Box<Statement>>,
|
||||
pub body: Box<Statement>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Expression {
|
||||
Identifier(String),
|
||||
|
||||
+6
-6
@@ -197,12 +197,12 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
||||
let name = var_decl.next().unwrap().as_str().to_string();
|
||||
let init = inner.next().map(Expression::from);
|
||||
|
||||
Statement::VarDecl {
|
||||
Statement::VarDecl(VarDeclStmt {
|
||||
mutable,
|
||||
name: name.as_str().to_string(),
|
||||
init,
|
||||
type_,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Rule::return_stmt => {
|
||||
@@ -225,11 +225,11 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
||||
|
||||
let else_branch = inner.next().map(Statement::from);
|
||||
|
||||
Statement::If {
|
||||
Statement::If(IfStmt {
|
||||
condition,
|
||||
then_branch: Box::new(then_branch),
|
||||
else_branch: else_branch.map(Box::new),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Rule::while_stmt => {
|
||||
@@ -238,10 +238,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
||||
let condition = Expression::from(inner.next().unwrap());
|
||||
let body = Statement::from(inner.next().unwrap());
|
||||
|
||||
Statement::While {
|
||||
Statement::While(WhileStmt {
|
||||
condition,
|
||||
body: Box::new(body),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
_ => unimplemented!(
|
||||
|
||||
+10
-7
@@ -1,4 +1,7 @@
|
||||
use parser::ast::{BinaryOp, Block, Expression, Postfix, Statement, TopLevel, TypeExpr};
|
||||
use parser::ast::{
|
||||
BinaryOp, Block, Expression, IfStmt, Postfix, Statement, TopLevel, TypeExpr, VarAssignStmt,
|
||||
VarDeclStmt, WhileStmt,
|
||||
};
|
||||
|
||||
pub struct RustCodegen {
|
||||
output: String,
|
||||
@@ -128,12 +131,12 @@ impl RustCodegen {
|
||||
self.add_indentedln("}");
|
||||
}
|
||||
|
||||
Statement::VarDecl {
|
||||
Statement::VarDecl(VarDeclStmt {
|
||||
mutable,
|
||||
name,
|
||||
init,
|
||||
type_,
|
||||
} => {
|
||||
}) => {
|
||||
let mutability = if *mutable { "mut " } else { "" };
|
||||
|
||||
let ty = type_
|
||||
@@ -149,7 +152,7 @@ impl RustCodegen {
|
||||
self.add_indentedln(&format!("let {}{}{}{};", mutability, name, ty, init));
|
||||
}
|
||||
|
||||
Statement::VarAssign { target, value } => {
|
||||
Statement::VarAssign(VarAssignStmt { target, value }) => {
|
||||
self.add_indentedln(&format!(
|
||||
"{} = {};",
|
||||
self.generate_expression(target),
|
||||
@@ -157,11 +160,11 @@ impl RustCodegen {
|
||||
));
|
||||
}
|
||||
|
||||
Statement::If {
|
||||
Statement::If(IfStmt {
|
||||
condition,
|
||||
then_branch,
|
||||
else_branch,
|
||||
} => {
|
||||
}) => {
|
||||
self.add_indentedln(&format!("if {} {{", self.generate_expression(condition)));
|
||||
|
||||
self.indent += 1;
|
||||
@@ -179,7 +182,7 @@ impl RustCodegen {
|
||||
}
|
||||
}
|
||||
|
||||
Statement::While { condition, body } => {
|
||||
Statement::While(WhileStmt { condition, body }) => {
|
||||
self.add_indentedln(&format!("while {} {{", self.generate_expression(condition)));
|
||||
|
||||
self.indent += 1;
|
||||
|
||||
Reference in New Issue
Block a user