From b0a1f3337cf48fad12375f1befcebbad6e8537fc Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 14 Aug 2019 10:31:07 -0700 Subject: [PATCH] Clean up some operator code --- schala-lang/language/src/builtin.rs | 40 ++++++++++++----------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/schala-lang/language/src/builtin.rs b/schala-lang/language/src/builtin.rs index 6093fc9..afca3f7 100644 --- a/schala-lang/language/src/builtin.rs +++ b/schala-lang/language/src/builtin.rs @@ -110,6 +110,20 @@ pub struct BinOp { pub builtin: Option, } +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 { pub fn from_sigil(sigil: &str) -> BinOp { let builtin = Builtin::from_str(sigil).ok(); @@ -119,35 +133,15 @@ impl BinOp { &self.sigil } pub fn from_sigil_token(tok: &TokenKind) -> Option { - use self::TokenKind::*; - let s = match tok { - Operator(op) => op, - Period => ".", - Pipe => "|", - Slash => "/", - LAngleBracket => "<", - RAngleBracket => ">", - Equals => "=", - _ => return None - }; + let s = token_kind_to_sigil(tok)?; Some(BinOp::from_sigil(s)) } pub fn min_precedence() -> i32 { i32::min_value() } - pub fn get_precedence_from_token(op: &TokenKind) -> Option { - use self::TokenKind::*; - let s = match op { - Operator(op) => op, - Period => ".", - Pipe => "|", - Slash => "/", - LAngleBracket => "<", - RAngleBracket => ">", - Equals => "=", - _ => return None - }; + pub fn get_precedence_from_token(op_tok: &TokenKind) -> Option { + let s = token_kind_to_sigil(op_tok)?; let default = 10_000_000; Some(BINOPS.get(s).map(|x| x.2.clone()).unwrap_or_else(|| { default