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

Handle user-defined functions with no dependencies

parent 43beeb01
No related branches found
No related tags found
No related merge requests found
......@@ -93,7 +93,7 @@ public final class FunctionDefinitionNode extends RRootNode implements RSyntaxNo
private String name;
private boolean instrumented = false;
private SourceSection sourceSectionR;
private final SourceSection[] argSourceSections;
public final SourceSection[] argSourceSections;
@Child private RNode saveArguments;
@Child private FrameSlotNode onExitSlot;
......
......@@ -27,6 +27,7 @@ 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.Map;
import java.util.Properties;
......@@ -41,6 +42,7 @@ import com.oracle.truffle.r.runtime.RRuntime;
import com.oracle.truffle.r.runtime.context.RContext;
import com.oracle.truffle.r.runtime.data.RFunction;
import com.oracle.truffle.r.runtime.env.REnvironment;
import qir.ast.*;
import qir.ast.data.*;
import qir.ast.value.*;
......@@ -93,7 +95,6 @@ public final class QIRInterface {
for (final QIRVariable fv : fvs.values()) {
final String varName = fv.getId();
final FrameSlot varSlot = frame.getFrameDescriptor().findFrameSlot(varName);
// TODO: Handle functions other than builtins
query = new QIRApply(dummy, new QIRLambda(dummy, null, new QIRVariable(dummy, varName, varSlot), query),
RToQIRType(fv.sourceSection, varSlot != null ? frame.getValue(varSlot) : RContext.lookupBuiltin(varName)));
}
......@@ -232,16 +233,33 @@ public final class QIRInterface {
tuple = new QIRTcons(src, (String) x, RToQIRType(src, env.get((String) x)), tuple);
return tuple;
}
if (value instanceof RFunction && ((RFunction) value).isBuiltin()) {
switch (((RFunction) value).getName()) {
case "new.env":
return new QIRLambda(null, "new.env", new QIRVariable(null, "_", null), QIRTnil.getInstance());
case "return":
case "(":
final QIRVariable x = new QIRVariable(null, "x", null);
return new QIRLambda(null, "identity", x, x);
default:
throw new RuntimeException("Unsupported value: " + value + " : " + value.getClass());
if (value instanceof RFunction) {
final RFunction fun = (RFunction) value;
if (fun.isBuiltin())
switch (fun.getName()) {
case "new.env":
return new QIRLambda(null, "new.env", new QIRVariable(null, "_", null), QIRTnil.getInstance());
case "return":
case "(":
final QIRVariable x = new QIRVariable(null, "x", null);
return new QIRLambda(null, "identity", x, x);
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());
}
}
throw new RuntimeException("Unsupported value: " + value);
......
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