From 2078b8eeca14d7009b2836be5fdcd41fef73e279 Mon Sep 17 00:00:00 2001
From: Julien Lopez <julien.lopez@lri.fr>
Date: Mon, 23 Jan 2017 16:30:40 +0100
Subject: [PATCH] Factorize code between translation of lambda and function

---
 .../r/nodes/qirinterface/QIRInterface.java    | 55 +++++++------------
 1 file changed, 21 insertions(+), 34 deletions(-)

diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRInterface.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRInterface.java
index d7c52c80bf..43547c55d2 100644
--- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRInterface.java
+++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRInterface.java
@@ -27,7 +27,6 @@ import java.io.IOException;
 import java.io.Reader;
 import java.math.BigInteger;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -209,24 +208,6 @@ public final class QIRInterface {
             return (Boolean) value ? QIRBoolean.qirTrue : QIRBoolean.qirFalse;
         if (value instanceof String)
             return new QIRString(src, (String) value);
-        if (value instanceof FunctionExpressionNode) {
-            final FunctionExpressionNode fun = (FunctionExpressionNode) value;
-            if (fun.getCallTarget() == null)
-                return new QIRBuiltin(src, fun.getSyntaxDebugName());
-            final String funName = fun.getSyntaxDebugName();
-            try {
-                QIRNode res = ((FunctionDefinitionNode) fun.getCallTarget().getRootNode()).getBody().accept(QIRTranslateVisitor.instance);
-                final String[] args = ((FunctionDefinitionNode) fun.getCallTarget().getRootNode()).getSignature().getNames();
-                for (int i = 0; i < args.length; i++)
-                    res = new QIRLambda(src, funName, new QIRVariable(null, args[i], null), res);
-                return res;
-            } catch (UnsupportedOperationException e) {
-                final SourceSection funSrc = fun.getCallTarget().getRootNode().getSourceSection();
-                final String program = funSrc.getCode() + "\n" + funName;
-                // TODO: Handle function dependencies
-                return new QIRTruffleNode(funSrc, program);
-            }
-        }
         if (value instanceof REnvironment) {
             REnvironment env = (REnvironment) value;
             final Object queryId = env.get("queryId");
@@ -247,6 +228,9 @@ public final class QIRInterface {
                 tuple = new QIRTcons(src, (String) x, RToQIRType(src, env.get((String) x)), tuple);
             return tuple;
         }
+        if (value instanceof FunctionExpressionNode) {
+            return RFunctionToQIRType(src, ((FunctionExpressionNode) value).getSyntaxDebugName(), (FunctionDefinitionNode) (((FunctionExpressionNode) value).getCallTarget().getRootNode()));
+        }
         if (value instanceof RFunction) {
             final RFunction fun = (RFunction) value;
             if (fun.isBuiltin())
@@ -268,22 +252,25 @@ public final class QIRInterface {
                     default:
                         throw new RuntimeException("Unsupported value: " + value + " : " + value.getClass());
                 }
-            try {
-                final FunctionDefinitionNode rootNode = (FunctionDefinitionNode) fun.getRootNode();
-                QIRNode res = rootNode.getBody().accept(QIRTranslateVisitor.instance);
-                final String[] args = Arrays.asList(rootNode.argSourceSections).stream().map(arg -> arg.getCode()).toArray(size -> new String[size]);
-                final String funName = fun.getName();
-                if (args.length == 0)
-                    return new QIRLambda(src, funName, null, res);
-                for (int i = 0; i < args.length; i++)
-                    res = new QIRLambda(src, funName, new QIRVariable(null, args[i], null), res);
-                return res;
-            } catch (UnsupportedOperationException e) {
-                final SourceSection funSrc = fun.getRootNode().getSourceSection();
-                // TODO: Handle dependencies
-                return new QIRTruffleNode(funSrc, funSrc.getCode());
-            }
+            return RFunctionToQIRType(src, fun.getName(), (FunctionDefinitionNode) fun.getRootNode());
         }
         throw new RuntimeException("Unsupported value: " + value);
     }
+
+    private static final <T> QIRNode RFunctionToQIRType(final SourceSection src, final String funName, final FunctionDefinitionNode fun) {
+        try {
+            QIRNode res = ((FunctionDefinitionNode) fun.getCallTarget().getRootNode()).getBody().accept(QIRTranslateVisitor.instance);
+            final String[] args = ((FunctionDefinitionNode) fun.getCallTarget().getRootNode()).getSignature().getNames();
+
+            if (args.length == 0)
+                return new QIRLambda(src, funName, null, res);
+            for (int i = 0; i < args.length; i++)
+                res = new QIRLambda(src, funName, new QIRVariable(null, args[i], null), res);
+            return res;
+        } catch (UnsupportedOperationException e) {
+            final SourceSection funSrc = fun.getCallTarget().getRootNode().getSourceSection();
+            // TODO: Handle dependencies
+            return new QIRTruffleNode(funSrc, funSrc.getCode());
+        }
+    }
 }
\ No newline at end of file
-- 
GitLab