diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-01-08 21:10:34 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-01-08 21:10:34 +0000 |
| commit | 7638649357e9cc2c339e743dc84b5249c6cdc0de (patch) | |
| tree | 7f2e50d942792e0f84948549f5cb7be8843b6f99 /src | |
| parent | 2201e7bd489dd3d14f647ff38d6283a88f15ed84 (diff) | |
Add support for adding strings
Diffstat (limited to 'src')
4 files changed, 32 insertions, 7 deletions
diff --git a/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java b/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java index cc2ec06..b370f6d 100644 --- a/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java +++ b/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java @@ -35,6 +35,9 @@ public class PrimitiveMethods { .put("substring", (receiver, arguments) -> pythonGetSlice(receiver, arguments)) + + .put("add", (receiver, arguments) -> + PythonBinaryOperation.pythonAdd(receiver, arguments.get(0))) .build(); diff --git a/src/main/java/org/zwobble/couscous/backends/python/ast/PythonBinaryOperation.java b/src/main/java/org/zwobble/couscous/backends/python/ast/PythonBinaryOperation.java index 1fc87c3..fb33626 100644 --- a/src/main/java/org/zwobble/couscous/backends/python/ast/PythonBinaryOperation.java +++ b/src/main/java/org/zwobble/couscous/backends/python/ast/PythonBinaryOperation.java @@ -6,6 +6,10 @@ public class PythonBinaryOperation implements PythonExpressionNode { public static PythonExpressionNode pythonIs(PythonExpressionNode left, PythonExpressionNode right) { return new PythonBinaryOperation("is", left, right); } + + public static PythonExpressionNode pythonAdd(PythonExpressionNode left, PythonExpressionNode right) { + return new PythonBinaryOperation("+", left, right); + } private final String operator; private final PythonExpressionNode left; diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/StringInterpreterValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/StringInterpreterValue.java index bbc9f6c..b19d849 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/StringInterpreterValue.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/StringInterpreterValue.java @@ -1,20 +1,33 @@ package org.zwobble.couscous.interpreter.values; -import java.util.Optional; import org.zwobble.couscous.interpreter.NoSuchField; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.PrimitiveValue; import org.zwobble.couscous.values.PrimitiveValues; import org.zwobble.couscous.values.StringValue; -import static java.util.Arrays.asList; + +import java.util.Optional; + import static org.zwobble.couscous.util.ExtraLists.list; public final class StringInterpreterValue implements InterpreterValue { - public static final ConcreteType TYPE = ConcreteType.builder(StringInterpreterValue.class, StringValue.REF).method("length", list(), (environment, arguments) -> new IntegerInterpreterValue(arguments.getReceiver().value.length())).method("substring", list(IntegerValue.REF, IntegerValue.REF), (environment, arguments) -> { - IntegerInterpreterValue startIndex = (IntegerInterpreterValue)arguments.get(0); - IntegerInterpreterValue endIndex = (IntegerInterpreterValue)arguments.get(1); - return new StringInterpreterValue(arguments.getReceiver().value.substring(startIndex.getValue(), endIndex.getValue())); - }).build(); + public static final ConcreteType TYPE = ConcreteType.builder(StringInterpreterValue.class, StringValue.REF) + .method("length", list(), (environment, arguments) -> + new IntegerInterpreterValue(arguments.getReceiver().value.length())) + + .method("substring", list(IntegerValue.REF, IntegerValue.REF), (environment, arguments) -> { + IntegerInterpreterValue startIndex = (IntegerInterpreterValue)arguments.get(0); + IntegerInterpreterValue endIndex = (IntegerInterpreterValue)arguments.get(1); + return new StringInterpreterValue(arguments.getReceiver().value.substring(startIndex.getValue(), endIndex.getValue())); + }) + + .method("add", list(StringValue.REF), (environment, arguments) -> { + StringInterpreterValue right = (StringInterpreterValue)arguments.get(0); + return new StringInterpreterValue(arguments.getReceiver().value + right.value); + }) + + .build(); + private final String value; @Override diff --git a/src/test/java/org/zwobble/couscous/tests/CompilerTests.java b/src/test/java/org/zwobble/couscous/tests/CompilerTests.java index ea12bc4..e8957f7 100644 --- a/src/test/java/org/zwobble/couscous/tests/CompilerTests.java +++ b/src/test/java/org/zwobble/couscous/tests/CompilerTests.java @@ -144,6 +144,11 @@ public abstract class CompilerTests { list())); } + @Test + public void stringAdd() throws Exception { + assertEquals(value("42"), evalExpression("String", "\"4\" + \"2\"")); + } + private PrimitiveValue execTestProgram( String directoryName, TypeName type, |
