Skip to content

Commit

Permalink
Partially fixed issue wyvernlang#337, support concatenation between c…
Browse files Browse the repository at this point in the history
…haracter and string literals. Failed Ant TestSuite (infinite loop on effects test).
  • Loading branch information
sychoo committed Jul 12, 2019
1 parent 8b92a6d commit 2baeda5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 3 additions & 2 deletions tools/src/wyvern/stdlib/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public static ValueType getSystemType() {
stringDeclTypes.add(new DefDeclType("==", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.stringType()))));
stringDeclTypes.add(new DefDeclType("<", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.stringType()))));
stringDeclTypes.add(new DefDeclType(">", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.stringType()))));
stringDeclTypes.add(new DefDeclType("+", Util.stringType(), Arrays.asList(new FormalArg("other", Util.stringType()))));
stringDeclTypes.add(new DefDeclType("+", Util.stringType(), Arrays.asList(new FormalArg("other", Util.emptyType()))));
stringDeclTypes.add(new DefDeclType("<=", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.stringType()))));
stringDeclTypes.add(new DefDeclType(">=", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.stringType()))));
stringDeclTypes.add(new DefDeclType("!=", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.stringType()))));
Expand All @@ -245,7 +245,8 @@ public static ValueType getSystemType() {
charDeclTypes.add(new DefDeclType("!=", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.charType()))));
charDeclTypes.add(new DefDeclType("<=", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.charType()))));
charDeclTypes.add(new DefDeclType(">=", Util.booleanType(), Arrays.asList(new FormalArg("other", Util.charType()))));

charDeclTypes.add(new DefDeclType("+", Util.stringType(), Arrays.asList(new FormalArg("other", Util.emptyType()))));

ValueType charType = new StructuralType("charSelf", charDeclTypes);
declTypes.add(new ConcreteTypeMember("Character", charType));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ public Value invoke(String methodName, List<Value> args, FileLocation loc) {
case "<=": return new BooleanLiteral(this.value.compareTo(((CharacterLiteral) args.get(0)).getValue()) <= 0);
case ">=": return new BooleanLiteral(this.value.compareTo(((CharacterLiteral) args.get(0)).getValue()) >= 0);
case "!=": return new BooleanLiteral(this.value.compareTo(((CharacterLiteral) args.get(0)).getValue()) != 0);
case "+":
// check the type of the right hand side of the operator
if (args.get(0) instanceof CharacterLiteral) {
// case when the right hand side of the operator is a CharacterLiteral type.
return new StringLiteral("" + this.value + ((CharacterLiteral) args.get(0)).getValue());
} else if (args.get(0) instanceof StringLiteral) {
// case when the right hand side of the operator is a StringLiteral type.
return new StringLiteral(this.value + ((StringLiteral) args.get(0)).getValue());
} else {
// case when the right hand side of the operator is neither a CharacterLiteral type nor a StringLiteral type.
throw new RuntimeException("The right side of \" + \" operator has wrong type! It must be either a character or a string.");
}

default: throw new RuntimeException("runtime error: character operation " + methodName + "not supported by the runtime");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,18 @@ public Value invoke(String methodName, List<Value> args, FileLocation loc) {
case "<=": return new BooleanLiteral(value.compareTo(((StringLiteral) args.get(0)).getValue()) <= 0);
case ">=": return new BooleanLiteral(value.compareTo(((StringLiteral) args.get(0)).getValue()) >= 0);
case "!=": return new BooleanLiteral(value.compareTo(((StringLiteral) args.get(0)).getValue()) != 0);
case "+": return new StringLiteral(value + ((StringLiteral) args.get(0)).getValue());
case "+":
// check the type of the right hand side of the operator
if (args.get(0) instanceof CharacterLiteral) {
// case when the right hand side of the operator is a CharacterLiteral type.
return new StringLiteral("" + this.value + ((CharacterLiteral) args.get(0)).getValue());
} else if (args.get(0) instanceof StringLiteral) {
// case when the right hand side of the operator is a StringLiteral type.
return new StringLiteral(this.value + ((StringLiteral) args.get(0)).getValue());
} else {
// case when the right hand side of the operator is neither a CharacterLiteral type nor a StringLiteral type.
throw new RuntimeException("The right side of \" + \" operator has wrong type! It must be either a character or a string.");
}
case "equals": return new BooleanLiteral(value.equals(((StringLiteral) args.get(0)).getValue()));
case "length": return new IntegerLiteral(value.length());
case "charAt": return new CharacterLiteral(value.charAt(((IntegerLiteral) args.get(0)).getValue()));
Expand Down

0 comments on commit 2baeda5

Please sign in to comment.