Convert && and || to shortcut boolean operators.

This commit is contained in:
Ondrej Zajicek 2011-03-23 13:40:46 +01:00
parent 26d92bb892
commit 0aa88530ad
2 changed files with 16 additions and 10 deletions

View file

@ -523,16 +523,21 @@ interpret(struct f_inst *what)
break; break;
case '&': case '&':
TWOARGS_C;
res.type = v1.type;
if (res.type != T_BOOL) runtime( "Can't do boolean operation on non-booleans" );
res.val.i = v1.val.i && v2.val.i;
break;
case '|': case '|':
TWOARGS_C; ARG(v1, a1.p);
res.type = v1.type; if (v1.type != T_BOOL)
if (res.type != T_BOOL) runtime( "Can't do boolean operation on non-booleans" ); runtime( "Can't do boolean operation on non-booleans" );
res.val.i = v1.val.i || v2.val.i; if (v1.val.i == (what->code == '|')) {
res.type = T_BOOL;
res.val.i = v1.val.i;
break;
}
ARG(v2, a2.p);
if (v2.type != T_BOOL)
runtime( "Can't do boolean operation on non-booleans" );
res.type = T_BOOL;
res.val.i = v2.val.i;
break; break;
case P('m','p'): case P('m','p'):
@ -577,6 +582,7 @@ interpret(struct f_inst *what)
res.val.i = val_in_range(v1, v2); res.val.i = val_in_range(v1, v2);
if (res.val.i == CMP_ERROR) if (res.val.i == CMP_ERROR)
runtime( "~ applied on unknown type pair" ); runtime( "~ applied on unknown type pair" );
res.val.i = !!res.val.i;
break; break;
case P('d','e'): case P('d','e'):
ONEARG; ONEARG;

View file

@ -180,7 +180,7 @@ string s;
is = [ 2, 3, 4, 7..11 ]; is = [ 2, 3, 4, 7..11 ];
print " must be true: ", 1.2.0.0/16 ~ [ 1.0.0.0/8{ 15 , 17 } ]; print " must be true: ", 1.2.0.0/16 ~ [ 1.0.0.0/8{ 15 , 17 } ];
print " data types; must be true: ", 1.2.3.4 = 1.2.3.4, ",", 1 ~ [1,2,3], ",", 5 ~ [1..20], ",", 10 ~ is, ",", 2 ~ [ 1, 2, 3 ], ",", 5 ~ [ 4 .. 7 ], ",", 1.2.3.4 ~ [ 1.2.3.3..1.2.3.5 ], ",", 1.2.3.4 ~ 1.0.0.0/8, ",", 1.0.0.0/8 ~ 1.0.0.0/8, ",", 1.0.0.0/8 ~ [ 1.0.0.0/8+ ]; print " data types; must be true: ", 1.2.3.4 = 1.2.3.4, ",", 1 ~ [1,2,3], ",", 5 ~ [1..20], ",", 10 ~ is, ",", 2 ~ [ 1, 2, 3 ], ",", 5 ~ [ 4 .. 7 ], ",", 1.2.3.4 ~ [ 1.2.3.3..1.2.3.5 ], ",", 1.2.3.4 ~ 1.0.0.0/8, ",", 1.0.0.0/8 ~ 1.0.0.0/8, ",", 1.0.0.0/8 ~ [ 1.0.0.0/8+ ];
print " must be true: ", true && true, ",", true || false, ",", ! false && ! false && true, ",", 1 < 2 && 1 != 3, ",", true && true && ! false; print " must be true: ", true && true, ",", true || false, ",", ! false && ! false && true, ",", 1 < 2 && 1 != 3, ",", true && true && ! false, ",", true || 1+"a", ",", !(false && 1+"a");
print " must be true: ", defined(1), ",", defined(1.2.3.4), ",", 1 != 2, ",", 1 <= 2; print " must be true: ", defined(1), ",", defined(1.2.3.4), ",", 1 != 2, ",", 1 <= 2;
print " data types: must be false: ", 1 ~ [ 2, 3, 4 ], ",", 5 ~ is, ",", 1.2.3.4 ~ [ 1.2.3.3, 1.2.3.5 ], ",", (1,2) > (2,2), ",", (1,1) > (1,1), ",", 1.0.0.0/9 ~ [ 1.0.0.0/8- ], ",", 1.2.0.0/17 ~ [ 1.0.0.0/8{ 15 , 16 } ], ",", true && false; print " data types: must be false: ", 1 ~ [ 2, 3, 4 ], ",", 5 ~ is, ",", 1.2.3.4 ~ [ 1.2.3.3, 1.2.3.5 ], ",", (1,2) > (2,2), ",", (1,1) > (1,1), ",", 1.0.0.0/9 ~ [ 1.0.0.0/8- ], ",", 1.2.0.0/17 ~ [ 1.0.0.0/8{ 15 , 16 } ], ",", true && false;