Skip to content

Commit

Permalink
Split parse::Stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lemolatoon committed Mar 14, 2023
1 parent 317668a commit 241b2ad
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 78 deletions.
3 changes: 2 additions & 1 deletion src/analyze/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use crate::{
expr::{BinOpKind, Binary, Expr, ExprKind, SizeOfOperandKind, UnaryOp},
parse::{
EnumConstant, EnumSpec, ForInitKind, LabelKind, Program, ProgramComponent, ProgramKind,
Stmt, StmtKind,
StmtKind,
},
stmt::Stmt,
},
tokenize::{debug_infos::DebugInfo, tokenize::AssignBinOpToken},
unimplemented_err,
Expand Down
1 change: 1 addition & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod expr;
pub mod parse;
pub mod parser_context;
pub mod scope;
pub mod stmt;
78 changes: 1 addition & 77 deletions src/parse/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::{
expr::{BinOpKind, Expr, UnaryOp},
parser_context::{ParserContext, ParserContextKind},
scope::Scope,
stmt::Stmt,
};

pub struct Parser {
Expand Down Expand Up @@ -1427,89 +1428,12 @@ impl ProgramKind {
}
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub struct Stmt {
pub kind: StmtKind,
pub debug_info: DebugInfo,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct EnumConstant {
pub ident: String,
pub debug_info: DebugInfo,
}

impl Stmt {
pub const fn expr(expr: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Expr(expr),
debug_info,
}
}

pub const fn ret(expr: Option<Expr>, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Return(expr),
debug_info,
}
}

pub fn new_block(stmts: Vec<Stmt>, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Block(stmts),
debug_info,
}
}

pub fn new_if(cond: Expr, then: Stmt, els: Option<Stmt>, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::If(cond, Box::new(then), els.map(Box::new)),
debug_info,
}
}

pub fn new_while(cond: Expr, then: Stmt, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::While(cond, Box::new(then)),
debug_info,
}
}

pub fn new_for(
init: Option<ForInitKind>,
cond: Option<Expr>,
inc: Option<Expr>,
then: Stmt,
debug_info: DebugInfo,
) -> Self {
Self {
kind: StmtKind::For(init, cond, inc, Box::new(then)),
debug_info,
}
}

pub fn new_switch(expr: Expr, stmt: Stmt, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Switch(expr, Box::new(stmt)),
debug_info,
}
}

pub fn new_labeled_stmt(label_kind: LabelKind, stmt: Stmt, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Labeled(label_kind, Box::new(stmt)),
debug_info,
}
}

pub const fn new_declare(declaration: Declaration, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Declare(declaration),
debug_info,
}
}
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub enum EnumSpec {
WithList(
Expand Down
84 changes: 84 additions & 0 deletions src/parse/stmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use crate::tokenize::debug_infos::DebugInfo;

use super::{
declaration::Declaration,
expr::Expr,
parse::{ForInitKind, LabelKind, StmtKind},
};

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub struct Stmt {
pub kind: StmtKind,
pub debug_info: DebugInfo,
}

impl Stmt {
pub const fn expr(expr: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Expr(expr),
debug_info,
}
}

pub const fn ret(expr: Option<Expr>, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Return(expr),
debug_info,
}
}

pub fn new_block(stmts: Vec<Stmt>, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Block(stmts),
debug_info,
}
}

pub fn new_if(cond: Expr, then: Stmt, els: Option<Stmt>, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::If(cond, Box::new(then), els.map(Box::new)),
debug_info,
}
}

pub fn new_while(cond: Expr, then: Stmt, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::While(cond, Box::new(then)),
debug_info,
}
}

pub fn new_for(
init: Option<ForInitKind>,
cond: Option<Expr>,
inc: Option<Expr>,
then: Stmt,
debug_info: DebugInfo,
) -> Self {
Self {
kind: StmtKind::For(init, cond, inc, Box::new(then)),
debug_info,
}
}

pub fn new_switch(expr: Expr, stmt: Stmt, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Switch(expr, Box::new(stmt)),
debug_info,
}
}

pub fn new_labeled_stmt(label_kind: LabelKind, stmt: Stmt, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Labeled(label_kind, Box::new(stmt)),
debug_info,
}
}

pub const fn new_declare(declaration: Declaration, debug_info: DebugInfo) -> Self {
Self {
kind: StmtKind::Declare(declaration),
debug_info,
}
}
}
1 change: 1 addition & 0 deletions tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ pub mod ast {
},
expr::{BinOpKind, Expr, UnaryOp},
parse::*,
stmt::Stmt,
},
tokenize::debug_infos::DebugInfo,
};
Expand Down

0 comments on commit 241b2ad

Please sign in to comment.