Fixed tests w/ respect to binop

There's a few unnecessary conversions of &str 's to Rc<String> and back
This commit is contained in:
greg 2018-02-24 17:50:57 -08:00
parent a396c448ec
commit 6e105bac55
2 changed files with 8 additions and 11 deletions

View File

@ -10,8 +10,8 @@ pub struct BinOp {
} }
impl BinOp { impl BinOp {
pub fn from_sigil(sigil: Rc<String>) -> BinOp { pub fn from_sigil(sigil: &str) -> BinOp {
BinOp { sigil } BinOp { sigil: Rc::new(sigil.to_string()) }
} }
pub fn sigil(&self) -> &Rc<String> { pub fn sigil(&self) -> &Rc<String> {
&self.sigil &self.sigil
@ -35,8 +35,8 @@ pub struct PrefixOp {
} }
impl PrefixOp { impl PrefixOp {
pub fn from_sigil(sigil: Rc<String>) -> PrefixOp { pub fn from_sigil(sigil: &str) -> PrefixOp {
PrefixOp { sigil } PrefixOp { sigil: Rc::new(sigil.to_string()) }
} }
pub fn sigil(&self) -> &Rc<String> { pub fn sigil(&self) -> &Rc<String> {
&self.sigil &self.sigil

View File

@ -545,7 +545,7 @@ impl Parser {
_ => unreachable!(), _ => unreachable!(),
}; };
let rhs = self.precedence_expr(new_precedence)?; let rhs = self.precedence_expr(new_precedence)?;
let operation = BinOp::from_sigil(sigil); let operation = BinOp::from_sigil(sigil.as_ref());
lhs = Expression(ExpressionType::BinExp(operation, bx!(lhs), bx!(rhs)), None); lhs = Expression(ExpressionType::BinExp(operation, bx!(lhs), bx!(rhs)), None);
} }
self.parse_level -= 1; self.parse_level -= 1;
@ -561,7 +561,7 @@ impl Parser {
}; };
let expr = self.primary()?; let expr = self.primary()?;
Ok(Expression( Ok(Expression(
ExpressionType::PrefixExp(PrefixOp::from_sigil(sigil), bx!(expr)), ExpressionType::PrefixExp(PrefixOp::from_sigil(sigil.as_str()), bx!(expr)),
None)) None))
}, },
_ => self.primary() _ => self.primary()
@ -849,13 +849,10 @@ mod parse_tests {
($string:expr) => { assert!(parse(tokenize($string)).0.is_err()) } ($string:expr) => { assert!(parse(tokenize($string)).0.is_err()) }
} }
macro_rules! binexp { macro_rules! binexp {
($op:expr, $lhs:expr, $rhs:expr) => { BinExp(op!($op), bx!(Expression($lhs, None)), bx!(Expression($rhs, None))) } ($op:expr, $lhs:expr, $rhs:expr) => { BinExp(BinOp::from_sigil($op), bx!(Expression($lhs, None)), bx!(Expression($rhs, None))) }
} }
macro_rules! prefexp { macro_rules! prefexp {
($op:expr, $lhs:expr) => { PrefixExp(op!($op), bx!(Expression($lhs, None))) } ($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression($lhs, None))) }
}
macro_rules! op {
($op:expr) => { BinOp::from_sigil($op.to_string()) }
} }
macro_rules! val { macro_rules! val {
($var:expr) => { Value(Rc::new($var.to_string()), vec![]) } ($var:expr) => { Value(Rc::new($var.to_string()), vec![]) }