use of suite.lp.Trail in project suite by stupidsing.
the class GeneralizerFactoryTest method test.
private void test(String pattern, String match) {
for (GeneralizerFactory gf : new GeneralizerFactory[] { new CompileGeneralizerImpl(), new SewingGeneralizerImpl() }) {
Generalize_ p = gf.generalizer(Suite.parse(pattern));
assertTrue(Binder.bind(p.apply(gf.mapper().env()), Suite.parse(match), new Trail()));
}
}
use of suite.lp.Trail in project suite by stupidsing.
the class Chr method chrThen.
private Streamlet<State> chrThen(Streamlet<State> states, Node then) {
Generalizer generalizer = new Generalizer();
Atom a = atom(".a"), b = atom(".b");
if (Binder.bind(then, generalizer.generalize(Suite.substitute(".0 = .1", a, b)), new Trail())) {
// built-in syntactic equality
Reference from = generalizer.getVariable(a);
Reference to = generalizer.getVariable(b);
states = states.map(new Fun<>() {
public State apply(State state) {
IMap<Prototype, ISet<Node>> factsByPrototype1 = IMap.empty();
for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) factsByPrototype1 = factsByPrototype1.put(e.t0, replace(e.t1));
return new State(factsByPrototype1);
}
private ISet<Node> replace(ISet<Node> facts) {
ISet<Node> facts1 = ISet.empty();
for (Node node : facts) facts1 = facts1.replace(rw.replace(from, to, node));
return facts1;
}
});
}
return states.map(state -> {
Prototype prototype = Prototype.of(then);
ISet<Node> facts = getFacts(state, prototype);
return setFacts(state, prototype, facts.replace(then));
});
}
use of suite.lp.Trail in project suite by stupidsing.
the class Assembler method assemble.
public Bytes assemble(Node input) {
Generalizer generalizer = new Generalizer();
Trail trail = new Trail();
List<Pair<Reference, Node>> lnis = new ArrayList<>();
for (Node node0 : Tree.iter(input)) {
Node node = generalizer.generalize(node0);
Tree tree;
if ((tree = Tree.decompose(node, TermOp.EQUAL_)) != null)
if (!Binder.bind(tree.getLeft(), tree.getRight(), trail))
Fail.t("bind failed");
else
;
else if ((tree = Tree.decompose(node, TermOp.TUPLE_)) != null)
lnis.add(Pair.of((Reference) tree.getLeft(), tree.getRight()));
else
Fail.t("cannot assemble " + node);
}
return assemble(generalizer, preassemble.apply(lnis));
}
use of suite.lp.Trail in project suite by stupidsing.
the class StackAssembler method preassemble.
private List<Pair<Reference, Node>> preassemble(List<Pair<Reference, Node>> lnis0) {
List<Pair<Reference, Node>> lnis1 = new ArrayList<>();
Deque<int[]> deque = new ArrayDeque<>();
Trail trail = new Trail();
int fs = 0, rs = 0;
for (Pair<Reference, Node> lni0 : lnis0) {
Node node0 = lni0.t1;
Node node1;
Node[] m;
if ((m = FRBGN_.match(node0)) != null) {
deque.push(new int[] { fs, rs });
fs = 0;
rs = 0;
node1 = Atom.NIL;
} else if ((m = FREND_.match(node0)) != null) {
if (fs != 0)
node1 = Fail.t("unbalanced frame stack in subroutine definition");
else if (rs != 0)
node1 = Fail.t("unbalanced register stack in subroutine definition");
else {
int[] arr = deque.pop();
fs = arr[0];
rs = arr[1];
}
node1 = Atom.NIL;
} else if ((m = FRGET_.match(node0)) != null)
if (Binder.bind(m[0], Int.of(-fs), trail))
node1 = Atom.NIL;
else
node1 = Fail.t("cannot bind local variable offset");
else if ((m = FRPOP_.match(node0)) != null) {
fs -= 4;
node1 = Suite.substitute("POP .0", rewrite(rs, m[0]));
} else if ((m = FRPOPN.match(node0)) != null) {
Int int_ = (Int) m[0].finalNode();
fs -= int_.number;
node1 = Atom.NIL;
} else if ((m = FRPSH_.match(node0)) != null) {
fs += 4;
node1 = Suite.substitute("PUSH .0", rewrite(rs, m[0]));
} else if ((m = FRPSHN.match(node0)) != null) {
Int int_ = (Int) m[0].finalNode();
fs += int_.number;
node1 = Atom.NIL;
} else if ((m = LET___.match(node0)) != null)
if (Binder.bind(m[0], Int.of(TreeUtil.evaluate(m[1])), trail))
node1 = Atom.NIL;
else
node1 = Fail.t("cannot calculate expression");
else if (node0 == RPOP__) {
rs--;
node1 = Atom.NIL;
} else if (node0 == RPSH__) {
rs++;
node1 = Atom.NIL;
} else if (node0 == RRESTA) {
fs -= 4 * rs;
for (int r = rs - 1; 0 <= r; r--) lnis1.add(Pair.of(new Reference(), Suite.substitute("POP .0", getRegister(r))));
node1 = Atom.NIL;
} else if (node0 == RSAVEA) {
for (int r = 0; r < rs; r++) lnis1.add(Pair.of(new Reference(), Suite.substitute("PUSH .0", getRegister(r))));
fs += 4 * rs;
node1 = Atom.NIL;
} else
node1 = rewrite(rs, node0);
lnis1.add(Pair.of(lni0.t0, node1));
}
return new PeepholeOptimizer().optimize(lnis1);
}
use of suite.lp.Trail 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);
}
Aggregations