use of suite.node.io.Rewrite_.NodeWrite in project suite by stupidsing.
the class ReversePolish method fromRpn.
public Node fromRpn(Reader reader) throws IOException {
BufferedReader br = new BufferedReader(reader);
Map<String, Reference> references = new HashMap<>();
Deque<Node> deque = new ArrayDeque<>();
br.lines().filter(elem -> !elem.isEmpty()).forEach(elem -> {
char type = elem.charAt(0);
String s = elem.substring(1);
Node n;
if (type == '\\')
n = Atom.of(s);
else if (type == '^') {
String[] a = s.split(":");
int size = Integer.valueOf(a[3]);
List<Pair<Node, Node>> children = new ArrayList<>();
for (int i = 0; i < size; i++) {
Node key = deque.pop();
Node value = deque.pop();
children.add(Pair.of(key, value));
}
n = new //
NodeWrite(//
ReadType.valueOf(a[0]), //
!String_.equals(a[1], "null") ? Suite.parse(a[1]) : null, //
TermOp.valueOf(a[2]), children).node;
// n = Suite.parse(s);
} else if (type == 'i')
n = Int.of(Integer.parseInt(s));
else if (type == 'r')
n = references.computeIfAbsent(s, key -> new Reference());
else if (type == 't') {
TermOp op = TermOp.valueOf(s);
Node left = deque.pop();
Node right = deque.pop();
n = Tree.of(op, left, right);
} else
n = Fail.t("RPN conversion error: " + elem);
deque.push(n);
});
return deque.pop();
}
Aggregations