Clean up some operator code

This commit is contained in:
greg 2019-08-14 10:31:07 -07:00
parent 2e147e141e
commit b0a1f3337c

View File

@ -110,6 +110,20 @@ pub struct BinOp {
pub builtin: Option<Builtin>, pub builtin: Option<Builtin>,
} }
fn token_kind_to_sigil<'a>(tok: &'a TokenKind) -> Option<&'a str> {
use self::TokenKind::*;
Some(match tok {
Operator(op) => op.as_str(),
Period => ".",
Pipe => "|",
Slash => "/",
LAngleBracket => "<",
RAngleBracket => ">",
Equals => "=",
_ => return None
})
}
impl BinOp { impl BinOp {
pub fn from_sigil(sigil: &str) -> BinOp { pub fn from_sigil(sigil: &str) -> BinOp {
let builtin = Builtin::from_str(sigil).ok(); let builtin = Builtin::from_str(sigil).ok();
@ -119,35 +133,15 @@ impl BinOp {
&self.sigil &self.sigil
} }
pub fn from_sigil_token(tok: &TokenKind) -> Option<BinOp> { pub fn from_sigil_token(tok: &TokenKind) -> Option<BinOp> {
use self::TokenKind::*; let s = token_kind_to_sigil(tok)?;
let s = match tok {
Operator(op) => op,
Period => ".",
Pipe => "|",
Slash => "/",
LAngleBracket => "<",
RAngleBracket => ">",
Equals => "=",
_ => return None
};
Some(BinOp::from_sigil(s)) Some(BinOp::from_sigil(s))
} }
pub fn min_precedence() -> i32 { pub fn min_precedence() -> i32 {
i32::min_value() i32::min_value()
} }
pub fn get_precedence_from_token(op: &TokenKind) -> Option<i32> { pub fn get_precedence_from_token(op_tok: &TokenKind) -> Option<i32> {
use self::TokenKind::*; let s = token_kind_to_sigil(op_tok)?;
let s = match op {
Operator(op) => op,
Period => ".",
Pipe => "|",
Slash => "/",
LAngleBracket => "<",
RAngleBracket => ">",
Equals => "=",
_ => return None
};
let default = 10_000_000; let default = 10_000_000;
Some(BINOPS.get(s).map(|x| x.2.clone()).unwrap_or_else(|| { Some(BINOPS.get(s).map(|x| x.2.clone()).unwrap_or_else(|| {
default default