#[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 StmtKind {
Import {
spec: String,
alias: String,
column: usize,
},
Let { name: String, value: Expr },
Assign { target: Expr, 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: String,
iterable: Expr,
body: Vec<Stmt>,
},
Function {
name: String,
params: Vec<String>,
body: Vec<Stmt>,
},
}
#[derive(Debug, Clone)]
pub struct Expr {
pub kind: ExprKind,
pub line: usize,
pub column: usize,
}
impl Expr {
pub fn new(kind: ExprKind, line: usize, column: usize) -> Expr {
Expr { kind, line, column }
}
}
impl PartialEq for Expr {
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ExprKind {
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>,
},
Try(Box<Expr>),
Function {
params: Vec<String>,
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,
}