Skip to content

Commit

Permalink
Resolved issue wyvernlang#334, added boolean comparison operator == a…
Browse files Browse the repository at this point in the history
…nd !=, added test cases in OIRTests.java
  • Loading branch information
sychoo committed Jul 12, 2019
1 parent 8b92a6d commit cd17950
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 @@ -694,7 +694,7 @@ public void testTypecheckingLoop() throws ParseException {
+ "\"typechecked!\"";
testPyFromInput(input, "typechecked!");
}

@Test
public void testComparisonOperator() throws ParseException {
String input =
Expand All @@ -711,8 +711,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 cd17950

Please sign in to comment.