use of suite.node.Dict in project suite by stupidsing.
the class RecursiveFactorizerTest method treeNode.
private Node treeNode(Source<Node> g, Node name, List<Node> nodes) {
List<Node> pairs = Read.from(nodes).map(node -> pairNode(node, g.source())).toList();
Dict dict = new Dict();
dict.map.put(Atom.of("name"), Reference.of(name));
dict.map.put(Atom.of("pairs"), Reference.of(Tree.of(TermOp.OR____, pairs)));
return Tree.of(TermOp.COLON_, Atom.of(FTree.class.getName()), dict);
}
use of suite.node.Dict in project suite by stupidsing.
the class RecursiveFactorizerTest method terminalNode.
private Node terminalNode(String s) {
Dict dict = new Dict();
dict.map.put(Atom.of("chars"), Reference.of(new Str(s)));
return Tree.of(TermOp.COLON_, Atom.of(FTerminal.class.getName()), dict);
}
use of suite.node.Dict in project suite by stupidsing.
the class TypeChecker method getEnumType.
private Node getEnumType(Node name, Node type1) {
Dict dict = new Dict();
dict.map.put(name, Reference.of(type1));
return dict;
}
use of suite.node.Dict in project suite by stupidsing.
the class Binder method bind.
public static boolean bind(Node n0, Node n1, Trail trail) {
n0 = n0.finalNode();
n1 = n1.finalNode();
if (n0 == n1)
return true;
Class<? extends Node> clazz0 = n0.getClass();
Class<? extends Node> clazz1 = n1.getClass();
if (clazz0 == Reference.class) {
trail.addBind((Reference) n0, n1);
return true;
} else if (clazz1 == Reference.class) {
trail.addBind((Reference) n1, n0);
return true;
}
if (clazz0 == Dict.class && clazz1 == Dict.class) {
Map<Node, Reference> map0 = ((Dict) n0).map;
Map<Node, Reference> map1 = ((Dict) n1).map;
boolean b = true;
for (Node key : List_.concat(map0.keySet(), map1.keySet())) {
Node v0 = map0.computeIfAbsent(key, k -> new Reference());
Node v1 = map1.computeIfAbsent(key, k -> new Reference());
b &= bind(v0, v1, trail);
}
return b;
} else if (clazz0 == Int.class && clazz1 == Int.class)
return ((Int) n0).number == ((Int) n1).number;
else if (clazz0 == Str.class && clazz1 == Str.class)
return Objects.equals(((Str) n0).value, ((Str) n1).value);
else if (Tree.class.isAssignableFrom(clazz0) && Tree.class.isAssignableFrom(clazz1)) {
Tree t0 = (Tree) n0;
Tree t1 = (Tree) n1;
return //
t0.getOperator() == t1.getOperator() && //
bind(t0.getLeft(), t1.getLeft(), trail) && bind(t0.getRight(), t1.getRight(), trail);
} else if (clazz0 == Tuple.class && clazz1 == Tuple.class) {
Node[] nodes0 = ((Tuple) n0).nodes;
Node[] nodes1 = ((Tuple) n1).nodes;
boolean b = nodes0.length == nodes1.length;
if (b) {
for (int i = 0; i < nodes0.length; i++) b &= bind(nodes0[i], nodes1[i], trail);
}
return b;
} else
return false;
}
use of suite.node.Dict in project suite by stupidsing.
the class SewingClonerImpl method cloner.
@Override
public Clone_ cloner(Node node) {
List<Clone_> funs = new ArrayList<>();
Clone_ fun;
while (true) {
Node node0 = node;
Tree tree;
if (node0 instanceof Dict) {
Clone_[][] array = //
Read.from2(//
((Dict) node0).map).map(//
(key, value) -> new Clone_[] { cloner(key), cloner(value) }).toArray(Clone_[].class);
int length = array.length;
return env -> {
@SuppressWarnings("unchecked") Pair<Node, Reference>[] pairs = new Pair[length];
for (int i = 0; i < length; i++) pairs[i] = Pair.of(array[i][0].apply(env), Reference.of(array[i][1].apply(env)));
return Dict.of(pairs);
};
} else if ((tree = Tree.decompose(node0)) != null) {
Operator operator = tree.getOperator();
if (operator != TermOp.OR____) {
Clone_ f = cloner(tree.getLeft());
funs.add(env -> Tree.of(operator, f.apply(env), null));
node = tree.getRight();
continue;
} else {
// delay generalizing for performance
Clone_ lf = cloner(tree.getLeft());
Clone_ rf = cloner(tree.getRight());
fun = env -> Tree.of(operator, lf.apply(env), new Suspend(() -> rf.apply(env)));
}
} else if (node0 instanceof Reference) {
int index = vm.computeIndex((Reference) node0);
fun = env -> env.get(index);
} else if (node0 instanceof Tuple) {
Clone_[] ps = Read.from(((Tuple) node0).nodes).map(this::cloner).toArray(Clone_.class);
int size = ps.length;
fun = env -> {
Node[] nodes = new Node[size];
for (int i = 0; i < size; i++) nodes[i] = ps[i].apply(env);
return Tuple.of(nodes);
};
} else
fun = env -> node0;
funs.add(fun);
break;
}
if (1 < funs.size())
return env -> {
Tree t = Tree.of(null, null, null);
Node node_ = t;
for (Clone_ fun_ : funs) {
Tree t_ = Tree.decompose(node_);
Tree.forceSetRight(t_, fun_.apply(env));
node_ = t_.getRight();
}
return t.getRight();
};
else
return funs.get(0);
}
Aggregations