Skip to content

Commit

Permalink
Corrected fix for #54
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborsch committed Apr 28, 2024
1 parent 0202dcd commit b471381
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
17 changes: 12 additions & 5 deletions programs/tests/fixtures/operators/booleans.rock
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ say not X (false)
say not X and not Y (false)
say not X or not Y (true)
say not X and not Y and not X or not Y (true)


shout "-----------"
shout 0 or 42
shout 0 or 0
shout 16 and 42
shout 16 and 0
shout 16 or 42
shout 16 or 0
shout 0 and 42
shout 0 and 0

shout 16 and 42
shout 16 and 0
shout "-----------"
shout true and true
shout true and false
shout false and true
shout false and false
shout true or true
shout true or false
shout false or true
shout false or false

16 changes: 13 additions & 3 deletions programs/tests/fixtures/operators/booleans.rock.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ false
false
true
true
-----------
42
0
0
16
16
16
16
0
0
42
0
-----------
true
false
false
false
true
true
true
false
Binary file modified rocky.jar
Binary file not shown.
11 changes: 5 additions & 6 deletions src/rockstar/expression/LogicalExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,16 @@ public Value evaluate(BlockContext ctx) {
// short circuit: do not evaluate expr2 if not needed
switch (type) {
case AND:
if (v1.isBoolean() && v1.asBoolean().equals(Value.BOOLEAN_TRUE)) {
return ctx.afterExpression(this, v1.and(expr2.evaluate(ctx)));
if (v1.asBoolean().equals(Value.BOOLEAN_TRUE)) {
return ctx.afterExpression(this, expr2.evaluate(ctx));
}
return ctx.afterExpression(this, v1);
case OR:
if (v1.isBoolean() && v1.asBoolean().equals(Value.BOOLEAN_FALSE)) {
return ctx.afterExpression(this, v1.or(expr2.evaluate(ctx)));
}
if (v1.asBoolean().equals(Value.BOOLEAN_FALSE)) {
return ctx.afterExpression(this, expr2.evaluate(ctx)); }
return ctx.afterExpression(this, v1);
case NOR:
if (v1.isBoolean() && v1.asBoolean().equals(Value.BOOLEAN_FALSE)) {
if (v1.asBoolean().equals(Value.BOOLEAN_FALSE)) {
return ctx.afterExpression(this, v1.nor(expr2.evaluate(ctx)));
}
return ctx.afterExpression(this, Value.BOOLEAN_FALSE);
Expand Down

0 comments on commit b471381

Please sign in to comment.