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);
}
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;
}
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;
}
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())));
}
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;
}
Aggregations