use of suite.node.Node in project suite by stupidsing.
the class Fractional method format.
public Node format(Fract<I> fract) {
OpGroup add = ex.add;
OpGroup mul = ex.mul;
Fun2<I, I, Node> f = (n, d) -> {
Node i0 = format_.apply(n);
Node i1 = format_.apply(d);
return mul.apply(i0, mul.inverse(i1));
};
I n_ = fract.t0;
I d_ = fract.t1;
I nn = neg_.apply(n_);
if (0 <= sgn_.apply(n_))
return f.apply(n_, d_);
else
return add.inverse(f.apply(nn, d_));
}
use of suite.node.Node in project suite by stupidsing.
the class Formatter method format.
/**
* Converts a node to its string representation.
*
* @param node
* Node to be converted.
* @param parentPrec
* Minimum operator precedence without adding parentheses.
*/
private void format(Node node0, int parentPrec) {
Node node = node0.finalNode();
Integer objectId = System.identityHashCode(node);
// avoids infinite recursion if object is recursive
if (set.add(objectId)) {
format_(node, parentPrec);
set.remove(objectId);
} else
sb.append("<<recurse>>");
}
use of suite.node.Node in project suite by stupidsing.
the class Formatter method format_.
private void format_(Node node, int parentPrec) {
new //
SwitchNode<Node>(//
node).doIf(Atom.class, n -> {
sb.append(quoteAtomIfRequired(n.name));
}).doIf(Data.class, n -> {
Object data = n.data;
if (data instanceof Chars)
sb.append("Chars<" + quoteStringIfRequired(data.toString()) + ">");
else if (data instanceof Node)
sb.append("Data<" + data.toString() + ">");
else
sb.append("Data<" + data.getClass().getSimpleName() + ">");
}).doIf(Dict.class, n -> {
sb.append("dict<");
for (Entry<Node, Reference> e : n.map.entrySet()) {
format(e.getKey(), TermOp.getLeftPrec(TermOp.AND___));
sb.append(":");
format(e.getValue(), TermOp.getLeftPrec(TermOp.AND___));
sb.append(",");
}
sb.append(">");
}).doIf(Int.class, n -> {
sb.append(n.number);
}).doIf(Reference.class, n -> {
sb.append(n.name());
}).doIf(Str.class, n -> {
sb.append(quoteStringIfRequired(n.value));
}).doIf(Tree.class, n -> {
formatTree(parentPrec, n);
}).doIf(Tuple.class, n -> {
sb.append("tuple<");
for (Node n_ : n.nodes) {
format(n_, TermOp.getLeftPrec(TermOp.AND___));
sb.append(", ");
}
sb.append(">");
}).doIf(Node.class, n -> {
sb.append(n.getClass().getSimpleName() + '@' + Integer.toHexString(System.identityHashCode(n)));
}).nonNullResult();
}
use of suite.node.Node in project suite by stupidsing.
the class Grapher method graph_.
private int graph_(Map<IdentityKey<Node>, Integer> ids, Node node) {
IdentityKey<Node> key = IdentityKey.of(node);
Integer id = ids.get(key);
if (id == null) {
ids.put(key, id = gns.size());
gns.add(null);
NodeRead nr = NodeRead.of(node);
List<IntIntPair> children = //
Read.from(//
nr.children).map(//
p -> IntIntPair.of(graph_(ids, p.t0), graph_(ids, p.t1))).toList();
gns.set(id, new GN(nr.type, nr.terminal, nr.op, children));
}
return id;
}
use of suite.node.Node in project suite by stupidsing.
the class Lister method leaves.
private Streamlet<IList<Node>> leaves(Node node, IList<Node> prefix) {
NodeRead nr = NodeRead.of(node);
Streamlet<IList<Node>> st;
if (nr.type == ReadType.TUPLE)
st = //
Read.from(//
nr.children).index().map(//
(i, p) -> leaves(p.t1, IList.cons(Int.of(i), prefix))).collect(As::concat);
else if (nr.type != ReadType.TERM)
st = Read.from(nr.children).concatMap(p -> leaves(p.t1, IList.cons(p.t0, prefix)));
else
st = Read.from(List.of(IList.cons(nr.terminal, prefix)));
if (nr.op != null)
st = st.cons(IList.cons(Atom.of(nr.op.toString()), prefix));
return st;
}
Aggregations