Skip to content

Commit

Permalink
Merge pull request #339 from sychoo/boolean-comparison
Browse files Browse the repository at this point in the history
Resolved issue #334, added boolean comparison operator == and !=, added test cases.
  • Loading branch information
JonathanAldrich authored Sep 17, 2019
2 parents 7dffaf8 + cd17950 commit 9219dd8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions tools/src/wyvern/stdlib/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ public static ValueType getSystemType() {
boolDeclTypes.add(new DefDeclType("&&", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.booleanType()))));
boolDeclTypes.add(new DefDeclType("||", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.booleanType()))));
boolDeclTypes.add(new DefDeclType("!", Util.booleanType(), Arrays.asList()));
boolDeclTypes.add(new DefDeclType("==", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.booleanType()))));
boolDeclTypes.add(new DefDeclType("!=", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.booleanType()))));

// construct a type for the system object
// N.B.: Operations here are built-in and performed on an instance of a type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import wyvern.tools.errors.FileLocation;

public class BooleanLiteral extends Literal implements Invokable {
private boolean value;
private Boolean value;

@Override
public int hashCode() {
Expand Down Expand Up @@ -118,7 +118,12 @@ public ValueType typeCheck(TypeContext ctx, EffectAccumulator effectAccumulator)
return new BooleanLiteral(this.value || ((BooleanLiteral) args.get(0)).value);
case "!":
return new BooleanLiteral(!this.value);
default: throw new RuntimeException();
case "==":
return new BooleanLiteral(this.value.compareTo(((BooleanLiteral) args.get(0)).getValue()) == 0);
case "!=":
return new BooleanLiteral(this.value.compareTo(((BooleanLiteral) args.get(0)).getValue()) != 0);
default:
throw new RuntimeException();
}
}

Expand Down
9 changes: 7 additions & 2 deletions tools/src/wyvern/tools/tests/OIRTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ public void testTypecheckingLoop() throws ParseException {
+ "\"typechecked!\"";
testPyFromInput(input, "typechecked!");
}

@Test
public void testComparisonOperator() throws ParseException {
String input =
Expand All @@ -727,8 +727,13 @@ public void testComparisonOperator() throws ParseException {
+ "stdout.printBoolean(\"a\" > \"b\")\n"
+ "stdout.printBoolean(\"a\" < \"b\")\n"
+ "stdout.printBoolean(\"a\" != \"b\")\n"
+ "stdout.printBoolean(true == true)\n"
+ "stdout.printBoolean(false == true)\n"
+ "stdout.printBoolean(false != true)\n"
+ "stdout.printBoolean((1 > 2) == (2 > 3))\n"
+ "stdout.println()\n"
+ "0";
testPyFromInput(input, "FalseTrueFalseFalseTrueTrueFalseTrueFalseFalseTrueTrue\n0");
testPyFromInput(input, "FalseTrueFalseFalseTrueTrueFalseTrueFalseFalseTrueTrueTrueFalseTrueTrue\n0");
}
}

0 comments on commit 9219dd8

Please sign in to comment.