Search in sources :

Example 36 with Tree

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

the class LengthEstimator method estimateLengths.

public int estimateLengths(Node node) {
    int key = getKey(node);
    Integer length = lengthByIds.get(key);
    if (length == null) {
        int len;
        if (node instanceof Tree) {
            Tree tree = (Tree) node;
            Operator op = tree.getOperator();
            int len0 = estimateLengths(tree.getLeft());
            int len1 = estimateLengths(tree.getRight());
            int opLength = op.getName().length();
            // rough estimation
            len = len0 + len1 + opLength + 2;
        } else
            len = Formatter.dump(node).length();
        length = len;
        lengthByIds.put(key, length);
    }
    return length;
}
Also used : Operator(suite.node.io.Operator) Tree(suite.node.Tree)

Example 37 with Tree

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

the class NewPrettyPrinter method format_.

private void format_(Node node, int parentPrec, String indent, String prefix) {
    Tree tree;
    if ((tree = Tree.decompose(node)) != null) {
        Operator operator = tree.getOperator();
        int prec = operator.getPrecedence();
        boolean isParenthesesRequired = operator != null ? prec <= parentPrec : false;
        String indent1 = indent + ind;
        Node[] m;
        if (isParenthesesRequired) {
            format_(node, 0, indent, concatWithSpace(prefix, "("));
            sb.append(indent + ")");
        } else if (operator == TermOp.NEXT__) {
            format_(tree.getLeft(), TermOp.getLeftPrec(operator), indent, prefix);
            sb.append(indent + "#\n");
            format_(tree.getRight(), TermOp.getRightPrec(operator), indent, "");
        } else if (operator == TermOp.IS____) {
            format_(tree.getLeft(), TermOp.getLeftPrec(operator), indent, prefix);
            format_(tree.getRight(), TermOp.getRightPrec(operator), indent1, operator.getName());
        } else if (operator == TermOp.BIGAND || operator == TermOp.BIGOR_) {
            format_(tree.getLeft(), TermOp.getLeftPrec(operator), indent, prefix);
            format_(tree.getRight(), TermOp.getRightPrec(operator), indent, operator.getName());
        } else if (operator == TermOp.AND___ || operator == TermOp.OR____) {
            format_(tree.getLeft(), prec, indent, prefix);
            node = tree.getRight();
            while ((tree = Tree.decompose(node)) != null && tree.getOperator() == operator) {
                format_(tree.getLeft(), prec, indent1, operator.getName());
                node = tree.getRight();
            }
            format_(node, prec, indent1, operator.getName());
        } else if (// 
        (m = Suite.pattern("if .0 then .1 else .2").match(node)) != null && lineLength < lengthEstimator.getEstimatedLength(node)) {
            format_(m[0], prec, indent, concatWithSpace(prefix, "if"));
            format_(m[1], prec, indent, "then");
            format_(m[2], prec, indent, "else");
        } else if (// 
        (m = Suite.pattern("not .0").match(node)) != null && lineLength < lengthEstimator.getEstimatedLength(node))
            format_(m[0], prec, indent, concatWithSpace(prefix, "not"));
        else if (// 
        (m = Suite.pattern("once .0").match(node)) != null && lineLength < lengthEstimator.getEstimatedLength(node))
            format_(m[0], prec, indent, concatWithSpace(prefix, "once"));
        else
            format_(node, indent, prefix);
    } else
        format_(node, indent, prefix);
}
Also used : Operator(suite.node.io.Operator) Node(suite.node.Node) Tree(suite.node.Tree)

Example 38 with Tree

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

the class PrettyPrinter method isLookingLikeList.

private boolean isLookingLikeList(Operator op, Node node) {
    Tree tree = Tree.decompose(node);
    if (tree != null && tree.getOperator() == op) {
        boolean isLeftAssoc = op.getAssoc() == Assoc.LEFT;
        Node child = isLeftAssoc ? tree.getLeft() : tree.getRight();
        return isLookingLikeList(op, child);
    }
    return op != TermOp.TUPLE_ && (op == TermOp.AND___ || op == TermOp.OR____ || node == Atom.NIL);
}
Also used : Node(suite.node.Node) Tree(suite.node.Tree)

Example 39 with Tree

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

the class P0Parse method expandMacros.

private Node expandMacros(Node node0) {
    class Expand {

        private IMap<Prototype, Node[]> macros;

        private Expand(IMap<Prototype, Node[]> macros) {
            this.macros = macros;
        }

        private Node expand(Node node) {
            Tree tree;
            Node[] m;
            Node[] ht;
            if ((m = Suite.pattern("expand .0 := .1 >> .2").match(node)) != null) {
                Node head = m[0];
                return new Expand(macros.put(Prototype.of(head), new Node[] { head, m[1] })).expand(m[2]);
            } else if ((ht = macros.get(Prototype.of(node))) != null) {
                Generalizer g = new Generalizer();
                Node t0_ = g.generalize(ht[0]);
                Node t1_ = g.generalize(ht[1]);
                if (Binder.bind(node, t0_, new Trail()))
                    return expand(t1_);
            }
            if ((tree = Tree.decompose(node)) != null)
                return Tree.of(tree.getOperator(), expand(tree.getLeft()), expand(tree.getRight()));
            else
                return node;
        }
    }
    return new Expand(IMap.empty()).expand(node0);
}
Also used : IMap(suite.immutable.IMap) Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) Tree(suite.node.Tree) FunpTree(suite.funp.P0.FunpTree)

Example 40 with Tree

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

Aggregations

Tree (suite.node.Tree)47 Node (suite.node.Node)36 Reference (suite.node.Reference)19 Atom (suite.node.Atom)14 Int (suite.node.Int)13 TermOp (suite.node.io.TermOp)11 Tuple (suite.node.Tuple)10 ArrayList (java.util.ArrayList)9 Pair (suite.adt.pair.Pair)9 Operator (suite.node.io.Operator)9 Read (suite.streamlet.Read)9 List (java.util.List)8 Dict (suite.node.Dict)8 Map (java.util.Map)7 Str (suite.node.Str)7 Binder (suite.lp.doer.Binder)6 HashMap (java.util.HashMap)5 Generalizer (suite.lp.doer.Generalizer)5 VariableMapper (suite.lp.sewing.VariableMapper)5 TreeUtil (suite.node.util.TreeUtil)5