use std::collections::HashSet;
#[derive(Debug, Clone, PartialEq)]
pub enum BinOp {
Add, Subtract, Multiply, Divide, Modulo,
Equal, NotEqual, Less, LessEqual, Greater, GreaterEqual,
And, Or,
BitAnd, BitOr, BitXor, Shl, Shr, }
#[derive(Debug, Clone, PartialEq)]
pub enum UnaryOp {
Minus, Not, }
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Number(f64),
BigInteger(num_bigint::BigInt), String(String),
Boolean(bool),
Null,
Identifier(String),
Binary {
left: Box<Expr>,
op: BinOp,
right: Box<Expr>,
},
Unary {
op: UnaryOp,
expr: Box<Expr>,
},
Call {
func: Box<Expr>,
args: Vec<Expr>,
},
Array(Vec<Expr>),
Dict(Vec<(String, Expr)>),
Index {
object: Box<Expr>,
index: Box<Expr>,
},
If {
condition: Box<Expr>,
then_branch: Vec<Located>,
elif_branches: Vec<(Expr, Vec<Located>)>, else_branch: Option<Vec<Located>>,
},
Lambda {
params: Vec<String>,
body: Vec<Located>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum Stmt {
Set {
name: String,
value: Expr,
},
SetIndex {
object: Box<Expr>,
index: Box<Expr>,
value: Expr,
},
FuncDef {
name: String,
params: Vec<String>,
body: Vec<Located>,
},
GeneratorDef {
name: String,
params: Vec<String>,
body: Vec<Located>,
},
LazyDef {
name: String,
expr: Expr,
},
Return(Expr),
Yield(Expr),
Break,
Continue,
While {
condition: Expr,
body: Vec<Located>,
},
For {
var: String,
iterable: Expr,
body: Vec<Located>,
},
ForIndexed {
index_var: String,
value_var: String,
iterable: Expr,
body: Vec<Located>,
},
Switch {
expr: Expr,
cases: Vec<(Expr, Vec<Located>)>,
default: Option<Vec<Located>>,
},
Import {
names: Vec<String>,
path: String,
aliases: Vec<Option<String>>, namespace: Option<String>, },
Export(String),
Throw(Expr),
Expression(Expr),
}
#[derive(Debug, Clone)]
pub struct Located {
pub line: usize,
pub stmt: Stmt,
}
impl PartialEq for Located {
fn eq(&self, other: &Self) -> bool {
self.stmt == other.stmt
}
}
impl Eq for Located {}
pub fn located(line: usize, stmt: Stmt) -> Located {
Located { line, stmt }
}
pub type Program = Vec<Located>;
pub fn collect_breakable_lines(program: &Program) -> HashSet<usize> {
let mut lines = HashSet::new();
for l in program {
lines.insert(l.line);
collect_stmt_lines(&l.stmt, &mut lines);
}
lines
}
fn collect_stmt_lines(stmt: &Stmt, lines: &mut HashSet<usize>) {
let body_of = |body: &Vec<Located>, lines: &mut HashSet<usize>| {
for l in body {
lines.insert(l.line);
collect_stmt_lines(&l.stmt, lines);
}
};
match stmt {
Stmt::FuncDef { body, .. } | Stmt::GeneratorDef { body, .. } => body_of(body, lines),
Stmt::While { body, .. } | Stmt::For { body, .. } | Stmt::ForIndexed { body, .. } => {
body_of(body, lines)
}
Stmt::Switch { cases, default, .. } => {
for (_, body) in cases {
body_of(body, lines);
}
if let Some(body) = default {
body_of(body, lines);
}
}
Stmt::Expression(Expr::If {
then_branch,
elif_branches,
else_branch,
..
}) => {
body_of(then_branch, lines);
for (_, body) in elif_branches {
body_of(body, lines);
}
if let Some(body) = else_branch {
body_of(body, lines);
}
}
_ => {}
}
}
impl Expr {
pub fn binary(left: Expr, op: BinOp, right: Expr) -> Self {
Expr::Binary {
left: Box::new(left),
op,
right: Box::new(right),
}
}
pub fn unary(op: UnaryOp, expr: Expr) -> Self {
Expr::Unary {
op,
expr: Box::new(expr),
}
}
pub fn call(func: Expr, args: Vec<Expr>) -> Self {
Expr::Call {
func: Box::new(func),
args,
}
}
pub fn index(object: Expr, index: Expr) -> Self {
Expr::Index {
object: Box::new(object),
index: Box::new(index),
}
}
}
impl std::fmt::Display for BinOp {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
BinOp::Add => write!(f, "+"),
BinOp::Subtract => write!(f, "-"),
BinOp::Multiply => write!(f, "*"),
BinOp::Divide => write!(f, "/"),
BinOp::Modulo => write!(f, "%"),
BinOp::BitAnd => write!(f, "&"),
BinOp::BitOr => write!(f, "|"),
BinOp::BitXor => write!(f, "^"),
BinOp::Shl => write!(f, "<<"),
BinOp::Shr => write!(f, ">>"),
BinOp::Equal => write!(f, "=="),
BinOp::NotEqual => write!(f, "!="),
BinOp::Less => write!(f, "<"),
BinOp::LessEqual => write!(f, "<="),
BinOp::Greater => write!(f, ">"),
BinOp::GreaterEqual => write!(f, ">="),
BinOp::And => write!(f, "&&"),
BinOp::Or => write!(f, "||"),
}
}
}
impl std::fmt::Display for UnaryOp {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
UnaryOp::Minus => write!(f, "-"),
UnaryOp::Not => write!(f, "!"),
}
}
}