use crate::pool::{Idx, Pool};
use crate::program::{Action, ArithOp, PathStep, PlaceRef, StopInfo};
use crate::syntax::ast::{BinOp, UnOp};
use crate::value::Value;
pub(crate) type ExprId = Idx<Expr>;
pub(crate) type StmtId = Idx<Stmt>;
#[derive(Debug, Default)]
pub(crate) struct Ir {
pub exprs: Pool<Expr>,
pub stmts: Pool<Stmt>,
}
impl Ir {
pub(crate) fn expr(&mut self, kind: ExprKind, line: u32) -> ExprId {
self.exprs.push(Expr { kind, line })
}
pub(crate) fn stmt(&mut self, kind: StmtKind, line: u32) -> StmtId {
self.stmts.push(Stmt { kind, line })
}
}
#[derive(Debug)]
pub(crate) struct Body {
pub ir: Ir,
pub stmts: Vec<StmtId>,
pub tail: Tail,
pub in_fn: bool,
pub locals: u32,
pub line: u32,
}
#[derive(Debug)]
pub(crate) enum Tail {
Return,
End,
Repl(Option<ExprId>),
}
#[derive(Debug)]
pub(crate) struct Expr {
pub kind: ExprKind,
pub line: u32,
}
#[derive(Debug)]
pub(crate) enum ExprKind {
Error,
Const(Value),
Var(PlaceRef),
Field {
base: ExprId,
field: u32,
},
IdField {
base: ExprId,
name: bool,
},
Unary {
op: UnOp,
operand: ExprId,
},
Binary {
op: BinOp,
ty: Operand,
lhs: ExprId,
rhs: ExprId,
},
List(Vec<ExprId>),
Map(Vec<(ExprId, ExprId)>),
Some(ExprId),
Struct {
ty: u32,
order: Vec<u32>,
fields: Vec<ExprId>,
},
Enum {
ty: u32,
variant: u32,
args: Vec<ExprId>,
},
Index {
base: ExprId,
index: ExprId,
},
Method {
method: Method,
base: ExprId,
args: Vec<ExprId>,
},
Builtin {
builtin: Builtin,
args: Vec<ExprId>,
},
Text {
parts: Vec<TextPiece>,
record: bool,
},
CallFn {
f: u32,
args: Vec<ExprId>,
},
CallValue {
callee: ExprId,
args: Vec<ExprId>,
},
MakeFn {
f: u32,
captures: Vec<ExprId>,
},
CallLabel {
label: u32,
args: Vec<ExprId>,
hash: u64,
},
CallQuery {
native: u32,
args: Vec<ExprId>,
},
CallEffect {
native: u32,
args: Vec<ExprId>,
},
Stop(Box<Stop>),
}
#[derive(Debug)]
pub(crate) struct Stop {
pub cmd: u32,
pub mark_seen: Option<String>,
pub args: Vec<StopArg>,
pub info: StopInfo,
}
#[derive(Debug)]
pub(crate) struct StopArg {
pub expr: ExprId,
pub text: bool,
}
#[derive(Debug)]
pub(crate) struct Stmt {
pub kind: StmtKind,
pub line: u32,
}
#[derive(Debug)]
pub(crate) enum StmtKind {
Eval(ExprId),
Store {
place: PlaceRef,
value: ExprId,
},
Compound {
place: PlaceRef,
op: ArithOp,
ty: Operand,
value: ExprId,
},
Mutate {
place: PlaceRef,
path: Vec<PathStep>,
indices: Vec<ExprId>,
operands: Vec<ExprId>,
action: Action,
},
If {
arms: Vec<IfArm>,
else_body: Vec<StmtId>,
},
While {
cond: ExprId,
body: Vec<StmtId>,
},
For {
iter: ExprId,
list: u32,
idx: u32,
var: u32,
key: Option<u32>,
kind: ForKind,
body: Vec<StmtId>,
},
Match {
scrutinee: ExprId,
arms: Vec<MatchArm>,
},
Choice(Box<Choice>),
Break,
Continue,
JumpLabel(u32),
End,
Return(Option<ExprId>),
}
#[derive(Debug)]
pub(crate) struct IfArm {
pub cond: ExprId,
pub body: Vec<StmtId>,
pub line: u32,
}
#[derive(Debug)]
pub(crate) struct MatchArm {
pub test: Option<(PatTest, u32)>,
pub binds: Vec<(u32, u32, u32)>,
pub body: Vec<StmtId>,
pub line: u32,
}
#[derive(Debug)]
pub(crate) enum PatTest {
Lit(Value),
Some,
None,
Variant(String),
}
#[derive(Debug)]
pub(crate) struct Choice {
pub cmd: u32,
pub options: Vec<ChoiceOpt>,
pub info: StopInfo,
}
#[derive(Debug)]
pub(crate) struct ChoiceOpt {
pub once: Option<String>,
pub cond: Option<ExprId>,
pub text: ExprId,
pub id: Value,
pub body: Vec<StmtId>,
pub line: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Operand {
Int,
Float,
Other,
}
impl Operand {
pub(crate) fn of(ty: Option<&crate::value::Ty>) -> Self {
match ty {
Some(crate::value::Ty::Int) => Operand::Int,
Some(crate::value::Ty::Float) => Operand::Float,
_ => Operand::Other,
}
}
}
#[derive(Debug)]
pub(crate) enum TextPiece {
Lit(String),
Interp {
expr: ExprId,
to_str: bool,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Method {
Len,
IsEmpty,
ListContains,
ListSort,
ListReverse,
ListSum,
ListSumFloat,
ListSortBy,
ListMinBy,
ListMaxBy,
MapHas,
MapGet,
MapKeys,
MapValues,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ForKind {
List,
Map,
Range { end: ExprId },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Builtin {
ToInt,
ToFloat,
Seen,
Visited,
Random,
RandomFloat,
Range,
}