use of suite.node.Tree in project suite by stupidsing.
the class InstructionExtractor method tupleToList.
private List<Node> tupleToList(Node node) {
List<Node> results = new ArrayList<>();
Tree tree;
while ((tree = Tree.decompose(node, TermOp.TUPLE_)) != null) {
results.add(tree.getLeft());
node = tree.getRight();
}
results.add(node);
return results;
}
use of suite.node.Tree in project suite by stupidsing.
the class InstructionExtractor method extractInstructions.
private void extractInstructions(Node snippet, List<List<Node>> rsList) {
Deque<Node> deque = new ArrayDeque<>();
deque.add(snippet);
Tree tree;
Node value;
while (!deque.isEmpty()) if ((tree = Tree.decompose(deque.pop(), TermOp.AND___)) != null) {
IdentityKey<Node> key = IdentityKey.of(tree);
Integer ip = ipByLabelId.get(key);
if (ip == null) {
ipByLabelId.put(key, ip = rsList.size());
List<Node> rs = tupleToList(tree.getLeft());
if (rs.get(0) == FRAME)
if ((value = label(rs.get(1))) != null) {
rsList.add(List.of(Atom.of("FRAME-BEGIN")));
extractInstructions(value, rsList);
rsList.add(List.of(Atom.of("FRAME-END")));
} else
Fail.t("bad frame definition");
else {
rsList.add(rs);
for (Node op : List_.right(rs, 1)) if ((value = label(op)) != null)
deque.push(value);
deque.push(tree.getRight());
}
} else
rsList.add(List.of(Atom.of("JUMP"), Int.of(ip)));
}
}
use of suite.node.Tree in project suite by stupidsing.
the class ThunkUtil method yawnList.
public static Outlet<Node> yawnList(Iterate<Node> yawn, Node node, boolean isFacilitateGc) {
return Outlet.of(new Source<>() {
private Node node_ = node;
private boolean first = true;
public Node source() {
Tree tree;
// first node is not a thunk, remainings are
if (!first)
node_ = yawn.apply(node_);
else
first = false;
if ((tree = Tree.decompose(node_)) != null) {
Node result = yawn.apply(tree.getLeft());
node_ = tree.getRight();
if (isFacilitateGc)
Tree.forceSetRight(tree, null);
return result;
} else if (node_ == Atom.NIL)
return null;
else
return Fail.t("not a list, unable to expand");
}
});
}
use of suite.node.Tree in project suite by stupidsing.
the class ThunkUtil method deepYawn.
/**
* Evaluates the whole (lazy) term to actual by invoking all the thunks.
*/
public static Node deepYawn(Iterate<Node> yawn, Node node) {
node = yawn.apply(node);
if (node instanceof Tree) {
Tree tree = (Tree) node;
Node left = deepYawn(yawn, tree.getLeft());
Node right = deepYawn(yawn, tree.getRight());
node = Tree.of(tree.getOperator(), left, right);
}
return node;
}
use of suite.node.Tree in project suite by stupidsing.
the class TypeChecker method getType.
private Node getType(Node data) {
Node type;
Tree tree;
if (data instanceof Reference)
type = variableTypes.computeIfAbsent(IdentityKey.of(data), k -> new Reference()).finalNode();
else if ((tree = Tree.decompose(data)) != null)
if (tree.getOperator() == TermOp.AND___) {
type = Suite.substitute(".0;", getType(tree.getLeft()));
bind(type, getType(tree.getRight()));
} else if (tree.getOperator() == TermOp.TUPLE_) {
Node name = tree.getLeft();
if (name instanceof Atom) {
Node node = tree.getRight();
Node[] ps = TreeUtil.elements(node, TreeUtil.nElements(node));
type = getEnumType(name, Tree.of(TermOp.TUPLE_, Read.from(ps).map(this::getType).toList()));
} else
// free type
return new Reference();
} else {
Atom name = Atom.of(tree.getOperator().getName());
Node lt = getType(tree.getLeft());
Node rt = getType(tree.getRight());
type = getEnumType(name, Tree.of(TermOp.TUPLE_, lt, rt));
}
else if (data == Atom.NIL)
type = Suite.substitute("_;");
else if (data instanceof Atom)
type = getEnumType(data, Atom.NIL);
else
type = Atom.of(data.getClass().getSimpleName());
return type;
}
Aggregations