use of primal.primitive.adt.Bytes in project suite by stupidsing.
the class AsmSl method assemble.
private Bytes assemble(Generalizer generalizer, List<Pair<Reference, Node>> lnis) {
var org = Int.num(generalizer.getVariable(Atom.of(".org")).finalNode());
var out = new BytesBuilder();
for (var isPass2 : new boolean[] { false, true }) {
AssemblePredicates.isPass2 = isPass2;
out.clear();
for (var lni : lnis) {
Bytes bytes = lni.map((reference, instruction) -> {
var address = org + out.size();
if (!isPass2)
reference.bound(Int.of(address));
else if (Int.num(reference.finalNode()) != address)
fail("address varied between passes at " + Integer.toHexString(address) + ": " + instruction);
try {
return asm.assemble(isPass2, address, instruction);
} catch (Exception ex) {
return fail("in " + instruction + " during pass " + (!isPass2 ? "1" : "2"), ex);
}
});
out.append(bytes);
}
if (isPass2)
for (var lni : lnis) lni.k.unbound();
}
return out.toBytes();
}
use of primal.primitive.adt.Bytes in project suite by stupidsing.
the class AsmSl method assemble.
public Bytes assemble(String in0) {
var whitespaces = Collections.singleton('\n');
Fun<String, List<Run>> gct = CommentPreprocessor.ofGroupComment(whitespaces)::preprocess;
Fun<String, List<Run>> lct = CommentPreprocessor.ofLineComment(whitespaces)::preprocess;
var in1 = Preprocess.transform(List.of(gct, lct), in0).k;
var generalizer = new Generalizer();
var lines = List.of(in1.split("\n"));
Pair<String, String> pe;
var start = 0;
while ((pe = Split.string(lines.get(start), "=")) != null) {
generalizer.getVariable(Atom.of(pe.k)).bound(Suite.parse(pe.v));
start++;
}
var lnis = //
Read.from(//
Right.of(lines, start)).map(line -> Split.strl(line, "\t").map((label, command) -> {
var reference = //
Is.notBlank(label) ? //
generalizer.getVariable(Atom.of(label)) : new Reference();
var instruction = generalizer.generalize(Suite.parse(command));
return Pair.of(reference, instruction);
})).toList();
return assemble(generalizer, lnis);
}
use of primal.primitive.adt.Bytes in project suite by stupidsing.
the class Impl method readPatch.
public List<BytesPair> readPatch(InputStream is) {
var list = new ArrayList<BytesPair>();
String line;
while (!Equals.string(line = ReadLine.from(is), "EOF")) list.add(FixieArray.of(line.split(" ")).map((f, s0, s1) -> ex(() -> {
var size0 = !Equals.string(s0, "N") ? Integer.valueOf(s0) : null;
var size1 = !Equals.string(s1, "N") ? Integer.valueOf(s1) : null;
Bytes bs0, bs1;
if (Equals.string("!", f)) {
bs0 = readBlock(is, size0, '<');
bs1 = readBlock(is, size1, '>');
} else
bs0 = bs1 = readBlock(is, size0, '=');
return new BytesPair(bs0, bs1);
})));
return list;
}
Aggregations