From ef701c979f6d4666cea113c6faa8518af29d3754 Mon Sep 17 00:00:00 2001 From: Victor Date: Sun, 28 Feb 2016 10:34:51 +0200 Subject: [PATCH] =?UTF-8?q?=D0=A4=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20?= =?UTF-8?q?=D1=81=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/functions/std_arrayCombine.java | 30 +++++++++++++++++++ .../modules/functions/std_arrayKeyExists.java | 19 ++++++++++++ .../lib/modules/functions/std_arrayKeys.java | 26 ++++++++++++++++ .../modules/functions/std_arrayValues.java | 26 ++++++++++++++++ src/com/annimon/ownlang/lib/modules/std.java | 10 +++++-- 5 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/com/annimon/ownlang/lib/modules/functions/std_arrayCombine.java create mode 100644 src/com/annimon/ownlang/lib/modules/functions/std_arrayKeyExists.java create mode 100644 src/com/annimon/ownlang/lib/modules/functions/std_arrayKeys.java create mode 100644 src/com/annimon/ownlang/lib/modules/functions/std_arrayValues.java 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()); } }