#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Pos {
pub line: u32,
pub col: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum UnOp {
Neg,
Not,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BinOp {
Add,
Sub,
Mul,
Div,
Rem,
Lt,
Le,
Gt,
Ge,
Eq,
Ne,
And,
Or,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum StrPart {
Text(String, String),
Interp(Expr),
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Expr {
Int(i64, Pos),
Float(f64, Pos),
Bool(bool, Pos),
Str(Vec<StrPart>, Pos),
Name(String, Pos),
List(Vec<Expr>, Pos),
Map(Vec<(Expr, Expr)>, Pos),
Some(Box<Expr>, Pos),
None(Pos),
Index(Box<Expr>, Box<Expr>, Pos),
Field(Box<Expr>, String, Pos),
Method(Box<Expr>, String, Vec<Expr>, Pos),
StructLit(TypeRef, Vec<FieldInit>, Pos),
Qualified(String, String, Pos),
Unary(UnOp, Box<Expr>, Pos),
Binary(BinOp, Box<Expr>, Box<Expr>, Pos),
Call(String, Vec<Expr>, Pos),
QueryCall(String, String, Vec<Expr>, Pos),
LabelCall(LabelCall),
Command(CommandCall),
MisplacedCommand(CommandCall),
Named(String, Box<Expr>, Pos),
Default(crate::value::Value, Pos),
Lambda(Box<Lambda>),
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Lambda {
pub params: Vec<LambdaParam>,
pub body: Expr,
pub pos: Pos,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct LambdaParam {
pub name: String,
pub pos: Pos,
pub ty: Option<TyName>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TypeRef {
pub module: Option<(String, Pos)>,
pub name: String,
pub pos: Pos,
}
impl TypeRef {
pub fn written(&self) -> String {
match &self.module {
Some((m, _)) => format!("{m}.{}", self.name),
None => self.name.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct FieldInit {
pub name: String,
pub pos: Pos,
pub value: Expr,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct LabelRef {
pub module: Option<(String, Pos)>,
pub name: String,
}
impl LabelRef {
pub fn written(&self) -> String {
match &self.module {
Some((m, _)) => format!("{m}.{}", self.name),
None => self.name.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct LabelCall {
pub target: LabelRef,
pub args: Vec<Expr>,
pub pos: Pos,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct CommandCall {
pub name: String,
pub args: Vec<Expr>,
pub id: Option<StableId>,
pub id_at: Pos,
pub pos: Pos,
}
impl Expr {
pub fn pos(&self) -> Pos {
match self {
Expr::Int(_, p)
| Expr::Float(_, p)
| Expr::Call(_, _, p)
| Expr::QueryCall(_, _, _, p)
| Expr::Bool(_, p)
| Expr::Str(_, p)
| Expr::Name(_, p)
| Expr::List(_, p)
| Expr::Map(_, p)
| Expr::Some(_, p)
| Expr::None(p)
| Expr::Index(_, _, p)
| Expr::Field(_, _, p)
| Expr::Method(_, _, _, p)
| Expr::StructLit(_, _, p)
| Expr::Qualified(_, _, p)
| Expr::Unary(_, _, p)
| Expr::Binary(_, _, _, p)
| Expr::Named(_, _, p)
| Expr::Default(_, p) => *p,
Expr::LabelCall(c) => c.pos,
Expr::Command(c) | Expr::MisplacedCommand(c) => c.pos,
Expr::Lambda(l) => l.pos,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AssignOp {
Set,
Add,
Sub,
Mul,
Div,
Rem,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TyName {
pub name: String,
pub args: Vec<TyName>,
pub pos: Pos,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum PathElem {
Field(String, Pos),
Index(Expr, Pos),
}
pub(crate) const STATEMENT_METHODS: &[&str] = &["push", "insert", "remove", "clear", "shuffle"];
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Pattern {
Wild(Pos),
Int(i64, Pos),
Str(String, Pos),
Bool(bool, Pos),
None(Pos),
Some(Option<(String, Pos)>, Pos),
Variant {
name: String,
binds: Vec<Option<(String, Pos)>>,
pos: Pos,
},
}
impl Pattern {
pub fn pos(&self) -> Pos {
match self {
Pattern::Wild(p)
| Pattern::Int(_, p)
| Pattern::Str(_, p)
| Pattern::Bool(_, p)
| Pattern::None(p)
| Pattern::Some(_, p) => *p,
Pattern::Variant { pos, .. } => *pos,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct MatchArm {
pub pattern: Pattern,
pub body: Vec<Stmt>,
pub pos: Pos,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct StableId {
pub id: String,
pub pos: Pos,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Stmt {
Let {
name: String,
name_pos: Pos,
ty: Option<TyName>,
init: Expr,
pos: Pos,
},
Assign {
module: Option<(String, Pos)>,
name: String,
path: Vec<PathElem>,
op: AssignOp,
value: Expr,
pos: Pos,
},
If {
arms: Vec<(Expr, Vec<Stmt>)>,
else_body: Vec<Stmt>,
pos: Pos,
},
While {
cond: Expr,
body: Vec<Stmt>,
pos: Pos,
},
For {
key: Option<(String, Pos)>,
var: String,
var_pos: Pos,
iter: Expr,
body: Vec<Stmt>,
pos: Pos,
},
ListMethod {
module: Option<(String, Pos)>,
name: String,
path: Vec<PathElem>,
method: String,
method_pos: Pos,
args: Vec<Expr>,
pos: Pos,
},
Match {
scrutinee: Expr,
arms: Vec<MatchArm>,
pos: Pos,
},
Break(Pos),
Continue(Pos),
Jump {
target: LabelRef,
pos: Pos,
},
Call(LabelCall),
Return {
value: Option<Expr>,
pos: Pos,
},
End(Pos),
Command(CommandCall),
Plain {
parts: Vec<StrPart>,
raw: String,
id: Option<StableId>,
id_at: Pos,
typo: Option<(String, Pos)>,
pos: Pos,
},
Quote {
identity: Option<IdentRef>,
parts: Vec<StrPart>,
id: Option<StableId>,
id_at: Pos,
pos: Pos,
},
Choice {
options: Vec<ChoiceOpt>,
pos: Pos,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct IdentRef {
pub module: Option<(String, Pos)>,
pub name: String,
pub pos: Pos,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ChoiceOpt {
pub parts: Vec<StrPart>,
pub once: Option<Pos>,
pub cond: Option<Expr>,
pub id: Option<StableId>,
pub id_at: Pos,
pub body: Vec<Stmt>,
pub pos: Pos,
}
impl ChoiceOpt {
pub fn empty(pos: Pos) -> Self {
Self {
parts: Vec::new(),
once: None,
cond: None,
id: None,
id_at: pos,
body: Vec::new(),
pos,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct VarDecl {
pub name: String,
pub ty: TyName,
pub init: Expr,
pub persistent: bool,
pub pos: Pos,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Param {
pub name: String,
pub pos: Pos,
pub ty: TyName,
pub default: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct LabelDecl {
pub name: String,
pub params: Vec<Param>,
pub ret: Option<TyName>,
pub body: Vec<Stmt>,
pub pos: Pos,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct TypeDeclAst {
pub name: String,
pub pos: Pos,
pub kind: TypeDeclKind,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum TypeDeclKind {
Struct(Vec<(String, Pos, TyName)>),
Enum(Vec<VariantAst>),
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct VariantAst {
pub name: String,
pub pos: Pos,
pub args: Vec<TyName>,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Import {
pub path: String,
pub local: String,
pub aliased: bool,
pub pos: Pos,
}
impl Import {
pub fn shown(&self) -> String {
if self.aliased {
format!("{} as {}", self.path, self.local)
} else {
self.path.clone()
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct IdentityDecl {
pub name: String,
pub display: String,
pub pos: Pos,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub(crate) struct Module {
pub imports: Vec<Import>,
pub identities: Vec<IdentityDecl>,
pub vars: Vec<VarDecl>,
pub labels: Vec<LabelDecl>,
pub fns: Vec<LabelDecl>,
pub types: Vec<TypeDeclAst>,
pub poisoned: std::collections::HashSet<String>,
pub repl_order: Vec<(u8, usize)>,
}