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

- Remove name for TruffleNode

- Minor fixes
parent 8537e722
No related branches found
No related tags found
No related merge requests found
......@@ -42,7 +42,7 @@ public final class QIRLambda extends QIRNode {
@Override
public final String toString() {
return (funName != null ? funName : "lambda") + " " + var + "-> {" + body + "}";
return (funName != null ? funName : "lambda") + " " + var + " -> {" + body + "}";
}
@Override
......
......@@ -9,26 +9,20 @@ import qir.util.IQIRVisitor;
* database representation.
*/
public final class QIRTruffleNode extends QIRNode {
private final String name;
private final String code;
public QIRTruffleNode(final SourceSection source, final String name, final String code) {
public QIRTruffleNode(final SourceSection source, final String code) {
super(source);
this.name = name;
this.code = code;
}
public final String getName() {
return name;
}
public final String getCode() {
return code;
}
@Override
public final String toString() {
return "TruffleNode(" + name + ")";
return "TruffleNode(" + code + ")";
}
@Override
......
......@@ -6,12 +6,12 @@ import qir.ast.*;
import qir.util.*;
/**
* The {@link QIRNot} represents an expression that is true if and only if the {@link #left}
* expression and the {@link #right} expression are true.
* The {@link QIRNot} represents an expression that is true if and only if the {@link #child} is
* false.
*/
public final class QIRNot extends QIRNode {
/**
* The left side of the {@link QIRNot} operator.
* The child of the {@link QIRNot} operator.
*/
private final QIRNode child;
......
......@@ -32,7 +32,14 @@ public final class QIRSQLVisitor implements IQIRVisitor<String> {
@Override
public final String visit(final QIRProject qirProject) {
return "select " + qirProject.getFormatter().accept(this) + " " + qirProject.getChild().accept(this);
final QIRNode formatter = qirProject.getFormatter();
final String f;
if (formatter instanceof QIRLambda)
f = ((QIRLambda) formatter).getBody().accept(this);
else
f = formatter.accept(this);
return "select " + f + " " + qirProject.getChild().accept(this);
}
@Override
......@@ -42,18 +49,39 @@ public final class QIRSQLVisitor implements IQIRVisitor<String> {
@Override
public final String visit(final QIRSelect qirSelect) {
return qirSelect.getChild().accept(this) + " where " + qirSelect.getFilter().accept(this);
final QIRNode filter = qirSelect.getFilter();
final String f;
if (filter instanceof QIRLambda)
f = ((QIRLambda) filter).getBody().accept(this);
else
f = filter.accept(this);
return qirSelect.getChild().accept(this) + " where " + f;
}
// TODO: Do something with "eq" attribute of QIRGroup.
@Override
public final String visit(final QIRGroup qirGroup) {
return qirGroup.getChild().accept(this) + " group by " + qirGroup.getGroup().accept(this);
final QIRNode group = qirGroup.getGroup();
final String g;
if (group instanceof QIRLambda)
g = ((QIRLambda) group).getBody().accept(this);
else
g = group.accept(this);
return qirGroup.getChild().accept(this) + " group by " + g;
}
@Override
public final String visit(final QIROrder qirOrder) {
return qirOrder.getChild().accept(this) + " order by " + qirOrder.getOrder().accept(this);
final QIRNode order = qirOrder.getOrder();
final String o;
if (order instanceof QIRLambda)
o = ((QIRLambda) order).getBody().accept(this);
else
o = order.accept(this);
return qirOrder.getChild().accept(this) + " order by " + o;
}
// TODO: Do something with "filter" attribute of QIRJoin.
......@@ -66,12 +94,19 @@ public final class QIRSQLVisitor implements IQIRVisitor<String> {
@Override
public final String visit(final QIRLimit qirLimit) {
return qirLimit.getChild().accept(this) + " limit " + qirLimit.getLimit().accept(this);
final QIRNode limit = qirLimit.getLimit();
final String l;
if (limit instanceof QIRLambda)
l = ((QIRLambda) limit).getBody().accept(this);
else
l = limit.accept(this);
return qirLimit.getChild().accept(this) + " limit " + l;
}
@Override
public final String visit(final QIRLambda qirLambda) {
return qirLambda.getBody().accept(this);
throw new QIRException("Lambdas are unsupported in QIR SQL.");
}
@Override
......@@ -79,8 +114,11 @@ public final class QIRSQLVisitor implements IQIRVisitor<String> {
final Deque<String> args = new ArrayDeque<>();
QIRNode curr = qirApply;
for (; curr instanceof QIRApply; curr = ((QIRApply) curr).getLeft())
args.push(((QIRApply) curr).getRight().accept(this));
if (qirApply.getRight() != null)
for (; curr instanceof QIRApply; curr = ((QIRApply) curr).getLeft())
args.push(((QIRApply) curr).getRight().accept(this));
else
curr = qirApply.getLeft();
if (curr instanceof QIRTruffleNode)
return "truffle.executeapply(" + curr.accept(this) + ", array[" + args.stream().map(arg -> "truffle.translate(" + arg + ")").collect(Collectors.joining(", ")) + "])";
return curr.accept(this) + "(" + args.stream().collect(Collectors.joining(", ")) + ")";
......
......@@ -67,14 +67,13 @@ public final class QIRGreedyReduceVisitor implements IQIRVisitor<QIRNode> {
if (left instanceof QIRLambda) {
final QIRLambda fun = (QIRLambda) left;
final QIRVariable var = fun.getVar();
if (var == null) {
if (var == null)
return fun.getBody();
}
final String varName = var.getId();
env.put(varName, right);
final QIRLambda result = (QIRLambda) fun.accept(this);
final QIRLambda res = (QIRLambda) fun.accept(this);
env.remove(varName);
return result.getBody();
return res.getBody();
}
return new QIRApply(qirApply.getSourceSection(), left, right);
}
......@@ -151,10 +150,7 @@ public final class QIRGreedyReduceVisitor implements IQIRVisitor<QIRNode> {
@Override
public final QIRNode visit(final QIRVariable qirVariable) {
if (env.containsKey(qirVariable.getId())) {
return env.get(qirVariable.getId());
}
return qirVariable;
return env.getOrDefault(qirVariable.getId(), qirVariable);
}
@Override
......@@ -190,14 +186,12 @@ public final class QIRGreedyReduceVisitor implements IQIRVisitor<QIRNode> {
final QIRNode body = fun.getBody();
final String varName = fun.getVar().getId();
env.put(varName, value);
if (body instanceof QIRLambda) {
if (body instanceof QIRLambda)
env.put(((QIRLambda) body).getVar().getId(), tail);
}
final QIRNode result = fun.accept(this);
env.remove(varName);
if (body instanceof QIRLambda) {
if (body instanceof QIRLambda)
env.remove(((QIRLambda) body).getVar().getId());
}
return result;
}
SourceSection src = SourceSection.createUnavailable("QIR", "Apply");
......@@ -224,9 +218,8 @@ public final class QIRGreedyReduceVisitor implements IQIRVisitor<QIRNode> {
while (current instanceof QIRTcons) {
final QIRTcons currentTuple = (QIRTcons) current;
if (colName == currentTuple.getId()) {
if (colName == currentTuple.getId())
return currentTuple.getValue();
}
current = currentTuple.getTail();
}
return new QIRTdestr(qirTdestr.getSourceSection(), tuple, colName);
......
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