mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Операция слияния объектов
This commit is contained in:
parent
56c504cc01
commit
1e33fa3a16
@ -14,6 +14,13 @@ public class MapValue implements Value, Iterable<Map.Entry<Value, Value>> {
|
|||||||
|
|
||||||
public static final MapValue EMPTY = new MapValue(1);
|
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;
|
private final Map<Value, Value> map;
|
||||||
|
|
||||||
public MapValue(int size) {
|
public MapValue(int size) {
|
||||||
|
@ -10,7 +10,7 @@ public final class Types {
|
|||||||
MAP = 4,
|
MAP = 4,
|
||||||
FUNCTION = 5;
|
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"};
|
private static final String[] NAMES = {"object", "number", "string", "array", "map", "function"};
|
||||||
|
|
||||||
public static String typeToString(int type) {
|
public static String typeToString(int type) {
|
||||||
|
@ -4,6 +4,7 @@ import com.annimon.ownlang.exceptions.OperationIsNotSupportedException;
|
|||||||
import com.annimon.ownlang.exceptions.TypeException;
|
import com.annimon.ownlang.exceptions.TypeException;
|
||||||
import com.annimon.ownlang.lib.ArrayValue;
|
import com.annimon.ownlang.lib.ArrayValue;
|
||||||
import com.annimon.ownlang.lib.Functions;
|
import com.annimon.ownlang.lib.Functions;
|
||||||
|
import com.annimon.ownlang.lib.MapValue;
|
||||||
import com.annimon.ownlang.lib.NumberValue;
|
import com.annimon.ownlang.lib.NumberValue;
|
||||||
import com.annimon.ownlang.lib.StringValue;
|
import com.annimon.ownlang.lib.StringValue;
|
||||||
import com.annimon.ownlang.lib.Types;
|
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.NUMBER: return add((NumberValue) value1, value2);
|
||||||
case Types.STRING: return new StringValue(value1.asString() + value2.asString());
|
case Types.STRING: return new StringValue(value1.asString() + value2.asString());
|
||||||
case Types.ARRAY: return ArrayValue.add((ArrayValue) value1, value2);
|
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 */
|
case Types.FUNCTION: /* TODO: combining functions */
|
||||||
default:
|
default:
|
||||||
// Concatenation strings
|
// Concatenation strings
|
||||||
|
Loading…
Reference in New Issue
Block a user