diff --git a/src/com/annimon/ownlang/lib/modules/functions/std_arrayCombine.java b/src/com/annimon/ownlang/lib/modules/functions/std_arrayCombine.java new file mode 100644 index 0000000..9e43c19 --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/functions/std_arrayCombine.java @@ -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; + } + +} \ No newline at end of file diff --git a/src/com/annimon/ownlang/lib/modules/functions/std_arrayKeyExists.java b/src/com/annimon/ownlang/lib/modules/functions/std_arrayKeyExists.java new file mode 100644 index 0000000..30a3967 --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/functions/std_arrayKeyExists.java @@ -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])); + } + +} \ No newline at end of file diff --git a/src/com/annimon/ownlang/lib/modules/functions/std_arrayKeys.java b/src/com/annimon/ownlang/lib/modules/functions/std_arrayKeys.java new file mode 100644 index 0000000..11c27b8 --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/functions/std_arrayKeys.java @@ -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 keys = new ArrayList<>(map.size()); + for (Map.Entry entry : map) { + keys.add(entry.getKey()); + } + return new ArrayValue(keys); + } + +} \ No newline at end of file diff --git a/src/com/annimon/ownlang/lib/modules/functions/std_arrayValues.java b/src/com/annimon/ownlang/lib/modules/functions/std_arrayValues.java new file mode 100644 index 0000000..fdd147e --- /dev/null +++ b/src/com/annimon/ownlang/lib/modules/functions/std_arrayValues.java @@ -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 values = new ArrayList<>(map.size()); + for (Map.Entry entry : map) { + values.add(entry.getValue()); + } + return new ArrayValue(values); + } + +} \ No newline at end of file diff --git a/src/com/annimon/ownlang/lib/modules/std.java b/src/com/annimon/ownlang/lib/modules/std.java index 9422a5f..7fceb41 100644 --- a/src/com/annimon/ownlang/lib/modules/std.java +++ b/src/com/annimon/ownlang/lib/modules/std.java @@ -13,8 +13,6 @@ public final class std implements Module { public void init() { Functions.set("echo", new std_echo()); 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("rand", new std_rand()); Functions.set("time", new std_time()); @@ -36,5 +34,13 @@ public final class std implements Module { Functions.set("replace", new std_replace()); Functions.set("replaceAll", new std_replaceall()); 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()); } }