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
1e33fa3a16
commit
ef701c979f
@ -0,0 +1,30 @@
|
|||||||
|
package com.annimon.ownlang.lib.modules.functions;
|
||||||
|
|
||||||
|
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
|
||||||
|
import com.annimon.ownlang.exceptions.TypeException;
|
||||||
|
import com.annimon.ownlang.lib.*;
|
||||||
|
|
||||||
|
public final class std_arrayCombine implements Function {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value execute(Value... args) {
|
||||||
|
if (args.length != 2) throw new ArgumentsMismatchException("Two arguments expected");
|
||||||
|
if (args[0].type() != Types.ARRAY) {
|
||||||
|
throw new TypeException("Array expected in first argument");
|
||||||
|
}
|
||||||
|
if (args[1].type() != Types.ARRAY) {
|
||||||
|
throw new TypeException("Array expected in second argument");
|
||||||
|
}
|
||||||
|
|
||||||
|
final ArrayValue keys = ((ArrayValue) args[0]);
|
||||||
|
final ArrayValue values = ((ArrayValue) args[1]);
|
||||||
|
final int length = Math.min(keys.size(), values.size());
|
||||||
|
|
||||||
|
final MapValue result = new MapValue(length);
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
result.set(keys.get(i), values.get(i));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.annimon.ownlang.lib.modules.functions;
|
||||||
|
|
||||||
|
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
|
||||||
|
import com.annimon.ownlang.exceptions.TypeException;
|
||||||
|
import com.annimon.ownlang.lib.*;
|
||||||
|
|
||||||
|
public final class std_arrayKeyExists implements Function {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value execute(Value... args) {
|
||||||
|
if (args.length != 2) throw new ArgumentsMismatchException("Two arguments expected");
|
||||||
|
if (args[1].type() != Types.MAP) {
|
||||||
|
throw new TypeException("Map expected in second argument");
|
||||||
|
}
|
||||||
|
final MapValue map = ((MapValue) args[1]);
|
||||||
|
return NumberValue.fromBoolean(map.containsKey(args[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.annimon.ownlang.lib.modules.functions;
|
||||||
|
|
||||||
|
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
|
||||||
|
import com.annimon.ownlang.exceptions.TypeException;
|
||||||
|
import com.annimon.ownlang.lib.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public final class std_arrayKeys implements Function {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value execute(Value... args) {
|
||||||
|
if (args.length != 1) throw new ArgumentsMismatchException("One argument expected");
|
||||||
|
if (args[0].type() != Types.MAP) {
|
||||||
|
throw new TypeException("Map expected in first argument");
|
||||||
|
}
|
||||||
|
final MapValue map = ((MapValue) args[0]);
|
||||||
|
final List<Value> keys = new ArrayList<>(map.size());
|
||||||
|
for (Map.Entry<Value, Value> entry : map) {
|
||||||
|
keys.add(entry.getKey());
|
||||||
|
}
|
||||||
|
return new ArrayValue(keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.annimon.ownlang.lib.modules.functions;
|
||||||
|
|
||||||
|
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
|
||||||
|
import com.annimon.ownlang.exceptions.TypeException;
|
||||||
|
import com.annimon.ownlang.lib.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public final class std_arrayValues implements Function {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value execute(Value... args) {
|
||||||
|
if (args.length != 1) throw new ArgumentsMismatchException("One argument expected");
|
||||||
|
if (args[0].type() != Types.MAP) {
|
||||||
|
throw new TypeException("Map expected in first argument");
|
||||||
|
}
|
||||||
|
final MapValue map = ((MapValue) args[0]);
|
||||||
|
final List<Value> values = new ArrayList<>(map.size());
|
||||||
|
for (Map.Entry<Value, Value> entry : map) {
|
||||||
|
values.add(entry.getValue());
|
||||||
|
}
|
||||||
|
return new ArrayValue(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -13,8 +13,6 @@ public final class std implements Module {
|
|||||||
public void init() {
|
public void init() {
|
||||||
Functions.set("echo", new std_echo());
|
Functions.set("echo", new std_echo());
|
||||||
Functions.set("readln", new std_readln());
|
Functions.set("readln", new std_readln());
|
||||||
Functions.set("newarray", new std_newarray());
|
|
||||||
Functions.set("sort", new std_sort());
|
|
||||||
Functions.set("length", new std_length());
|
Functions.set("length", new std_length());
|
||||||
Functions.set("rand", new std_rand());
|
Functions.set("rand", new std_rand());
|
||||||
Functions.set("time", new std_time());
|
Functions.set("time", new std_time());
|
||||||
@ -36,5 +34,13 @@ public final class std implements Module {
|
|||||||
Functions.set("replace", new std_replace());
|
Functions.set("replace", new std_replace());
|
||||||
Functions.set("replaceAll", new std_replaceall());
|
Functions.set("replaceAll", new std_replaceall());
|
||||||
Functions.set("replaceFirst", new std_replacefirst());
|
Functions.set("replaceFirst", new std_replacefirst());
|
||||||
|
|
||||||
|
// Arrays and maps
|
||||||
|
Functions.set("newarray", new std_newarray());
|
||||||
|
Functions.set("sort", new std_sort());
|
||||||
|
Functions.set("arrayCombine", new std_arrayCombine());
|
||||||
|
Functions.set("arrayKeyExists ", new std_arrayKeyExists());
|
||||||
|
Functions.set("arrayKeys ", new std_arrayKeys());
|
||||||
|
Functions.set("arrayValues ", new std_arrayValues());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user