1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
(** Copyright 2026, Dmitrii Kuznetsov *)
(** SPDX-License-Identifier: LGPL-3.0-or-later *)
open C_sharp_strange_lib
open Prettyprinter
open Parser
open Format
let test_pp _ source =
let prog = parse_option parse_prog source in
let pretty =
match prog with
| Some x -> asprintf "%a" pp_prog x
| None -> ""
in
let prog_after_pp = parse_option parse_prog pretty in
prog = prog_after_pp
;;
let samples =
[ ( "Factorial"
, {|
public class Program
{
public int Factorial(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * Factorial(n - 1);
}
}
public static void Main()
{
}
}
|}
)
; ( "Cycles 1"
, {|
public class Program
{
public int Cycles(int n, bool e, string x)
{
int x = 0;
while (x < n)
{
if (x == -1)
{
break;
}
if (x == -2)
{
continue;
}
x = x + 1;
}
for (int i = 1; i < n; i++)
{
break;
}
for (;;)
{
break;
}
for (int i = 1;; i++)
{
break;
}
}
public static void Main()
{
Cycles(5, true, "sample");
}
}
|}
)
; ( "Binops 1"
, {|
public class Program
{
public int Binops(int n, bool e, string x)
{
int x_ = n;
bool sample = !e || ((1 + 2 < 3 + 4) && (5 == 8));
string e = x;
char eeAe065ef = 'a';
e = null;
const int a = 1;
}
public static void Main()
{
Binops(5, true, "");
}
}
|}
)
; ( "StaticClass"
, {|
public static class Program {
static int result = 0;
public static void Main() {
int a = 5;
int b = 3;
result = a + b * 2;
if (result > 10) {
result = result - 10;
}
return;
}
}
|}
)
; ( "EmptyClass"
, {|
public static class Program {
public static void Main() {
{
{
}
}
}
}
|}
)
; ( "MultipleFields"
, {|
public class Test {
int a, b, c;
static string x, y;
const int MAX = 100;
}
|}
)
; ( "Simple arithmetic"
, {|
public static class Program {
static int result = 0;
public static void Main() {
int a = 5;
int b = 3;
result = a + b * 2;
if (result > 10) {
result = result - 10;
}
return;
}
}
|}
)
; ( "Cycles 2"
, {|
public static class Program {
static int sum = 0;
public static void Main() {
int i = 0;
while (i < 5) {
sum = sum + i;
i = i + 1;
}
for (int j = 0; j < 3; j = j + 1) {
sum = sum + j;
}
return;
}
}
|}
)
; ( "Boolean"
, {|
public static class Program {
static bool flag = true;
static int value = 42;
public static void Main() {
bool condition = flag && (value > 40);
if (condition) {
value = 100;
} else {
value = 0;
}
if (value == 100) {
flag = false;
}
return;
}
}
|}
)
; ( "Strings & chars"
, {|
public static class Program {
static string message = "Hello";
static char symbol = 'A';
public static void Main() {
string name = "World";
string result = message + " " + name;
char nextSymbol = symbol + 1;
if (result != "Hello World") {
result = "Error";
}
return;
}
}
|}
)
; ( "Cycles 3"
, {|
public static class Program {
static int counter = 0;
public static void Main() {
for (int i = 0; i < 10; i = i + 1) {
if (i == 3) {
continue;
}
counter = counter + 1;
if (counter > 5) {
break;
}
{
int temp = counter * 2;
counter = temp;
}
}
return;
}
}
|}
)
; ( "Complex exprs"
, {|
public static class Program {
static int x = 10;
static int y = 20;
static bool ok = true;
public static void Main() {
int result = (x + y) * (x - y) / 2;
bool check = (x > y) && ok || (x <= y);
if (!check && result != 0) {
result = -result;
}
return;
}
}
|}
)
; ( "Multiple definitions"
, {|
public static class Program {
static int a = 1;
static int b = 2;
static string s1 = "first";
static string s2 = "second";
static bool b1 = true;
static bool b2 = false;
static char c1 = 'X';
static char c2 = 'Y';
public static void Main() {
int x = a + b;
string text = s1 + s2;
bool flag = b1 && b2;
char letter = c1;
return;
}
}
|}
)
; ( "Binops 2"
, {|
public static class Program {
static int value = 100;
public static void Main() {
int a = 5;
int b = 3;
int sum = a + b;
int sub = a - b;
int mul = a * b;
int div = a / b;
int mod = a % b;
bool eq = a == b;
bool neq = a != b;
bool lt = a < b;
bool gt = a > b;
bool lte = a <= b;
bool gte = a >= b;
bool and = true && false;
bool or = true || false;
bool not = !true;
int neg = -a;
return;
}
}
|}
)
]
;;
let%test "All pp roundtrip tests" =
List.for_all (fun (name, source) -> test_pp name source) samples
;;