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 d7c52c80bf2460ce581b2521aec8bfeb8e36be27..43547c55d2fb7db919fb3e94fec92e999a76cc2a 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