62 lines
1.2 KiB
Rust
62 lines
1.2 KiB
Rust
use std::rc::Rc;
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
pub struct PrefixOp {
|
|
sigil: Rc<String>,
|
|
}
|
|
|
|
impl PrefixOp {
|
|
pub fn from_sigil(sigil: &str) -> PrefixOp {
|
|
PrefixOp { sigil: Rc::new(sigil.to_string()) }
|
|
}
|
|
|
|
pub fn sigil(&self) -> &str {
|
|
&self.sigil
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
pub struct BinOp {
|
|
sigil: Rc<String>,
|
|
}
|
|
|
|
impl BinOp {
|
|
pub fn from_sigil(sigil: &str) -> BinOp {
|
|
BinOp { sigil: Rc::new(sigil.to_string()) }
|
|
}
|
|
|
|
pub fn sigil(&self) -> &str {
|
|
&self.sigil
|
|
}
|
|
|
|
pub fn min_precedence() -> i32 {
|
|
i32::min_value()
|
|
}
|
|
pub fn get_precedence(&self) -> i32 {
|
|
binop_precedences(self.sigil.as_ref())
|
|
}
|
|
}
|
|
|
|
fn binop_precedences(s: &str) -> i32 {
|
|
let default = 10_000_000;
|
|
match s {
|
|
"+" => 10,
|
|
"-" => 10,
|
|
"*" => 20,
|
|
"/" => 20,
|
|
"%" => 20,
|
|
"++" => 30,
|
|
"^" => 30,
|
|
"&" => 20,
|
|
"|" => 20,
|
|
">" => 20,
|
|
">=" => 20,
|
|
"<" => 20,
|
|
"<=" => 20,
|
|
"==" => 40,
|
|
"<=>" => 30,
|
|
"=" => 5, // Assignment shoudl have highest precedence
|
|
_ => default,
|
|
}
|
|
}
|