From 494680046c9d5737e0ce2786938c9a4ec91dbb3e Mon Sep 17 00:00:00 2001
From: Florian Angerer <florian.angerer@oracle.com>
Date: Thu, 2 Feb 2017 16:51:24 +0100
Subject: [PATCH] added builtin function "is.single"

---
 .../r/nodes/builtin/base/BasePackage.java     |  1 +
 .../r/nodes/builtin/base/IsSingle.java        | 47 ++++++++++++++++
 .../com/oracle/truffle/r/runtime/RError.java  |  3 +-
 .../truffle/r/test/ExpectedTestOutput.test    | 20 +++++++
 .../r/test/builtins/TestBuiltin_isSingle.java | 55 +++++++++++++++++++
 5 files changed, 125 insertions(+), 1 deletion(-)
 create mode 100644 com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/IsSingle.java
 create mode 100644 com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_isSingle.java

diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java
index 369665c952..223dadf8ec 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java
@@ -683,6 +683,7 @@ public class BasePackage extends RBuiltinPackage {
         add(WhichFunctions.WhichMax.class, WhichFunctions.WhichMax::create);
         add(WhichFunctions.WhichMin.class, WhichFunctions.WhichMin::create);
         add(Xtfrm.class, XtfrmNodeGen::create);
+        add(IsSingle.class, IsSingleNodeGen::create);
 
         // infix functions
         add(Subscript.class, SubscriptNodeGen::create, Subscript::special);
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/IsSingle.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/IsSingle.java
new file mode 100644
index 0000000000..43baa46886
--- /dev/null
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/IsSingle.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.r.nodes.builtin.base;
+
+import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE;
+import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE;
+
+import com.oracle.truffle.api.dsl.Specialization;
+import com.oracle.truffle.r.nodes.builtin.CastBuilder;
+import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
+import com.oracle.truffle.r.runtime.RError;
+import com.oracle.truffle.r.runtime.builtins.RBuiltin;
+
+@RBuiltin(name = "is.single", kind = PRIMITIVE, parameterNames = {"x"}, behavior = PURE)
+public abstract class IsSingle extends RBuiltinNode {
+
+    @Override
+    protected void createCasts(CastBuilder casts) {
+        casts.arg("x").mustNotBeMissing(RError.Message.ARGUMENTS_PASSED, 0, "'is.single'", 1);
+    }
+
+    @Specialization
+    protected Object isSingle(@SuppressWarnings("unused") Object x) {
+        throw RError.error(this, RError.Message.UNIMPLEMENTED_TYPE_IN_R, "single");
+    }
+
+}
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RError.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RError.java
index 8f3c3a3a6d..b74df9293b 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RError.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RError.java
@@ -782,7 +782,8 @@ public final class RError extends RuntimeException {
         MACRO_CAN_BE_APPLIED_TO("%s can only be applied to a '%s', not a '%s'"),
         LOSS_OF_ACCURACY_MOD("probable complete loss of accuracy in modulus"),
         LENGTH_MISAPPLIED("LENGTH or similar applied to %s object"),
-        TOO_MANY_ARGS("too many arguments");
+        TOO_MANY_ARGS("too many arguments"),
+        UNIMPLEMENTED_TYPE_IN_R("type \"%s\" unimplemented in R");
 
         public final String message;
         final boolean hasArgs;
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test
index c96eb6ba63..95ec299279 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test
@@ -24202,6 +24202,26 @@ Levels: a.b.c a.b.b.c a.c
 #argv <- list(structure(3.14159265358979, class = structure('3.14159265358979', class = 'testit')));isS4(argv[[1]]);
 [1] FALSE
 
+##com.oracle.truffle.r.test.builtins.TestBuiltin_isSingle.testisSingle0#
+#is.single
+function (x)  .Primitive("is.single")
+
+##com.oracle.truffle.r.test.builtins.TestBuiltin_isSingle.testisSingle1#
+#is.single()
+Error in is.single() : 0 arguments passed to 'is.single' which requires 1
+
+##com.oracle.truffle.r.test.builtins.TestBuiltin_isSingle.testisSingle2#
+#is.single(0)
+Error in is.single(0) : type "single" unimplemented in R
+
+##com.oracle.truffle.r.test.builtins.TestBuiltin_isSingle.testisSingle3#
+#is.single(NULL)
+Error in is.single(NULL) : type "single" unimplemented in R
+
+##com.oracle.truffle.r.test.builtins.TestBuiltin_isSingle.testisSingle4#
+#is.single(+)
+Error: unexpected ')' in "is.single(+)"
+
 ##com.oracle.truffle.r.test.builtins.TestBuiltin_isTRUE.testIsTRUE#
 #{ file.path("a", "b", c("d","e","f")) }
 [1] "a/b/d" "a/b/e" "a/b/f"
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_isSingle.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_isSingle.java
new file mode 100644
index 0000000000..bfa7a01bef
--- /dev/null
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_isSingle.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.r.test.builtins;
+
+import org.junit.Test;
+
+import com.oracle.truffle.r.test.TestBase;
+
+public class TestBuiltin_isSingle extends TestBase {
+
+    @Test
+    public void testisSingle0() {
+        assertEval("is.single");
+    }
+
+    @Test
+    public void testisSingle1() {
+        assertEval("is.single()");
+    }
+
+    @Test
+    public void testisSingle2() {
+        assertEval("is.single(0)");
+    }
+
+    @Test
+    public void testisSingle3() {
+        assertEval("is.single(NULL)");
+    }
+
+    @Test
+    public void testisSingle4() {
+        assertEval("is.single(+)");
+    }
+}
-- 
GitLab