#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Mod,
Pow,
Eq,
Ne,
Lt,
Gt,
Le,
Ge,
Cmp, CaseEq, Match, NMatch, And, Or, BitAnd,
BitOr,
BitXor,
Shl,
Shr,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnOp {
Neg,
Not,
BitNot,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VarKind {
Local, Instance, Class, Global, Const, }
#[derive(Debug, Clone, PartialEq, Default)]
pub struct BlockArity {
pub req: u16,
pub opt: u16,
pub kwnames: Vec<String>,
pub kwreq: Vec<String>,
pub kwsplat: Option<String>,
pub blockparam: Option<String>,
pub locals: Vec<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Block {
pub params: Vec<String>,
pub splat: Option<usize>,
pub body: Vec<Stmt>,
pub arity: Option<BlockArity>,
pub line: u32,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Nil,
True,
False,
Int(i64),
Float(f64),
Str(Vec<StrPart>),
Symbol(String),
Array(Vec<Expr>),
Hash(Vec<(Expr, Expr)>),
KwArgs(Box<Expr>),
Range {
lo: Option<Box<Expr>>,
hi: Option<Box<Expr>>,
exclusive: bool,
},
Var(VarKind, String),
Assign(Box<Expr>, Box<Expr>),
MultiAssign {
targets: Vec<Expr>,
values: Vec<Expr>,
},
Unary(UnOp, Box<Expr>),
Binary(BinOp, Box<Expr>, Box<Expr>),
If {
cond: Box<Expr>,
then: Vec<Stmt>,
elifs: Vec<(Expr, Vec<Stmt>)>,
els: Option<Vec<Stmt>>,
},
While {
cond: Box<Expr>,
body: Vec<Stmt>,
},
DoWhile {
cond: Box<Expr>,
body: Vec<Stmt>,
},
For {
var: String,
iter: Box<Expr>,
body: Vec<Stmt>,
},
Case {
subject: Box<Expr>,
whens: Vec<(Vec<Expr>, Vec<Stmt>)>,
els: Option<Vec<Stmt>>,
},
CaseIn {
subject: Box<Expr>,
clauses: Vec<InClause>,
els: Option<Vec<Stmt>>,
},
Call {
recv: Option<Box<Expr>>,
name: String,
args: Vec<Expr>,
block: Option<Block>,
},
Index(Box<Expr>, Vec<Expr>),
BlockPass(Box<Expr>),
Def {
name: String,
params: Vec<Param>,
body: Vec<Stmt>,
singleton: bool,
singleton_recv: Option<Box<Expr>>,
},
Class {
name: String,
superclass: Option<String>,
body: Vec<Stmt>,
},
Module {
name: String,
body: Vec<Stmt>,
},
SelfExpr,
Begin {
body: Vec<Stmt>,
rescues: Vec<Rescue>,
ensure: Option<Vec<Stmt>>,
},
Return(Option<Box<Expr>>),
Break(Option<Box<Expr>>),
Next(Option<Box<Expr>>),
Retry,
Redo,
Yield(Vec<Expr>),
Defined(Box<Expr>),
Super {
args: Option<Vec<Expr>>,
block: Option<Block>,
},
Splat(Box<Expr>),
Lambda(Block),
Regex(String, String),
SingletonClass {
recv: Option<Box<Expr>>,
body: Vec<Stmt>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct InClause {
pub pattern: Pattern,
pub guard: Option<Expr>,
pub body: Vec<Stmt>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Pattern {
Value(Expr),
Pin(Expr),
Bind(String),
Splat(Option<String>),
Array(Vec<Pattern>),
Hash(Vec<(String, Option<Pattern>)>, HashRest),
Const(String, Option<Box<Pattern>>),
As(Box<Pattern>, String),
Or(Vec<Pattern>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum HashRest {
None,
Splat(Option<String>),
Nil,
}
#[derive(Debug, Clone, PartialEq)]
pub enum StrPart {
Lit(String),
Interp(Box<Expr>),
Bytes(Vec<u8>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Param {
pub name: String,
pub default: Option<Expr>,
pub splat: bool,
pub keyword: bool,
pub kwsplat: bool,
pub block: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Rescue {
pub classes: Vec<String>,
pub splat: Option<Expr>,
pub body: Vec<Stmt>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Stmt {
pub expr: Expr,
pub line: u32,
}
impl From<Expr> for Stmt {
fn from(expr: Expr) -> Stmt {
Stmt { expr, line: 0 }
}
}