Search in sources :

Example 1 with Run

use of suite.text.Preprocess.Run in project suite by stupidsing.

the class Assembler method assemble.

public Bytes assemble(String in0) {
    Set<Character> whitespaces = Collections.singleton('\n');
    Fun<String, List<Run>> gct = CommentPreprocessor.groupCommentPreprocessor(whitespaces);
    Fun<String, List<Run>> lct = CommentPreprocessor.lineCommentPreprocessor(whitespaces);
    String in1 = Preprocess.transform(List.of(gct, lct), in0).t0;
    Generalizer generalizer = new Generalizer();
    List<String> lines = List.of(in1.split("\n"));
    Pair<String, String> pe;
    int start = 0;
    while (!(pe = String_.split2(lines.get(start), "=")).t1.isEmpty()) {
        generalizer.getVariable(Atom.of(pe.t0)).bound(Suite.parse(pe.t1));
        start++;
    }
    List<Pair<Reference, Node>> lnis = // 
    Read.from(// 
    List_.right(lines, start)).map(line -> {
        Pair<String, String> pt = String_.split2(line, "\t");
        String label = pt.t0;
        String command = pt.t1;
        Reference reference = String_.isNotBlank(label) ? generalizer.getVariable(Atom.of(label)) : null;
        Node instruction = generalizer.generalize(Suite.parse(command));
        return Pair.of(reference, instruction);
    }).toList();
    return assemble(generalizer, lnis);
}
Also used : Read(suite.streamlet.Read) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) Node(suite.node.Node) CommentPreprocessor(suite.parser.CommentPreprocessor) String_(suite.util.String_) Preprocess(suite.text.Preprocess) Run(suite.text.Preprocess.Run) Binder(suite.lp.doer.Binder) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Finder(suite.lp.search.ProverBuilder.Finder) Set(java.util.Set) TermOp(suite.node.io.TermOp) Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder) List_(suite.util.List_) To(suite.util.To) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Int(suite.node.Int) Collections(java.util.Collections) Fail(suite.util.Fail) Reference(suite.node.Reference) Node(suite.node.Node) Generalizer(suite.lp.doer.Generalizer) ArrayList(java.util.ArrayList) List(java.util.List) Pair(suite.adt.pair.Pair)

Example 2 with Run

use of suite.text.Preprocess.Run in project suite by stupidsing.

the class CommentPreprocessor method apply.

@Override
public List<Run> apply(String in) {
    int closeLength = !isWhitespaces(closeComment) ? closeComment.length() : 0;
    int start = 0;
    List<Run> runs = new ArrayList<>();
    while (true) {
        int pos0 = ParseUtil.search(in, start, openComment);
        if (pos0 == -1)
            break;
        int pos1 = in.indexOf(closeComment, pos0 + openComment.length());
        if (pos1 == -1)
            break;
        runs.add(new Run(start, pos0));
        start = pos1 + closeLength;
    }
    runs.add(new Run(start, in.length()));
    return runs;
}
Also used : ArrayList(java.util.ArrayList) Run(suite.text.Preprocess.Run)

Example 3 with Run

use of suite.text.Preprocess.Run in project suite by stupidsing.

the class IndentationPreprocessor method apply.

@Override
public List<Run> apply(String in) {
    List<Run> runs = new ArrayList<>();
    int nLastIndents = 0;
    String lastIndent = "";
    int pos = 0;
    int length = in.length();
    while (pos < length) {
        int pos0 = pos;
        char ch;
        while (pos0 < length && (ch = in.charAt(pos0)) != '\n' && Character.isWhitespace(ch)) pos0++;
        Segment segment = ParseUtil.searchPosition(in.toCharArray(), Segment.of(pos0, length), "\n", Assoc.RIGHT, false);
        int pos1 = segment != null ? segment.start : length;
        // includes LF
        int pos2 = segment != null ? segment.end : length;
        String indent = in.substring(pos, pos0);
        String line = in.substring(pos0, pos1);
        int nIndents = pos0 - pos, lineLength = pos1 - pos0;
        if (!lastIndent.startsWith(indent) && !indent.startsWith(lastIndent))
            Fail.t("indent mismatch");
        if (lineLength != 0) {
            // ignore empty lines
            int startPos = 0, endPos = lineLength;
            lastIndent = indent;
            // find operators at beginning and end of line
            for (Operator operator : operators) {
                String name = operator.getName().trim();
                if (!name.isEmpty()) {
                    if (line.startsWith(name + " "))
                        startPos = max(startPos, name.length() + 1);
                    if (String_.equals(line, name))
                        startPos = max(startPos, name.length());
                    if (line.endsWith(name))
                        endPos = min(endPos, lineLength - name.length());
                }
            }
            if (// when a line has only one operator
            endPos < startPos)
                startPos = 0;
            // insert parentheses by line indentation
            while (nIndents < nLastIndents) {
                runs.add(new Run(") "));
                nLastIndents--;
            }
            runs.add(new Run(pos0, pos0 + startPos));
            while (nLastIndents < nIndents) {
                runs.add(new Run(" ("));
                nLastIndents++;
            }
            runs.add(new Run(pos0 + startPos, pos2));
            nLastIndents = nIndents;
        }
        pos = pos2;
    }
    while (0 < nLastIndents--) runs.add(new Run(") "));
    return runs;
}
Also used : Operator(suite.node.io.Operator) ArrayList(java.util.ArrayList) Run(suite.text.Preprocess.Run) Segment(suite.text.Segment)

Example 4 with Run

use of suite.text.Preprocess.Run in project suite by stupidsing.

the class IncludePreprocessor method doIncludes.

private void doIncludes(Path dir, String in, boolean isInput, List<Run> runs) {
    int start = 0;
    while (true) {
        int pos0 = ParseUtil.search(in, start, open);
        if (pos0 == -1)
            break;
        int pos1 = ParseUtil.search(in, pos0 + open.length(), close);
        if (pos1 == -1)
            break;
        if (isInput)
            runs.add(new Run(start, pos0));
        else
            runs.add(new Run(in.substring(start, pos0)));
        Path path = dir.resolve(in.substring(pos0 + open.length(), pos1));
        if (included.add(path.toAbsolutePath()))
            doIncludes(path.getParent(), To.string(path), false, runs);
        start = pos1 + close.length();
    }
    if (isInput)
        runs.add(new Run(start, in.length()));
    else
        runs.add(new Run(in.substring(start, in.length())));
}
Also used : Path(java.nio.file.Path) Run(suite.text.Preprocess.Run)

Example 5 with Run

use of suite.text.Preprocess.Run in project suite by stupidsing.

the class WhitespacePreprocessor method apply.

@Override
public List<Run> apply(String in) {
    List<Run> runs = new ArrayList<>();
    int length = in.length();
    int pos0 = 0, pos = 0;
    int quote = 0;
    boolean backquote = false;
    while (pos < length) {
        char ch = in.charAt(pos++);
        if (ch != '`') {
            if ((quote = ParseUtil.getQuoteChange(quote, ch)) == 0 && whitespaces.contains(ch)) {
                runs.add(new Run(pos0, pos - 1));
                runs.add(new Run(" "));
                pos0 = pos;
            }
        } else if (quote == 0) {
            int split = (backquote = !backquote) ? pos : pos - 1;
            runs.add(new Run(pos0, split));
            runs.add(new Run(" "));
            pos0 = split;
        }
    }
    runs.add(new Run(pos0, length));
    return runs;
}
Also used : ArrayList(java.util.ArrayList) Run(suite.text.Preprocess.Run)

Aggregations

Run (suite.text.Preprocess.Run)5 ArrayList (java.util.ArrayList)4 Path (java.nio.file.Path)1 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 Suite (suite.Suite)1 Pair (suite.adt.pair.Pair)1 Trail (suite.lp.Trail)1 Binder (suite.lp.doer.Binder)1 Generalizer (suite.lp.doer.Generalizer)1 RuleSet (suite.lp.kb.RuleSet)1 Finder (suite.lp.search.ProverBuilder.Finder)1 SewingProverBuilder2 (suite.lp.search.SewingProverBuilder2)1 Atom (suite.node.Atom)1 Int (suite.node.Int)1 Node (suite.node.Node)1 Reference (suite.node.Reference)1 Tree (suite.node.Tree)1 Operator (suite.node.io.Operator)1