Операция слияния объектов

This commit is contained in:
Victor 2016-02-28 10:14:00 +02:00
parent 56c504cc01
commit 1e33fa3a16
3 changed files with 13 additions and 2 deletions

View File

@ -14,6 +14,13 @@ public class MapValue implements Value, Iterable<Map.Entry<Value, Value>> {
public static final MapValue EMPTY = new MapValue(1);
public static MapValue merge(MapValue map1, MapValue map2) {
final MapValue result = new MapValue(map1.size() + map2.size());
result.map.putAll(map1.map);
result.map.putAll(map2.map);
return result;
}
private final Map<Value, Value> map;
public MapValue(int size) {

View File

@ -10,7 +10,7 @@ public final class Types {
MAP = 4,
FUNCTION = 5;
private static int FIRST = OBJECT, LAST = FUNCTION;
private static final int FIRST = OBJECT, LAST = FUNCTION;
private static final String[] NAMES = {"object", "number", "string", "array", "map", "function"};
public static String typeToString(int type) {

View File

@ -4,6 +4,7 @@ import com.annimon.ownlang.exceptions.OperationIsNotSupportedException;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.Functions;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.StringValue;
import com.annimon.ownlang.lib.Types;
@ -89,7 +90,10 @@ public final class BinaryExpression implements Expression {
case Types.NUMBER: return add((NumberValue) value1, value2);
case Types.STRING: return new StringValue(value1.asString() + value2.asString());
case Types.ARRAY: return ArrayValue.add((ArrayValue) value1, value2);
case Types.MAP: /* TODO: merge maps */
case Types.MAP:
if (value2.type() != Types.MAP)
throw new TypeException("Cannot merge non map value to map");
return MapValue.merge((MapValue) value1, (MapValue) value2);
case Types.FUNCTION: /* TODO: combining functions */
default:
// Concatenation strings