Skip to content
Snippets Groups Projects
Commit 2078b8ee authored by Julien Lopez's avatar Julien Lopez
Browse files

Factorize code between translation of lambda and function

parent e12b52fd
No related branches found
No related tags found
No related merge requests found
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment