use of suite.node.Int 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);
}
use of suite.node.Int 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();
}
use of suite.node.Int 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;
}
use of suite.node.Int in project suite by stupidsing.
the class TreeUtil method evaluate.
public static int evaluate(Node node) {
Tree tree = Tree.decompose(node);
int result;
if (tree != null) {
Operator op = tree.getOperator();
IntInt_Int fun;
int lhs, rhs;
if (op == TermOp.TUPLE_) {
Tree rightTree = Tree.decompose(tree.getRight());
lhs = evaluate(tree.getLeft());
rhs = evaluate(rightTree.getRight());
fun = evaluateOp(rightTree.getLeft());
} else {
lhs = evaluate(tree.getLeft());
rhs = evaluate(tree.getRight());
fun = evaluateOp(op);
}
result = fun.apply(lhs, rhs);
} else if (node instanceof Int)
result = ((Int) node).number;
else
result = Fail.t("cannot evaluate expression: " + node);
return result;
}
Aggregations