refactored AST statements

This commit is contained in:
2026-05-02 17:16:16 +02:00
parent 2a753e8af8
commit aae71f1d0e
3 changed files with 56 additions and 51 deletions
+40 -38
View File
@@ -56,53 +56,55 @@ pub enum Postfix {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum Statement { pub enum Statement {
// expr;
Expression(Expression), Expression(Expression),
// { ... }
Block(Block), Block(Block),
// let/const/var x = ... VarDecl(VarDeclStmt),
VarDecl { VarAssign(VarAssignStmt),
mutable: bool, If(IfStmt),
name: String, While(WhileStmt),
init: Option<Expression>, For(ForStmt),
type_: Option<TypeExpr>,
},
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>), Return(Option<Expression>),
Break, Break,
Continue, 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)] #[derive(Debug, Clone, Serialize)]
pub enum Expression { pub enum Expression {
Identifier(String), Identifier(String),
+6 -6
View File
@@ -197,12 +197,12 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
let name = var_decl.next().unwrap().as_str().to_string(); let name = var_decl.next().unwrap().as_str().to_string();
let init = inner.next().map(Expression::from); let init = inner.next().map(Expression::from);
Statement::VarDecl { Statement::VarDecl(VarDeclStmt {
mutable, mutable,
name: name.as_str().to_string(), name: name.as_str().to_string(),
init, init,
type_, type_,
} })
} }
Rule::return_stmt => { Rule::return_stmt => {
@@ -225,11 +225,11 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
let else_branch = inner.next().map(Statement::from); let else_branch = inner.next().map(Statement::from);
Statement::If { Statement::If(IfStmt {
condition, condition,
then_branch: Box::new(then_branch), then_branch: Box::new(then_branch),
else_branch: else_branch.map(Box::new), else_branch: else_branch.map(Box::new),
} })
} }
Rule::while_stmt => { Rule::while_stmt => {
@@ -238,10 +238,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
let condition = Expression::from(inner.next().unwrap()); let condition = Expression::from(inner.next().unwrap());
let body = Statement::from(inner.next().unwrap()); let body = Statement::from(inner.next().unwrap());
Statement::While { Statement::While(WhileStmt {
condition, condition,
body: Box::new(body), body: Box::new(body),
} })
} }
_ => unimplemented!( _ => unimplemented!(
+10 -7
View File
@@ -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 { pub struct RustCodegen {
output: String, output: String,
@@ -128,12 +131,12 @@ impl RustCodegen {
self.add_indentedln("}"); self.add_indentedln("}");
} }
Statement::VarDecl { Statement::VarDecl(VarDeclStmt {
mutable, mutable,
name, name,
init, init,
type_, type_,
} => { }) => {
let mutability = if *mutable { "mut " } else { "" }; let mutability = if *mutable { "mut " } else { "" };
let ty = type_ let ty = type_
@@ -149,7 +152,7 @@ impl RustCodegen {
self.add_indentedln(&format!("let {}{}{}{};", mutability, name, ty, init)); self.add_indentedln(&format!("let {}{}{}{};", mutability, name, ty, init));
} }
Statement::VarAssign { target, value } => { Statement::VarAssign(VarAssignStmt { target, value }) => {
self.add_indentedln(&format!( self.add_indentedln(&format!(
"{} = {};", "{} = {};",
self.generate_expression(target), self.generate_expression(target),
@@ -157,11 +160,11 @@ impl RustCodegen {
)); ));
} }
Statement::If { Statement::If(IfStmt {
condition, condition,
then_branch, then_branch,
else_branch, else_branch,
} => { }) => {
self.add_indentedln(&format!("if {} {{", self.generate_expression(condition))); self.add_indentedln(&format!("if {} {{", self.generate_expression(condition)));
self.indent += 1; 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.add_indentedln(&format!("while {} {{", self.generate_expression(condition)));
self.indent += 1; self.indent += 1;