Search in sources :

Example 6 with Bytes

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();
}
Also used : Bytes(primal.primitive.adt.Bytes) BytesBuilder(primal.primitive.adt.Bytes.BytesBuilder)

Example 7 with Bytes

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);
}
Also used : Asm(suite.asm.Assembler.Asm) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Fail.fail(primal.statics.Fail.fail) Fun(primal.fp.Funs.Fun) Amd64Assemble(suite.assembler.Amd64Assemble) Right(primal.Verbs.Right) Is(primal.Verbs.Is) ArrayList(java.util.ArrayList) Node(suite.node.Node) CommentPreprocessor(suite.parser.CommentPreprocessor) Preprocess(suite.text.Preprocess) Utf8(primal.Nouns.Utf8) Split(primal.MoreVerbs.Split) Run(suite.text.Preprocess.Run) Take(primal.Verbs.Take) Binder(suite.lp.doer.Binder) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Amd64Mode(suite.assembler.Amd64Mode) Pair(primal.adt.Pair) Reference(suite.node.Reference) Suite(suite.Suite) Amd64Parse(suite.assembler.Amd64Parse) Finder(suite.lp.search.ProverBuilder.Finder) TermOp(suite.node.io.TermOp) Read(primal.MoreVerbs.Read) Bytes(primal.primitive.adt.Bytes) Tree(suite.node.Tree) List(java.util.List) Atom(suite.node.Atom) Fail.failBool(primal.statics.Fail.failBool) BytesBuilder(primal.primitive.adt.Bytes.BytesBuilder) SwitchNode(suite.node.io.SwitchNode) Int(suite.node.Int) Collections(java.util.Collections) Str(suite.node.Str) Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with Bytes

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;
}
Also used : Bytes(primal.primitive.adt.Bytes) ArrayList(java.util.ArrayList) BytesPair(suite.text.TextUtil.BytesPair)

Aggregations

Bytes (primal.primitive.adt.Bytes)8 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 Utf8 (primal.Nouns.Utf8)3 Sink (primal.fp.Funs.Sink)3 IOException (java.io.IOException)2 List (java.util.List)2 Pull (primal.MoreVerbs.Pull)2 Read (primal.MoreVerbs.Read)2 Split (primal.MoreVerbs.Split)2 Equals (primal.Verbs.Equals)2 Log_ (primal.os.Log_)2 BytesBuilder (primal.primitive.adt.Bytes.BytesBuilder)2 Rethrow.ex (primal.statics.Rethrow.ex)2 Handler (suite.http.Http.Handler)2 Header (suite.http.Http.Header)2 Response (suite.http.Http.Response)2 BufferedReader (java.io.BufferedReader)1 Closeable (java.io.Closeable)1 InputStream (java.io.InputStream)1