Skip to content

Commit

Permalink
Center value output in display source
Browse files Browse the repository at this point in the history
  • Loading branch information
s123unny committed Jun 28, 2023
1 parent 52bb4ff commit b5a1cf9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
24 changes: 15 additions & 9 deletions aeneas/src/ssa/SsaDebugger.v3
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,13 @@ class SrcPrinter(vstFiles: Array<VstFile>, interp: SsaInterpreter) {
if (VoidType.?(vtype) || instr.valueNum < 0) return;
var source = SsaApplyOp.!(instr).source;
var point = source.column - 1;
var pad = if(source.startColumn != -1, source.startColumn-1, point);
extractTmpConstraint(pad);
tmpBuf.pad(' ', pad);
tmpBuf.pad('~', point);
var start = if(source.startColumn != -1, source.startColumn-1, point);
var end = if(source.endColumn != -1, source.endColumn-1, point);
extractTmpConstraint(start);
tmpBuf.pad(' ', start);
V3.renderResult(interp.getVal(instr), vtype, tmpBuf);
tmpBuf.pad('~', source.endColumn-1);
tmpBuf.center('-', tmpBuf.length - start, end - start - 2);
tmpBuf.center('|', tmpBuf.length - start, end - start);
}
def out() {
extractTmpConstraint(0);
Expand All @@ -574,13 +575,18 @@ class SrcPrinter(vstFiles: Array<VstFile>, interp: SsaInterpreter) {
targetSp++;
}
}
def putIndent() {
for (i < indent) outBuf.tab();
}
def extractTmpConstraint(len: int) {
if (tmpBuf.length >= len) {
putPad();
putIndent();
outBuf.pad(' ', padNum).tab().valColor().puts(tmpBuf.ln().extract()).end();
outBuf.valColor().puts(tmpBuf.ln().extract()).end();
}
}
def putIndent() {
for (i < indent) outBuf.tab();
}
def putPad() {
for (i < padNum) outBuf.sp();
outBuf.tab();
}
}
13 changes: 13 additions & 0 deletions lib/util/StringBuilder.v3
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ class StringBuilder {
for (i < diff) buf[start + i] = ch;
length = end;
}
// Center the last {nchars} of this buffer to {len}, inserting {ch} before and after.
def center(ch: byte, nchars: int, len: int) -> this {
if (nchars > length) nchars = length;
var diff = len - nchars;
if (diff <= 0) return;
var start = length - nchars, end = start + len;
grow(end);
var lpad = diff / 2, rpad = diff - lpad;
for (i < nchars) buf[end - i - 1 - rpad] = buf[end - diff - i - 1];
for (i < lpad) buf[start + i] = ch;
for (i < rpad) buf[end - i - 1] = ch;
length = end;
}
// Reset the size of this buffer, reusing internal storage.
def reset() -> this {
length = 0;
Expand Down
20 changes: 20 additions & 0 deletions test/lib/StringBuilderTest.v3
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def X = [
T("outr2", test_outr2),
T("repeat", test_repeat),
T("rjustify", test_rjustify),
T("center", test_center),
()
];

Expand Down Expand Up @@ -363,3 +364,22 @@ def test_rjustify(t: LibTest) {
buf.rjustify('*', 2, 2);
t.assert_string(buf.extract(), "bug");
}

def test_center(t: LibTest) {
var buf = StringBuilder.new();

buf.center(' ', 1, 5);
t.assert_string(buf.extract(), " ");

buf.puts("foo");
buf.center('@', 1, 7);
t.assert_string(buf.extract(), "fo@@@o@@@");

buf.puts("bar");
buf.center('*', 4, 4);
t.assert_string(buf.extract(), "bar*");

buf.puts("bug");
buf.center('*', 2, 2);
t.assert_string(buf.extract(), "bug");
}

0 comments on commit b5a1cf9

Please sign in to comment.