Implementing a few more operators

WIP - not done
This commit is contained in:
greg 2017-01-05 18:10:57 -08:00
parent 4cf165b408
commit 23d2209d8b
2 changed files with 8 additions and 2 deletions

View File

@ -259,6 +259,8 @@ impl<'a> Evaluator<'a> {
match (&op[..], left, right) {
("+", Number(l), Number(r)) => Number(l + r),
("+", StringLiteral(s1), StringLiteral(s2)) => StringLiteral(Rc::new(format!("{}{}", *s1, *s2))),
("+", StringLiteral(s1), Number(r)) => StringLiteral(Rc::new(format!("{}{}", *s1, r))),
("+", Number(l), StringLiteral(s1)) => StringLiteral(Rc::new(format!("{}{}", l, *s1))),
("-", Number(l), Number(r)) => Number(l - r),
("*", Number(l), Number(r)) => Number(l * r),
("/", Number(l), Number(r)) if r != 0.0 => Number(l / r),
@ -271,6 +273,10 @@ impl<'a> Evaluator<'a> {
("==", Null, Null) => truthy,
("==", StringLiteral(s1), StringLiteral(s2)) => if s1 == s2 { truthy } else { falsy },
("==", _, _) => falsy,
("+=", l, r) => BinExp("=", l, BinExp("+", l, r)),
("-=", l, r) => BinExp("=", l, BinExp("-", l, r)),
("*=", l, r) => BinExp("=", l, BinExp("*", l, r)),
("/=", l, r) => BinExp("=", l, BinExp("/", l, r)),
_ => falsy,
}
}

View File

@ -127,8 +127,8 @@ impl Parser {
"*" => 20,
"/" => 20,
"%" => 20,
"=" => 1,
"==" => 40,
"==" => 40
"=" | "+=" | "-=" | "*=" | "/=" => 1,
">" | ">=" | "<" | "<=" => 30,
_ => 255,
}