Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved issue #334, added boolean comparison operator == and !=, added test cases. #339

Merged
merged 1 commit into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}