Search in sources :

Example 31 with Reference

use of suite.node.Reference in project suite by stupidsing.

the class Grapher method load.

public void load(DataInputStream dis) throws IOException {
    int size = dis.readInt();
    id = dis.readInt();
    for (int index = 0; index < size; index++) {
        ReadType type = ReadType.of(dis.readByte());
        Node terminal;
        Operator op;
        List<IntIntPair> children = new ArrayList<>();
        if (type == ReadType.TERM) {
            char ch = (char) dis.readByte();
            switch(ch) {
                case 'a':
                    terminal = Atom.of(dis.readUTF());
                    break;
                case 'i':
                    terminal = Int.of(dis.readInt());
                    break;
                case 'r':
                    terminal = new Reference();
                    break;
                case 's':
                    terminal = new Str(dis.readUTF());
                    break;
                default:
                    terminal = Fail.t("unknown type " + ch);
            }
        } else
            terminal = null;
        if (type == ReadType.TREE) {
            op = TermOp.find(dis.readUTF());
            children.add(IntIntPair.of(0, dis.readInt() + index));
            children.add(IntIntPair.of(0, dis.readInt() + index));
        } else
            op = null;
        if (type == ReadType.DICT || type == ReadType.TUPLE) {
            int size1 = dis.readInt();
            for (int i = 0; i < size1; i++) {
                int i0 = type != ReadType.DICT ? 0 : dis.readInt() + index;
                int i1 = dis.readInt() + index;
                children.add(IntIntPair.of(i0, i1));
            }
        }
        gns.add(new GN(type, terminal, op, children));
    }
}
Also used : Str(suite.node.Str) ReadType(suite.node.io.Rewrite_.ReadType) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Example 32 with Reference

use of suite.node.Reference in project suite by stupidsing.

the class Grapher method ungraph_.

private Node ungraph_(int id) {
    int size = gns.size();
    List<Node> nodes = // 
    Read.from(// 
    gns).map(gn -> {
        switch(gn.type) {
            case DICT:
                return new Dict();
            case TERM:
                return gn.terminal;
            case TREE:
                return Tree.of(gn.op, null, null);
            case TUPLE:
                return Tuple.of(new Node[gn.children.size()]);
            default:
                return Fail.t();
        }
    }).toList();
    for (int i = 0; i < size; i++) {
        GN gn = gns.get(i);
        Node node = nodes.get(i);
        List<Pair<Node, Node>> children = Read.from(gn.children).map(p -> Pair.of(nodes.get(p.t0), nodes.get(p.t1))).toList();
        switch(gn.type) {
            case DICT:
                ((Dict) node).map.putAll(Read.from2(children).mapValue(Reference::of).collect(As::map));
                break;
            case TERM:
                break;
            case TREE:
                Tree tree = (Tree) node;
                Tree.forceSetLeft(tree, children.get(0).t1);
                Tree.forceSetRight(tree, children.get(1).t1);
                break;
            case TUPLE:
                Node[] list = ((Tuple) node).nodes;
                for (int j = 0; j < children.size(); j++) list[j] = children.get(j).t1;
        }
    }
    return nodes.get(id);
}
Also used : DataInputStream(java.io.DataInputStream) Read(suite.streamlet.Read) HashMap(java.util.HashMap) Deque(java.util.Deque) ArrayList(java.util.ArrayList) Node(suite.node.Node) HashSet(java.util.HashSet) ProverConstant(suite.lp.doer.ProverConstant) DataOutputStream(java.io.DataOutputStream) Map(java.util.Map) IdentityKey(suite.adt.IdentityKey) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Reference(suite.node.Reference) Trail(suite.lp.Trail) IntIntPair(suite.primitive.adt.pair.IntIntPair) Set(java.util.Set) IOException(java.io.IOException) NodeRead(suite.node.io.Rewrite_.NodeRead) NodeHead(suite.node.io.Rewrite_.NodeHead) IntObjMap(suite.primitive.adt.map.IntObjMap) Tree(suite.node.Tree) Objects(java.util.Objects) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Entry(java.util.Map.Entry) As(suite.streamlet.As) ArrayDeque(java.util.ArrayDeque) Int(suite.node.Int) Fail(suite.util.Fail) Str(suite.node.Str) ReadType(suite.node.io.Rewrite_.ReadType) Dict(suite.node.Dict) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Tuple(suite.node.Tuple) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 33 with Reference

use of suite.node.Reference 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)

Example 34 with Reference

use of suite.node.Reference in project suite by stupidsing.

the class InstructionExecutor method yawnThunk_.

private Node yawnThunk_(Thunk thunk0) {
    Frame f0 = new Frame(null, 2);
    f0.registers[0] = thunk0;
    Activation current = new Activation(f0, yawnEntryPoint, null);
    Node[] stack = new Node[Suite.stackSize];
    int ip = 0, sp = 0;
    Node returnValue = null;
    Exec exec = new Exec();
    exec.stack = stack;
    Comparer comparer = comparer();
    Tree tree;
    while (true) try {
        Frame frame = current.frame;
        Node[] regs = frame != null ? frame.registers : null;
        Instruction insn = instructions[ip = current.ip++];
        Thunk thunk;
        TermOp op;
        int i;
        switch(insn.insn) {
            case ASSIGNCONST___:
                regs[insn.op0] = constantPool.get(insn.op1);
                break;
            case ASSIGNFRAMEREG:
                i = insn.op1;
                while (i++ < 0) frame = frame.previous;
                regs[insn.op0] = frame.registers[insn.op2];
                break;
            case ASSIGNINT_____:
                regs[insn.op0] = number(insn.op1);
                break;
            case ASSIGNRESULT__:
                regs[insn.op0] = returnValue;
                break;
            case ASSIGNTHUNK___:
                regs[insn.op0] = new Thunk(frame, insn.op1);
                break;
            case ASSIGNTHUNKRES:
                regs[insn.op0] = returnValue;
                thunk = (Thunk) regs[insn.op1];
                // facilitates garbage collection
                thunk.frame = null;
                thunk.result = returnValue;
                break;
            case CALL__________:
                current = new Activation(frame, insn.op0, current);
                break;
            case CALLTHUNK_____:
                thunk = (Thunk) regs[insn.op0];
                if (thunk.result == null)
                    current = new Activation(thunk, current);
                else
                    returnValue = thunk.result;
                break;
            case ENTER_________:
                AnalyzedFrame af = analyzer.getFrame(ip);
                Frame parent = af.isRequireParent() ? frame : null;
                Instruction frameBegin = instructions[af.getFrameBeginIp()];
                current.frame = new Frame(parent, frameBegin.op0);
                break;
            case ERROR_________:
                Fail.t("error termination");
            case EVALADD_______:
                regs[insn.op0] = number(i(regs[insn.op1]) + i(regs[insn.op2]));
                break;
            case EVALDIV_______:
                regs[insn.op0] = number(i(regs[insn.op1]) / i(regs[insn.op2]));
                break;
            case EVALEQ________:
                i = comparer.compare(regs[insn.op1], regs[insn.op2]);
                regs[insn.op0] = atom(i == 0);
                break;
            case EVALLE________:
                i = comparer.compare(regs[insn.op1], regs[insn.op2]);
                regs[insn.op0] = atom(i <= 0);
                break;
            case EVALLT________:
                i = comparer.compare(regs[insn.op1], regs[insn.op2]);
                regs[insn.op0] = atom(i < 0);
                break;
            case EVALNE________:
                i = comparer.compare(regs[insn.op1], regs[insn.op2]);
                regs[insn.op0] = atom(i != 0);
                break;
            case EVALMOD_______:
                regs[insn.op0] = number(i(regs[insn.op1]) % i(regs[insn.op2]));
                break;
            case EVALMUL_______:
                regs[insn.op0] = number(i(regs[insn.op1]) * i(regs[insn.op2]));
                break;
            case EVALSUB_______:
                regs[insn.op0] = number(i(regs[insn.op1]) - i(regs[insn.op2]));
                break;
            case EXIT__________:
                return returnValue;
            case FORMTREE0_____:
                Node left = regs[insn.op0];
                Node right = regs[insn.op1];
                insn = instructions[current.ip++];
                op = TermOp.find(((Atom) constantPool.get(insn.op0)).name);
                regs[insn.op1] = Tree.of(op, left, right);
                break;
            case FRAMEBEGIN____:
            case FRAMEEND______:
                break;
            case IFFALSE_______:
                if (regs[insn.op0] != Atom.TRUE)
                    current.ip = insn.op1;
                break;
            case IFNOTCONS_____:
                if ((tree = Tree.decompose(regs[insn.op0], TermOp.OR____)) != null) {
                    stack[sp++] = tree.getLeft();
                    stack[sp++] = tree.getRight();
                } else
                    current.ip = insn.op1;
                break;
            case IFNOTPAIR_____:
                if ((tree = Tree.decompose(regs[insn.op0], TermOp.AND___)) != null) {
                    stack[sp++] = tree.getLeft();
                    stack[sp++] = tree.getRight();
                } else
                    current.ip = insn.op1;
                break;
            case IFNOTEQUALS___:
                if (regs[insn.op1] != regs[insn.op2])
                    current.ip = insn.op0;
                break;
            case JUMP__________:
                current.ip = insn.op0;
                break;
            case JUMPCLOSURE___:
                thunk = (Thunk) regs[insn.op0];
                current = current.previous;
                if (thunk.result == null)
                    current = new Activation(thunk, current);
                else
                    returnValue = thunk.result;
                break;
            case LEAVE_________:
                current.frame = current.frame.previous;
                break;
            case LOGREG________:
                LogUtil.info(regs[insn.op0].toString());
                break;
            case NEWNODE_______:
                regs[insn.op0] = new Reference();
                break;
            case PUSH__________:
                stack[sp++] = regs[insn.op0];
                break;
            case POP___________:
                regs[insn.op0] = stack[--sp];
                break;
            case POPANY________:
                --sp;
                break;
            case REMARK________:
                break;
            case RETURN________:
                current = current.previous;
                break;
            case SETRESULT_____:
                returnValue = regs[insn.op0];
                break;
            case TOP___________:
                regs[insn.op0] = stack[sp + insn.op1];
                break;
            default:
                exec.current = current;
                exec.sp = sp;
                handle(exec, insn);
                current = exec.current;
                sp = exec.sp;
        }
    } catch (Exception ex) {
        Fail.t("at IP = " + ip, ex);
    }
}
Also used : AnalyzedFrame(suite.instructionexecutor.InstructionAnalyzer.AnalyzedFrame) Frame(suite.instructionexecutor.InstructionUtil.Frame) Reference(suite.node.Reference) Node(suite.node.Node) Activation(suite.instructionexecutor.InstructionUtil.Activation) Instruction(suite.instructionexecutor.InstructionUtil.Instruction) Thunk(suite.instructionexecutor.InstructionUtil.Thunk) TermOp(suite.node.io.TermOp) Comparer(suite.node.util.Comparer) Tree(suite.node.Tree) AnalyzedFrame(suite.instructionexecutor.InstructionAnalyzer.AnalyzedFrame)

Example 35 with Reference

use of suite.node.Reference in project suite by stupidsing.

the class InstructionExtractor method getRegisterNumber.

private int getRegisterNumber(List<Node> rs, int index) {
    if (index < rs.size()) {
        Node node = rs.get(index).finalNode();
        Tree tree;
        if (node instanceof Int)
            return ((Int) node).number;
        else if (node instanceof Reference) {
            // transient register
            // allocates new register in current local frame
            Instruction frameBegin = frameBegins.getFirst();
            int registerNumber = frameBegin.op0++;
            Binder.bind(node, Int.of(registerNumber), trail);
            return registerNumber;
        } else if ((tree = Tree.decompose(node, TermOp.COLON_)) != null) {
            Node key = tree.getLeft(), value = tree.getRight();
            if (key == KEYC)
                return allocateInPool(value);
            else if (key == KEYL)
                return ipByLabelId.get(IdentityKey.of(value));
            else if (key == KEYR)
                return 0;
        }
        return Fail.t("cannot parse instruction " + rs.get(0) + " operand " + node);
    } else
        return 0;
}
Also used : Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Instruction(suite.instructionexecutor.InstructionUtil.Instruction) Int(suite.node.Int)

Aggregations

Reference (suite.node.Reference)38 Node (suite.node.Node)31 Tree (suite.node.Tree)21 Atom (suite.node.Atom)17 ArrayList (java.util.ArrayList)15 Pair (suite.adt.pair.Pair)13 Int (suite.node.Int)13 List (java.util.List)11 Generalizer (suite.lp.doer.Generalizer)10 TermOp (suite.node.io.TermOp)10 Read (suite.streamlet.Read)10 Trail (suite.lp.Trail)9 Dict (suite.node.Dict)9 Tuple (suite.node.Tuple)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 Binder (suite.lp.doer.Binder)7 Str (suite.node.Str)7 Fail (suite.util.Fail)7 Suite (suite.Suite)6