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;
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations