schala/schala-lang/src/ast/operators.rs

62 lines
1.2 KiB
Rust
Raw Normal View History

use std::rc::Rc;
#[derive(Debug, PartialEq, Clone)]
pub struct PrefixOp {
2021-10-28 02:00:37 -07:00
sigil: Rc<String>,
}
impl PrefixOp {
2021-10-28 02:00:37 -07:00
pub fn from_sigil(sigil: &str) -> PrefixOp {
PrefixOp { sigil: Rc::new(sigil.to_string()) }
}
2021-10-28 02:00:37 -07:00
pub fn sigil(&self) -> &str {
&self.sigil
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct BinOp {
2021-10-28 02:00:37 -07:00
sigil: Rc<String>,
}
impl BinOp {
2021-10-28 02:00:37 -07:00
pub fn from_sigil(sigil: &str) -> BinOp {
BinOp { sigil: Rc::new(sigil.to_string()) }
}
2021-10-28 02:00:37 -07:00
pub fn sigil(&self) -> &str {
&self.sigil
}
2021-10-28 02:00:37 -07:00
pub fn min_precedence() -> i32 {
i32::min_value()
}
2021-11-03 23:57:22 -07:00
pub fn get_precedence(&self) -> i32 {
binop_precedences(self.sigil.as_ref())
}
}
fn binop_precedences(s: &str) -> i32 {
2021-10-28 02:00:37 -07:00
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,
}
}