#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Pow,
Lt,
Gt,
Le,
Ge,
Eq,
Ne,
And,
Or,
And2,
Or2,
Colon,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UnOp {
Neg,
Plus,
Not,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexKind {
Single,
Double,
Dollar,
At,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Arg {
pub name: Option<String>,
pub value: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Param {
pub name: String,
pub default: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Num(f64),
Int(i64),
Str(String),
Bool(bool),
Null,
Na(NaKind),
Inf,
NaN,
Ident(String),
Dots,
Call {
fun: Box<Expr>,
args: Vec<Arg>,
},
Function {
params: Vec<Param>,
body: Box<Expr>,
},
If {
cond: Box<Expr>,
then: Box<Expr>,
els: Option<Box<Expr>>,
},
For {
var: String,
seq: Box<Expr>,
body: Box<Expr>,
},
While {
cond: Box<Expr>,
body: Box<Expr>,
},
Repeat(Box<Expr>),
Block(Vec<Expr>),
Paren(Box<Expr>),
Assign {
target: Box<Expr>,
value: Box<Expr>,
super_assign: bool,
},
Formula {
lhs: Option<Box<Expr>>,
rhs: Box<Expr>,
},
Binary {
op: BinOp,
lhs: Box<Expr>,
rhs: Box<Expr>,
},
Special {
name: String,
lhs: Box<Expr>,
rhs: Box<Expr>,
},
Unary {
op: UnOp,
operand: Box<Expr>,
},
Index {
kind: IndexKind,
obj: Box<Expr>,
args: Vec<Arg>,
},
Break,
Next,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NaKind {
Logical,
Integer,
Real,
Character,
}
pub fn call_syntax(fun: Expr, args: Vec<Arg>) -> Expr {
let Expr::Ident(op) = &fun else {
return Expr::Call {
fun: Box::new(fun),
args,
};
};
let plain: Option<Vec<Expr>> = match args.iter().all(|a| a.name.is_none()) {
true => args.iter().map(|a| a.value.clone()).collect(),
false => None,
};
let b = Box::new;
let binop = match op.as_str() {
"+" => Some(BinOp::Add),
"-" => Some(BinOp::Sub),
"*" => Some(BinOp::Mul),
"/" => Some(BinOp::Div),
"^" => Some(BinOp::Pow),
"<" => Some(BinOp::Lt),
">" => Some(BinOp::Gt),
"<=" => Some(BinOp::Le),
">=" => Some(BinOp::Ge),
"==" => Some(BinOp::Eq),
"!=" => Some(BinOp::Ne),
"&" => Some(BinOp::And),
"|" => Some(BinOp::Or),
"&&" => Some(BinOp::And2),
"||" => Some(BinOp::Or2),
":" => Some(BinOp::Colon),
_ => None,
};
let built = match (op.as_str(), plain) {
(_, Some(v)) if binop.is_some() && v.len() == 2 => binop.map(|op| Expr::Binary {
op,
lhs: b(v[0].clone()),
rhs: b(v[1].clone()),
}),
("-" | "+" | "!", Some(v)) if v.len() == 1 => {
let op = match op.as_str() {
"-" => UnOp::Neg,
"+" => UnOp::Plus,
_ => UnOp::Not,
};
Some(Expr::Unary {
op,
operand: b(v[0].clone()),
})
}
(s, Some(v)) if s.len() >= 2 && s.starts_with('%') && s.ends_with('%') && v.len() == 2 => {
let name = match s {
"%%" => String::new(),
"%/%" => "/".to_string(),
other => other[1..other.len() - 1].to_string(),
};
Some(Expr::Special {
name,
lhs: b(v[0].clone()),
rhs: b(v[1].clone()),
})
}
("(", Some(v)) if v.len() == 1 => Some(Expr::Paren(b(v[0].clone()))),
("{", Some(v)) => Some(Expr::Block(v)),
("if", Some(v)) if v.len() == 2 || v.len() == 3 => Some(Expr::If {
cond: b(v[0].clone()),
then: b(v[1].clone()),
els: v.get(2).cloned().map(b),
}),
("while", Some(v)) if v.len() == 2 => Some(Expr::While {
cond: b(v[0].clone()),
body: b(v[1].clone()),
}),
("repeat", Some(v)) if v.len() == 1 => Some(Expr::Repeat(b(v[0].clone()))),
("for", Some(v)) if v.len() == 3 => match &v[0] {
Expr::Ident(var) => Some(Expr::For {
var: var.clone(),
seq: b(v[1].clone()),
body: b(v[2].clone()),
}),
_ => None,
},
("<-" | "<<-", Some(v)) if v.len() == 2 => Some(Expr::Assign {
target: b(v[0].clone()),
value: b(v[1].clone()),
super_assign: op == "<<-",
}),
("$" | "@", Some(v)) if v.len() == 2 => match &v[1] {
Expr::Ident(n) | Expr::Str(n) => Some(Expr::Index {
kind: if op == "$" {
IndexKind::Dollar
} else {
IndexKind::At
},
obj: b(v[0].clone()),
args: vec![Arg {
name: None,
value: Some(Expr::Str(n.clone())),
}],
}),
_ => None,
},
_ => None,
};
if let Some(e) = built {
return e;
}
if matches!(op.as_str(), "[" | "[[") && args.first().is_some_and(|a| a.name.is_none()) {
if let Some(obj) = args[0].value.clone() {
return Expr::Index {
kind: if op == "[" {
IndexKind::Single
} else {
IndexKind::Double
},
obj: b(obj),
args: args[1..].to_vec(),
};
}
}
Expr::Call { fun: b(fun), args }
}