Skip to content

Commit

Permalink
Split parse::Expr
Browse files Browse the repository at this point in the history
  • Loading branch information
lemolatoon committed Mar 14, 2023
1 parent 63add3a commit 317668a
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 282 deletions.
5 changes: 3 additions & 2 deletions src/analyze/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use crate::{
DeclarationSpecifier, Declarator, DirectDeclarator, Initializer, StorageClassSpecifier,
StructOrUnionSpec, TypeSpecifier,
},
expr::{BinOpKind, Binary, Expr, ExprKind, SizeOfOperandKind, UnaryOp},
parse::{
BinOpKind, Binary, EnumConstant, EnumSpec, Expr, ExprKind, ForInitKind, LabelKind,
Program, ProgramComponent, ProgramKind, SizeOfOperandKind, Stmt, StmtKind, UnaryOp,
EnumConstant, EnumSpec, ForInitKind, LabelKind, Program, ProgramComponent, ProgramKind,
Stmt, StmtKind,
},
},
tokenize::{debug_infos::DebugInfo, tokenize::AssignBinOpToken},
Expand Down
2 changes: 1 addition & 1 deletion src/generate/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
FuncCallTargetKind, GVar, LVar, LoopControlKind, Type,
},
error::{CompileError, UnexpectedTypeSizeStatus},
parse::parse::BinOpKind,
parse::expr::BinOpKind,
unimplemented_err,
};

Expand Down
1 change: 1 addition & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod declaration;
pub mod expr;
pub mod parse;
pub mod parser_context;
pub mod scope;
2 changes: 1 addition & 1 deletion src/parse/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
unimplemented_err,
};

use super::parse::{EnumSpec, Expr};
use super::{expr::Expr, parse::EnumSpec};

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
/// Declaration
Expand Down
279 changes: 279 additions & 0 deletions src/parse/expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
use crate::tokenize::{debug_infos::DebugInfo, tokenize::AssignBinOpToken};

use super::parse::TypeName;

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

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub enum ExprKind {
Binary(Binary),
Comma(Box<Expr>, Box<Expr>),
Num(isize),
StrLit(String),
Unary(UnaryOp, Box<Expr>),
Assign(Box<Expr>, Box<Expr>, AssignBinOpToken),
Ident(String),
Func(String, Vec<Expr>),
Deref(Box<Expr>),
Addr(Box<Expr>),
SizeOf(SizeOfOperandKind),
Array(Box<Expr>, Box<Expr>),
Member(Box<Expr>, String),
Arrow(Box<Expr>, String),
Conditional {
cond: Box<Expr>,
then: Box<Expr>,
els: Box<Expr>,
},
PostfixIncrement(Box<Expr>),
PostfixDecrement(Box<Expr>),
UnaryIncrement(Box<Expr>),
UnaryDecrement(Box<Expr>),
Asm(String),
NullPtr,
BuiltinVaStart(Box<Expr>, Box<Expr>),
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub enum SizeOfOperandKind {
Expr(Box<Expr>),
Type(TypeName),
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub enum UnaryOp {
Plus,
Increment,
Minus,
Decrement,
BitInvert,
LogicalNot,
}

impl Expr {
pub fn new_comma(lhs: Expr, rhs: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Comma(Box::new(lhs), Box::new(rhs)),
debug_info,
}
}
pub fn new_inline_asm(asm: String, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Asm(asm),
debug_info,
}
}

pub fn new_null_ptr(debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::NullPtr,
debug_info,
}
}

pub fn new_conditional(cond: Expr, then: Expr, els: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Conditional {
cond: Box::new(cond),
then: Box::new(then),
els: Box::new(els),
},
debug_info,
}
}
pub fn new_member(expr: Expr, ident: String, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Member(Box::new(expr), ident),
debug_info,
}
}
pub fn new_arrow(expr: Expr, ident: String, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Arrow(Box::new(expr), ident),
debug_info,
}
}

pub fn new_postfix_increment(expr: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::PostfixIncrement(Box::new(expr)),
debug_info,
}
}

pub fn new_postfix_decrement(expr: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::PostfixDecrement(Box::new(expr)),
debug_info,
}
}

pub fn new_unary_increment(expr: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::UnaryIncrement(Box::new(expr)),
debug_info,
}
}

pub fn new_unary_decrement(expr: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::UnaryDecrement(Box::new(expr)),
debug_info,
}
}
pub fn new_array(expr: Expr, index: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Array(Box::new(expr), Box::new(index)),
debug_info,
}
}

pub fn new_binary(kind: BinOpKind, lhs: Expr, rhs: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Binary(Binary::new(kind, Box::new(lhs), Box::new(rhs))),
debug_info,
}
}

pub const fn new_num(num: isize, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Num(num),
debug_info,
}
}

pub const fn new_str_lit(letters: String, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::StrLit(letters),
debug_info,
}
}

pub const fn new_lvar(name: String, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Ident(name),
debug_info,
}
}

pub fn new_unary(kind: UnaryOp, expr: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Unary(kind, Box::new(expr)),
debug_info,
}
}

pub fn new_assign(
lhs: Expr,
rhs: Expr,
assign_bin_op_token: AssignBinOpToken,
debug_info: DebugInfo,
) -> Self {
// pos is Position of TokenKind::Eq (i.e. `=`)
Self {
kind: ExprKind::Assign(Box::new(lhs), Box::new(rhs), assign_bin_op_token),
debug_info,
}
}

pub fn new_func(name: String, args: Vec<Expr>, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Func(name, args),
debug_info,
}
}

pub fn new_built_in_va_start(ap: Expr, last: Expr) -> Self {
let debug_info = ap.debug_info.clone();
Self {
kind: ExprKind::BuiltinVaStart(Box::new(ap), Box::new(last)),
debug_info,
}
}

pub fn new_deref(expr: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Deref(Box::new(expr)),
debug_info,
}
}

pub fn new_addr(expr: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::Addr(Box::new(expr)),
debug_info,
}
}

pub fn new_expr_sizeof(expr: Expr, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::SizeOf(SizeOfOperandKind::Expr(Box::new(expr))),
debug_info,
}
}

pub fn new_type_sizeof(type_name: TypeName, debug_info: DebugInfo) -> Self {
Self {
kind: ExprKind::SizeOf(SizeOfOperandKind::Type(type_name)),
debug_info,
}
}

#[cfg(test)]
pub fn kind_eq(&self, lhs: &Expr) -> bool {
lhs == self
}
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub struct Binary {
pub kind: BinOpKind,
pub lhs: Box<Expr>,
pub rhs: Box<Expr>,
}

impl Binary {
pub fn new(kind: BinOpKind, lhs: Box<Expr>, rhs: Box<Expr>) -> Self {
Self { kind, lhs, rhs }
}
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub enum BinOpKind {
/// The `+` operator (addition)
Add,
/// The `-` operator (subtraction)
Sub,
/// The `*` operator (multiplication)
Mul,
/// The `/` operator (division)
Div,
/// The `%` operator (remains)
Rem,
/// The `==` operator (equality)
Eq,
/// The `<=` operator (less than or equal to)
Le,
/// The `<` operator (less than)
Lt,
/// The `>=` operator (greater than or equal to)
Ge,
/// The `>` operator (greater than)
Gt,
/// The `!=` operator (Not equal to)
Ne,
/// The `<<` operator
LShift,
/// The `>>` operator
RShift,
/// The `&` operator (bit wise and)
BitWiseAnd,
/// The `||` operator (logical or)
LogicalOr,
/// The `&&` operator (logical and)
LogicalAnd,
}
Loading

0 comments on commit 317668a

Please sign in to comment.