#[derive(Debug, Clone, PartialEq)]
pub struct Stmt {
pub kind: StmtKind,
pub line: usize,
}
impl Stmt {
pub fn new(kind: StmtKind, line: usize) -> Stmt {
Stmt { kind, line }
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Pattern {
Name(String),
Array(Vec<Pattern>),
}
impl Pattern {
pub fn names(&self) -> Vec<&str> {
let mut found = Vec::new();
self.collect_names(&mut found);
found
}
fn collect_names<'a>(&'a self, found: &mut Vec<&'a str>) {
match self {
Pattern::Name(name) => found.push(name),
Pattern::Array(items) => {
for item in items {
item.collect_names(found);
}
}
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Param {
pub name: String,
pub default: Option<Expr>,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Params {
pub named: Vec<Param>,
pub rest: Option<String>,
}
impl Params {
pub fn required(&self) -> usize {
self.named.iter().filter(|p| p.default.is_none()).count()
}
pub fn names(&self) -> impl Iterator<Item = &str> {
self.named
.iter()
.map(|p| p.name.as_str())
.chain(self.rest.as_deref())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct MatchArm<B> {
pub cases: Vec<Expr>,
pub guard: Option<Expr>,
pub body: B,
}
#[derive(Debug, Clone, PartialEq)]
pub enum StmtKind {
Import {
spec: String,
alias: String,
column: usize,
},
Let { pattern: Pattern, value: Expr },
Assign { target: Expr, value: Expr },
CompoundAssign {
target: Expr,
op: BinaryOp,
value: Expr,
},
Expr(Expr),
Return(Option<Expr>),
Break,
Continue,
If {
condition: Expr,
then_branch: Vec<Stmt>,
else_branch: Option<Vec<Stmt>>,
},
While { condition: Expr, body: Vec<Stmt> },
For {
name: Pattern,
value: Option<Pattern>,
iterable: Expr,
body: Vec<Stmt>,
},
Function {
name: String,
params: Params,
body: Vec<Stmt>,
},
Match {
subject: Expr,
arms: Vec<MatchArm<Vec<Stmt>>>,
column: usize,
},
}
#[derive(Debug, Clone)]
pub struct Expr {
pub kind: ExprKind,
pub line: usize,
pub column: usize,
pub height: usize,
}
impl Expr {
pub fn new(kind: ExprKind, line: usize, column: usize) -> Expr {
let height = kind.height();
Expr {
kind,
line,
column,
height,
}
}
}
impl PartialEq for Expr {
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind
}
}
fn tallest(exprs: &[Expr]) -> usize {
exprs.iter().map(|expr| expr.height).max().unwrap_or(0)
}
impl ExprKind {
fn height(&self) -> usize {
let below = match self {
ExprKind::Int(_)
| ExprKind::Float(_)
| ExprKind::Str(_)
| ExprKind::Bool(_)
| ExprKind::Nil
| ExprKind::Identifier(_)
| ExprKind::FString(_)
| ExprKind::Function { .. } => 0,
ExprKind::Array(items) => tallest(items),
ExprKind::Map(pairs) => pairs
.iter()
.map(|(key, value)| key.height.max(value.height))
.max()
.unwrap_or(0),
ExprKind::Index { target, index } => target.height.max(index.height),
ExprKind::Field { target, .. } => target.height,
ExprKind::Unary { operand, .. } => operand.height,
ExprKind::Binary { left, right, .. } | ExprKind::Logical { left, right, .. } => {
left.height.max(right.height)
}
ExprKind::Call { callee, arguments } => callee.height.max(tallest(arguments)),
ExprKind::If {
condition,
then_value,
else_value,
} => condition.height.max(then_value.height).max(else_value.height),
ExprKind::Try(inner) => inner.height,
ExprKind::Match { subject, arms } => arms
.iter()
.map(|arm| {
tallest(&arm.cases)
.max(arm.guard.as_ref().map_or(0, |g| g.height))
.max(arm.body.height)
})
.max()
.unwrap_or(0)
.max(subject.height),
};
below + 1
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ExprKind {
FString(Vec<crate::token::FStringPart>),
Int(i64),
Float(f64),
Str(String),
Bool(bool),
Nil,
Identifier(String),
Array(Vec<Expr>),
Map(Vec<(Expr, Expr)>),
Index {
target: Box<Expr>,
index: Box<Expr>,
},
Field {
target: Box<Expr>,
name: String,
},
Unary {
op: UnaryOp,
operand: Box<Expr>,
},
Binary {
op: BinaryOp,
left: Box<Expr>,
right: Box<Expr>,
},
Logical {
op: LogicalOp,
left: Box<Expr>,
right: Box<Expr>,
},
Call {
callee: Box<Expr>,
arguments: Vec<Expr>,
},
If {
condition: Box<Expr>,
then_value: Box<Expr>,
else_value: Box<Expr>,
},
Match {
subject: Box<Expr>,
arms: Vec<MatchArm<Expr>>,
},
Try(Box<Expr>),
Function {
params: Params,
body: Vec<Stmt>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
Negate, Not, }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
Add,
Subtract,
Multiply,
Divide,
Modulo,
Equal,
NotEqual,
Less,
Greater,
LessEqual,
GreaterEqual,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogicalOp {
And,
Or,
}