FEATURE: Comparison binary expressions now support non-compare expressions on both sides.

This commit is contained in:
Jeremy Wall 2018-06-06 21:02:02 -05:00
parent 3d7c70aa2a
commit 3524a5df73
2 changed files with 13 additions and 9 deletions

View File

@ -1,5 +1,4 @@
use super::Builder; use super::Builder;
use std;
fn assert_build(input: &str) { fn assert_build(input: &str) {
let mut b = Builder::new("<Eval>"); let mut b = Builder::new("<Eval>");
@ -13,8 +12,7 @@ fn assert_build(input: &str) {
#[test] #[test]
fn test_comparisons() { fn test_comparisons() {
assert_build( assert_build(
" "let one = 1;
let one = 1;
let two = 2; let two = 2;
let foo = \"foo\"; let foo = \"foo\";
let bar = \"bar\"; let bar = \"bar\";
@ -45,8 +43,8 @@ fn test_comparisons() {
#[test] #[test]
fn test_deep_comparison() { fn test_deep_comparison() {
let input = " assert_build(
let tpl1 = { "let tpl1 = {
foo = \"bar\", foo = \"bar\",
lst = [1, 2, 3], lst = [1, 2, 3],
inner = { inner = {
@ -65,8 +63,8 @@ fn test_deep_comparison() {
assert \"tpl1 == copy\"; assert \"tpl1 == copy\";
assert \"tpl1 != extra\"; assert \"tpl1 != extra\";
assert \"tpl1 != less\"; assert \"tpl1 != less\";
"; ",
assert_build(input); );
} }
#[test] #[test]
@ -74,6 +72,10 @@ fn test_expression_comparisons() {
assert_build("assert \"2 == 1+1\";"); assert_build("assert \"2 == 1+1\";");
assert_build("assert \"(1+1) == 2\";"); assert_build("assert \"(1+1) == 2\";");
assert_build("assert \"(1+1) == (1+1)\";"); assert_build("assert \"(1+1) == (1+1)\";");
assert_build(
"let want = \"foo\";
assert \"select want, 1, { foo=2, } == 2\";",
);
} }
#[test] #[test]

View File

@ -386,14 +386,16 @@ fn tuple_to_compare_expression(
})) }))
} }
// This macro is much simpler than the math binary expressions since they are the
// bottom of the precendence tree and we can hard code the precedence in here.
macro_rules! do_compare_expr { macro_rules! do_compare_expr {
($i:expr, $subrule:ident!( $($args:tt)* ), $typ:expr) => { ($i:expr, $subrule:ident!( $($args:tt)* ), $typ:expr) => {
map_res!($i, map_res!($i,
do_parse!( do_parse!(
pos: pos >> pos: pos >>
left: alt!(simple_expression | grouped_expression | math_expression) >> left: alt!(math_expression | non_op_expression) >>
$subrule!($($args)*) >> $subrule!($($args)*) >>
right: expression >> right: alt!(math_expression | non_op_expression) >>
(pos, $typ, left, right) (pos, $typ, left, right)
), ),
tuple_to_compare_expression tuple_to_compare_expression