Skip to content

Commit fe8f421

Browse files
author
Dwight Guth
committed
fix sound output mode
Former-commit-id: 65158438c4568ca3be337dcbff45cb83b20a06a2 [formerly bb9a7a370f4541f8c94b1a454aa2537ed28bc644] Former-commit-id: 7166dedfc940635077762f21b611d34d92bdb920
1 parent 08e1c42 commit fe8f421

3 files changed

Lines changed: 58 additions & 19 deletions

File tree

k-distribution/samples/kernelc/tests/execution/tests/undefined.c.out

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
</struct>
77
<fun>
88
main |-> functionBody ( .ParameterDeclarations , int , {
9-
.VariableDeclarations { struct listNode * x ; x = ( struct listNode
10-
* ) malloc ( sizeof ( struct listNode ) ) ; printf ( "%d\n", x ->
11-
value ) ; return 0 ; } return tv ( void , undef ) ; } )
9+
.VariableDeclarations { struct listNode * x ; x = ( struct
10+
listNode * ) malloc ( sizeof ( struct listNode ) ) ; printf (
11+
"%d\n", x -> value ) ; return 0 ; } return tv ( void , undef ) ; }
12+
)
1213
</fun>
1314
<k>
14-
* tv ( int * , member ( 2 , value ) ) ~> HOLE, .Expressions ~> "%d\n"
15-
, HOLE ~> printf ( HOLE ) ~> HOLE ; ~> return 0 ; .Statements ~>
16-
.Map ~> .List ~> return tv ( void , undef ) ; .Statements
15+
* tv ( int * , member ( 2 , value ) ) ~> HOLE, .Expressions ~>
16+
"%d\n", HOLE ~> printf ( HOLE ) ~> HOLE ; ~> return 0 ;
17+
.Statements ~> .Map ~> .List ~> return tv ( void , undef ) ;
18+
.Statements
1719
</k>
1820
<env>
1921
x |-> tv ( struct listNode * * , object ( 1 ) )

kernel/src/main/java/org/kframework/backend/unparser/Unparser.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,43 +82,62 @@ public Unparser(Context context, ColorSetting color, Color terminalColor) {
8282
public int compare(ASTNode o1, ASTNode o2) {
8383
Deque<Component> thisStack = new LinkedList<>();
8484
Deque<Component> thatStack = new LinkedList<>();
85-
StringBuilder thisString = new StringBuilder();
86-
StringBuilder thatString = new StringBuilder();
85+
Indenter thisString = new Indenter();
86+
thisString.setWidth(-1);
87+
Indenter thatString = new Indenter();
88+
thisString.setWidth(-1);
8789
thisStack.push(new TermComponent(o1));
8890
thatStack.push(new TermComponent(o2));
91+
int lastIdx = 0;
8992
while (true) {
9093
processStack(thisStack, thisString);
9194
processStack(thatStack, thatString);
9295
int lim = Math.min(thisString.length(), thatString.length());
93-
String s1 = thisString.substring(0, lim);
94-
String s2 = thisString.substring(0, lim);
96+
String s1 = thisString.stringBuilder.substring(lastIdx, lim);
97+
String s2 = thatString.stringBuilder.substring(lastIdx, lim);
98+
lastIdx = lim;
9599
int result = comparator.compare(s1, s2);
96100
if (result != 0) return result;
97-
thisString.delete(0, lim);
98-
thatString.delete(0, lim);
99101
if (thisStack.isEmpty() && thatStack.isEmpty()) {
100-
return comparator.compare(thisString.toString(), thatString.toString());
102+
return comparator.compare(thisString.stringBuilder.substring(lim), thatString.stringBuilder.substring(lim));
101103
}
102104
}
103105
}
104106

105-
public String print(ASTNode node) {
107+
public String print(ASTNode node, IndentationOptions indent) {
106108
Deque<Component> stack = new LinkedList<>();
107-
StringBuilder string = new StringBuilder();
109+
Indenter string = new Indenter(indent);
108110
stack.push(new TermComponent(node));
109111
while(!stack.isEmpty()) {
110112
processStack(stack, string);
111113
}
112114
return string.toString();
113115
}
114116

115-
private void processStack(Deque<Component> stack, StringBuilder string) {
117+
private void processStack(Deque<Component> stack, Indenter string) {
116118
if (stack.isEmpty()) {
117119
return;
118120
}
119121
Component comp = stack.pop();
120122
if (comp instanceof StringComponent) {
121-
string.append(((StringComponent)comp).s);
123+
string.write(((StringComponent)comp).s);
124+
return;
125+
}
126+
if (comp instanceof FormatComponent) {
127+
switch (((FormatComponent) comp).format) {
128+
case INDENT:
129+
string.indent(4);
130+
break;
131+
case DEDENT:
132+
string.unindent();
133+
break;
134+
case INDENT_TO_CURRENT:
135+
string.indentToCurrent();
136+
break;
137+
case NEWLINE:
138+
string.endLine();
139+
break;
140+
}
122141
return;
123142
}
124143
ASTNode term = ((TermComponent) comp).term;
@@ -138,6 +157,11 @@ private static class StringComponent implements Component {
138157
public StringComponent(String s) {
139158
this.s = s;
140159
}
160+
161+
@Override
162+
public String toString() {
163+
return s;
164+
}
141165
}
142166

143167
private static class TermComponent implements Component {
@@ -146,6 +170,11 @@ private static class TermComponent implements Component {
146170
public TermComponent(ASTNode term) {
147171
this.term = term;
148172
}
173+
174+
@Override
175+
public String toString() {
176+
return term.toString();
177+
}
149178
}
150179

151180
private static enum Format {
@@ -158,6 +187,11 @@ private static class FormatComponent implements Component {
158187
public FormatComponent(Format format) {
159188
this.format = format;
160189
}
190+
191+
@Override
192+
public String toString() {
193+
return format.toString();
194+
}
161195
}
162196

163197
private class ComponentVisitor extends NonCachingVisitor {

kernel/src/main/java/org/kframework/krun/KRunModule.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ protected void configure() {
9696
experimentalOptionsBinder.addBinding().toInstance(KRunOptions.Experimental.class);
9797
experimentalOptionsBinder.addBinding().toInstance(SMTOptions.class);
9898

99-
bind(OutputModes.class).toInstance(options.output);
100-
10199
ThrowingProviderBinder throwingBinder = ThrowingProviderBinder.create(binder());
102100

103101
// bind Transformation<P, Q> to TransformationProvider<Transformation<P, Q>>
@@ -174,6 +172,11 @@ private <T> TypeToken<TransformationProvider<T>> providerOf(TypeToken<T> transfo
174172
}
175173
}
176174

175+
@Provides
176+
OutputModes outputMode(KRunOptions options) {
177+
return options.output;
178+
}
179+
177180
public static class CommonModule extends AbstractModule {
178181

179182
@Override

0 commit comments

Comments
 (0)