use of suite.adt.pair.Pair 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.adt.pair.Pair in project suite by stupidsing.
the class PeepholeOptimizer method optimize.
public List<Pair<Reference, Node>> optimize(List<Pair<Reference, Node>> lnis0) {
List<Pair<Reference, Node>> lnis1 = new ArrayList<>();
for (Pair<Reference, Node> lni0 : lnis0) {
Node node0 = lni0.t1;
Node node1;
Node[] m;
if ((m = ADDI__.match(node0)) != null) {
Node m0 = m[0];
int i = TreeUtil.evaluate(m[1]);
if (i == 1)
node1 = Suite.substitute("INC .0", m0);
else if (i == -1)
node1 = Suite.substitute("DEC .0", m0);
else if (0 < i)
node1 = Suite.substitute("ADD (.0, .1)", m0, Int.of(i));
else if (i < 0)
node1 = Suite.substitute("SUB (.0, .1)", m0, Int.of(-i));
else
node1 = Atom.NIL;
} else if ((m = MOV___.match(node0)) != null) {
Node m0 = m[0];
Node m1 = m[1];
if (m0 == m1)
node1 = Atom.NIL;
else if (m0 instanceof Atom && m1 instanceof Int && ((Int) m1).number == 0)
node1 = Suite.substitute("XOR (.0, .0)", m0);
else
node1 = Suite.substitute("MOV (.0, .1)", m0, m1);
} else
node1 = node0;
lnis1.add(Pair.of(lni0.t0, node1));
}
return lnis1;
}
use of suite.adt.pair.Pair 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.adt.pair.Pair in project suite by stupidsing.
the class LrParse method parse.
private Ast parse(Source<Ast> tokens, State state) {
Deque<Pair<Ast, State>> stack = new ArrayDeque<>();
Ast token = tokens.source();
while (true) {
String lookahead = token != null ? token.entity : "EOF";
Pair<State, Reduce> sr = shift(stack, state, lookahead);
if (sr.t0 != null) {
// shift
stack.push(Pair.of(token, state));
state = sr.t0;
token = tokens.source();
} else {
// reduce
Reduce reduce = sr.t1;
IList<Ast> nodes = IList.end();
for (int i = 0; i < reduce.n(); i++) {
Pair<Ast, State> ns = stack.pop();
nodes = IList.cons(ns.t0, nodes);
state = ns.t1;
}
Ast token1 = new Ast(reduce.name(), 0, 0, Read.from(nodes).toList());
if (rootEntity.equals(reduce.name()) && stack.size() == 0 && token == null)
return token1;
// force shift after reduce
stack.push(Pair.of(token1, state));
state = shift(stack, state, token1.entity).t0;
}
}
}
use of suite.adt.pair.Pair in project suite by stupidsing.
the class InterpretFunEager method eager.
public Node eager(Node node) {
Node mode = isLazyify ? Atom.of("LAZY") : Atom.of("EAGER");
Node query = Suite.substitute("source .in, fc-process-function .0 .in .out, sink .out", mode);
RuleSet rs = Suite.newRuleSet(List.of("auto.sl", "fc/fc.sl"));
Finder finder = new SewingProverBuilder2().build(rs).apply(query);
Node parsed = FindUtil.collectSingle(finder, node);
IntrinsicCallback ic = isLazyify ? lazyIntrinsicCallback() : Intrinsics.eagerIntrinsicCallback;
Map<String, Node> df = new HashMap<>();
df.put(TermOp.AND___.name, f2((a, b) -> Tree.of(TermOp.AND___, a, b)));
df.put("+call%i-t1", f1(i -> fn(1, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+call%i-t2", f1(i -> fn(2, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+call%i-t3", f1(i -> fn(3, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+call%i-v1", f1(i -> fn(1, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+call%i-v2", f1(i -> fn(2, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+call%i-v3", f1(i -> fn(3, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+compare", f2((a, b) -> Int.of(Comparer.comparer.compare(a, b))));
df.put("+get%i", f1(a -> new Data<>(Intrinsics.intrinsics.get(((Atom) a).name.split("!")[1]))));
df.put("+is-list", f1(a -> b(Tree.decompose(a) != null)));
df.put("+is-pair", f1(a -> b(Tree.decompose(a) != null)));
df.put("+lcons", f2((a, b) -> Tree.of(TermOp.OR____, a, b)));
df.put("+lhead", f1(a -> Tree.decompose(a).getLeft()));
df.put("+ltail", f1(a -> Tree.decompose(a).getRight()));
df.put("+pcons", f2((a, b) -> Tree.of(TermOp.AND___, a, b)));
df.put("+pleft", f1(a -> Tree.decompose(a).getLeft()));
df.put("+pright", f1(a -> Tree.decompose(a).getRight()));
for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
IntInt_Bool fun = e.getValue();
df.put(e.getKey().getName(), f2((a, b) -> b(fun.apply(compare(a, b), 0))));
}
for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
IntInt_Int fun = e.getValue();
df.put(e.getKey().getName(), f2((a, b) -> Int.of(fun.apply(i(a), i(b)))));
}
List<String> keys = df.keySet().stream().sorted().collect(Collectors.toList());
Eager_ eager0 = new Eager_(0, IMap.empty());
Frame frame = new Frame(null);
for (String key : keys) {
eager0 = eager0.put(Atom.of(key));
frame.add(df.get(key));
}
return eager0.eager_(parsed).apply(frame);
}
Aggregations