use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)]
pub struct Program {
pub rules: Vec<Rule>,
pub funcs: HashMap<String, FunctionDef>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionDef {
pub name: String,
pub params: Vec<String>,
pub body: Vec<Stmt>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Rule {
pub pattern: Pattern,
pub stmts: Vec<Stmt>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Pattern {
Begin,
End,
BeginFile,
EndFile,
Expr(Expr),
Regexp(String),
Range(Box<Pattern>, Box<Pattern>),
Empty,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Stmt {
If {
cond: Expr,
then_: Vec<Stmt>,
else_: Vec<Stmt>,
},
While {
cond: Expr,
body: Vec<Stmt>,
},
DoWhile {
body: Vec<Stmt>,
cond: Expr,
},
ForC {
init: Option<Expr>,
cond: Option<Expr>,
iter: Option<Expr>,
body: Vec<Stmt>,
},
ForIn {
var: String,
arr: String,
body: Vec<Stmt>,
},
Block(Vec<Stmt>),
Expr(Expr),
Print {
args: Vec<Expr>,
redir: Option<PrintRedir>,
},
Printf {
args: Vec<Expr>,
redir: Option<PrintRedir>,
},
Break,
Continue,
Next,
NextFile,
Exit(Option<Expr>),
Delete {
name: String,
indices: Option<Vec<Expr>>,
},
Return(Option<Expr>),
GetLine {
var: Option<String>,
redir: GetlineRedir,
},
Switch {
expr: Expr,
arms: Vec<SwitchArm>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum SwitchArm {
Case {
label: SwitchLabel,
stmts: Vec<Stmt>,
},
Default {
stmts: Vec<Stmt>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum SwitchLabel {
Expr(Expr),
Regexp(String),
}
#[derive(Debug, Clone, PartialEq)]
pub enum PrintRedir {
Overwrite(Box<Expr>),
Append(Box<Expr>),
Pipe(Box<Expr>),
Coproc(Box<Expr>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum GetlineRedir {
Primary,
File(Box<Expr>),
Coproc(Box<Expr>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Number(f64),
Str(String),
Var(String),
Field(Box<Expr>),
Index {
name: String,
indices: Vec<Expr>,
},
Binary {
op: BinOp,
left: Box<Expr>,
right: Box<Expr>,
},
Unary {
op: UnaryOp,
expr: Box<Expr>,
},
Assign {
name: String,
op: Option<BinOp>,
rhs: Box<Expr>,
},
AssignField {
field: Box<Expr>,
op: Option<BinOp>,
rhs: Box<Expr>,
},
AssignIndex {
name: String,
indices: Vec<Expr>,
op: Option<BinOp>,
rhs: Box<Expr>,
},
Call {
name: String,
args: Vec<Expr>,
},
Ternary {
cond: Box<Expr>,
then_: Box<Expr>,
else_: Box<Expr>,
},
In {
key: Box<Expr>,
arr: String,
},
IncDec {
op: IncDecOp,
target: IncDecTarget,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IncDecOp {
PreInc,
PostInc,
PreDec,
PostDec,
}
#[derive(Debug, Clone, PartialEq)]
pub enum IncDecTarget {
Var(String),
Field(Box<Expr>),
Index { name: String, indices: Vec<Expr> },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Mod,
Eq,
Ne,
Lt,
Le,
Gt,
Ge,
Match,
NotMatch,
Concat,
And,
Or,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
Neg,
Pos,
Not,
}
pub mod parallel;