Search in sources :

Example 1 with NodeWrite

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();
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) NodeWrite(suite.node.io.Rewrite_.NodeWrite) IOException(java.io.IOException) HashMap(java.util.HashMap) NodeRead(suite.node.io.Rewrite_.NodeRead) Reader(java.io.Reader) Deque(java.util.Deque) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) String_(suite.util.String_) StringReader(java.io.StringReader) Rethrow(suite.util.Rethrow) Atom(suite.node.Atom) Map(java.util.Map) BufferedReader(java.io.BufferedReader) ArrayDeque(java.util.ArrayDeque) Int(suite.node.Int) Fail(suite.util.Fail) ReadType(suite.node.io.Rewrite_.ReadType) HashMap(java.util.HashMap) Reference(suite.node.Reference) Node(suite.node.Node) ArrayDeque(java.util.ArrayDeque) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Suite (suite.Suite)1 Pair (suite.adt.pair.Pair)1 Atom (suite.node.Atom)1 Int (suite.node.Int)1 Node (suite.node.Node)1 Reference (suite.node.Reference)1 Tree (suite.node.Tree)1 NodeRead (suite.node.io.Rewrite_.NodeRead)1 NodeWrite (suite.node.io.Rewrite_.NodeWrite)1 ReadType (suite.node.io.Rewrite_.ReadType)1