use of suite.node.Node in project suite by stupidsing.
the class Rewrite_ method map.
public static Node map(Node node, Iterate<Node> fun) {
NodeRead nr = NodeRead.of(node);
List<Pair<Node, Node>> children1 = new ArrayList<>();
boolean isSame = true;
for (Pair<Node, Node> pair : nr.children) {
Node child0 = pair.t1;
Node childx = fun.apply(child0);
if (child0 != childx) {
isSame = false;
children1.add(Pair.of(pair.t0, childx));
} else
children1.add(pair);
}
if (isSame)
return node;
else
return new NodeWrite(nr.type, nr.terminal, nr.op, children1).node;
}
use of suite.node.Node in project suite by stupidsing.
the class PrettyPrinter method prettyPrint_.
// op0 for avoiding unnecessary indenting; prec0 for parenthesizing
private void prettyPrint_(Node node, Operator op0, int prec0) {
int x = getX(), y = getY();
int length = lengthEstimator.getEstimatedLength(node);
// line too long?
if (node instanceof Tree) {
Tree tree = (Tree) node;
Operator op = tree.getOperator();
int prec = op.getPrecedence();
boolean isNeedPars = prec <= prec0;
int parsIndent = 0, parsIndent0 = 0;
if (isNeedPars) {
parsIndent = currentLineIndent;
parsIndent0 = incrementIndent();
append("(");
}
if (lineLength < x + length)
if (isLookingLikeList(op, node))
prettyPrintList(op, node);
else {
Node left = tree.getLeft();
Node right = tree.getRight();
Assoc assoc = op.getAssoc();
int leftPrec = prec - (assoc == Assoc.LEFT ? 1 : 0);
int rightPrec = prec - (assoc == Assoc.RIGHT ? 1 : 0);
if (op == TermOp.BRACES)
leftPrec = rightPrec = 0;
Tree tree1 = Tree.decompose(right, op);
Node r0 = tree1 != null ? tree1.getLeft() : null;
int es0 = lengthEstimator.getEstimatedLength(left);
int es1 = r0 != null ? lengthEstimator.getEstimatedLength(r0) : lineLength;
int opLength = op.getName().length();
// breaks "a + b + xxx" in the second operator
if (//
assoc == Assoc.RIGHT && //
x + es0 + es1 + opLength < lineLength && r0 != preferLineBreakBeforeKeyword) {
prettyPrint_(left, op, leftPrec);
OperatorPosition opPos = appendOperator(op);
prettyPrint_(right, op, rightPrec);
closeBraces(op, opPos);
} else {
// breaks after the operator
boolean isIncRightIndent = op != op0;
int indent0 = 0;
prettyPrint_(left, op, leftPrec);
if (isIncRightIndent)
indent0 = incrementIndent();
OperatorPosition opPos;
if (getLineSize() + lengthEstimator.getEstimatedLength(right) < squeezeLineLength)
opPos = appendOperator(op);
else
opPos = appendOperatorLineFeed(op);
prettyPrint_(right, op, rightPrec);
closeBraces(op, opPos);
if (isIncRightIndent)
revertIndent(indent0);
}
}
else
append(Formatter.dump(node));
if (isNeedPars) {
if (y != getY())
nl(parsIndent);
append(")");
revertIndent(parsIndent0);
}
} else {
if (node == lineBreakBeforeKeyword && !isLineBegin())
nl();
// space sufficient
append(Formatter.dump(node));
}
}
use of suite.node.Node in project suite by stupidsing.
the class Chr method chrIf.
private Streamlet<State> chrIf(Streamlet<State> states, Trail trail, Node if_) {
Prototype prototype = Prototype.of(if_);
Fun<State, Streamlet<State>> fun = state -> {
ISet<Node> facts = getFacts(state, prototype);
Predicate<Node> bindFun = bindFun(trail, if_);
return facts.streamlet().filter(bindFun).map(node -> setFacts(state, prototype, facts.remove(node)));
};
return states.concatMap(fun);
}
use of suite.node.Node in project suite by stupidsing.
the class Chr method chr.
private Streamlet<State> chr(State state, Rule rule) {
Generalizer generalizer = new Generalizer();
Trail trail = new Trail();
Streamlet<State> states = Read.each(state);
for (Node if_ : rule.ifs) states = chrIf(states, trail, generalizer.generalize(if_));
for (Node given : rule.givens) states = chrGiven(states, trail, generalizer.generalize(given));
states = chrWhen(states, generalizer.generalize(rule.when));
for (Node then : rule.thens) states = chrThen(states, generalizer.generalize(then));
return states;
}
use of suite.node.Node in project suite by stupidsing.
the class Chr method chr.
public Collection<Node> chr(Collection<Node> facts) {
State state = new State(IMap.empty());
for (Node fact : facts) {
Prototype prototype = Prototype.of(fact);
state = setFacts(state, prototype, getFacts(state, prototype).replace(fact));
}
State state1;
while ((state1 = chr(state)) != null) state = state1;
List<Node> nodes1 = new ArrayList<>();
for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) nodes1.addAll(To.list(e.t1));
return nodes1;
}
Aggregations