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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
(** Copyright 2026, Dmitrii Kuznetsov *)
(** SPDX-License-Identifier: LGPL-3.0-or-later *)
open C_sharp_strange_lib.Ast
open C_sharp_strange_lib.Parser
let%test "Parse one integer" = apply_parser parse_int {|1|} = Ok (ValInt 1)
let%test "Parse one char" = apply_parser parse_char {|'c'|} = Ok (ValChar 'c')
let%test "Parse true" = apply_parser parse_bool {|true|} = Ok (ValBool true)
let%test "Parse false" = apply_parser parse_bool {|false|} = Ok (ValBool false)
let%test "Parse string" =
apply_parser parse_val_string {|"sample"|} = Ok (ValString "sample")
;;
let%test "Parse parens" = apply_parser (parens parse_int) {|(1)|} = Ok (ValInt 1)
let%test "Parse braces" = apply_parser (braces parse_int) {|{1}|} = Ok (ValInt 1)
let%test "Parse brackets" = apply_parser (brackets parse_int) {|[1]|} = Ok (ValInt 1)
let%test "Parse one modifier 1" = apply_parser parse_modifiers {|static|} = Ok [ MStatic ]
let%test "Parse one modifier 2" = apply_parser parse_modifiers {|public|} = Ok [ MPublic ]
let%test "Parse two modifiers" =
apply_parser parse_modifiers {|public async|} = Ok [ MPublic; MAsync ]
;;
let%test "Parse add 1" =
apply_parser parse_ops {| 1 + 2|}
= Ok (EBinOp (OpAdd, EValue (ValInt 1), EValue (ValInt 2)))
;;
let%test "Parse add 2" =
apply_parser parse_ops {| a + b|} = Ok (EBinOp (OpAdd, EId (Id "a"), EId (Id "b")))
;;
let%test "Parse many adds" =
apply_parser parse_ops {| 1 + 2 + 3|}
= Ok
(EBinOp
(OpAdd, EBinOp (OpAdd, EValue (ValInt 1), EValue (ValInt 2)), EValue (ValInt 3)))
;;
let%test "Parse adds with mul 1" =
apply_parser parse_ops {|1 + 2 * 3|}
= Ok
(EBinOp
(OpAdd, EValue (ValInt 1), EBinOp (OpMul, EValue (ValInt 2), EValue (ValInt 3))))
;;
let%test "Parse adds with mul 2" =
apply_parser parse_ops {| (1 + 2 ) * 3|}
= Ok
(EBinOp
(OpMul, EBinOp (OpAdd, EValue (ValInt 1), EValue (ValInt 2)), EValue (ValInt 3)))
;;
let%test "Parse div with mod" =
apply_parser parse_ops {| 1 / 2 % 3|}
= Ok
(EBinOp
(OpMod, EBinOp (OpDiv, EValue (ValInt 1), EValue (ValInt 2)), EValue (ValInt 3)))
;;
let%test "Parse div with mod" =
apply_parser parse_ops {| 1 - 2 / 3 + 4|}
= Ok
(EBinOp
( OpAdd
, EBinOp
( OpSub
, EValue (ValInt 1)
, EBinOp (OpDiv, EValue (ValInt 2), EValue (ValInt 3)) )
, EValue (ValInt 4) ))
;;
let%test "Parse simple boolean expression" =
apply_parser parse_ops {| ( 1 + 2 == 3 + 4 )|}
= Ok
(EBinOp
( OpEqual
, EBinOp (OpAdd, EValue (ValInt 1), EValue (ValInt 2))
, EBinOp (OpAdd, EValue (ValInt 3), EValue (ValInt 4)) ))
;;
let%test "Parse complex boolean expression" =
apply_parser parse_ops {|( 1 + 2 < 3 + 4) && (5 == 8)|}
= Ok
(EBinOp
( OpAnd
, EBinOp
( OpLess
, EBinOp (OpAdd, EValue (ValInt 1), EValue (ValInt 2))
, EBinOp (OpAdd, EValue (ValInt 3), EValue (ValInt 4)) )
, EBinOp (OpEqual, EValue (ValInt 5), EValue (ValInt 8)) ))
;;
let%test "Parse ident expr" = apply_parser parse_ops {| x|} = Ok (EId (Id "x"))
let%test "Parse id in expressions 1" = apply_parser parse_ops {| x|} = Ok (EId (Id "x"))
let%test "Parse id in expressions 2" =
apply_parser parse_ops {|x + 1|} = Ok (EBinOp (OpAdd, EId (Id "x"), EValue (ValInt 1)))
;;
let%test "Parse var declaration 1" =
apply_parser parse_decl {|int x|}
= Ok (SDecl (Var (TypeVar (TypeBase TypeInt), Id "x"), None))
;;
let%test "Parse var declaration 2" =
apply_parser parse_decl {|int x = 1|}
= Ok (SDecl (Var (TypeVar (TypeBase TypeInt), Id "x"), Some (EValue (ValInt 1))))
;;
let%test "Parse multiple var declarations" =
apply_parser parse_decl {|int x = y = z = 1|}
= Ok
(SDecl
( Var (TypeVar (TypeBase TypeInt), Id "x")
, Some
(EBinOp
( OpAssign
, EId (Id "y")
, EBinOp (OpAssign, EId (Id "z"), EValue (ValInt 1)) )) ))
;;
let%test "Parse return 1" =
apply_parser parse_return {|return 5|} = Ok (SReturn (Some (EValue (ValInt 5))))
;;
let%test "Parse return 2" = apply_parser parse_return {|return|} = Ok (SReturn None)
let%test "Parse break" = apply_parser parse_break {|break|} = Ok SBreak
let%test "Parse continue" = apply_parser parse_continue {|continue|} = Ok SContinue
let%test "Parse empty block 1" = apply_parser parse_block {|{}|} = Ok (SBlock [])
let%test "Parse empty block 2" = apply_parser parse_block {|{;;;;}|} = Ok (SBlock [])
let%test "Parse block 1" =
apply_parser parse_block {|{return 5;}|}
= Ok (SBlock [ SReturn (Some (EValue (ValInt 5))) ])
;;
let%test "Parse block 2" =
apply_parser parse_block {|{int x = 6; x = 6 + 1; return x;}|}
= Ok
(SBlock
[ SDecl (Var (TypeVar (TypeBase TypeInt), Id "x"), Some (EValue (ValInt 6)))
; SExpr
(EBinOp
( OpAssign
, EId (Id "x")
, EBinOp (OpAdd, EValue (ValInt 6), EValue (ValInt 1)) ))
; SReturn (Some (EId (Id "x")))
])
;;
let%test "Parse while" =
apply_parser
parse_block
{|
{
int x = 1;
while ( x < 1 )
{
x = 2;
break;
continue;
}
}|}
= Ok
(SBlock
[ SDecl (Var (TypeVar (TypeBase TypeInt), Id "x"), Some (EValue (ValInt 1)))
; SWhile
( EBinOp (OpLess, EId (Id "x"), EValue (ValInt 1))
, SBlock
[ SExpr (EBinOp (OpAssign, EId (Id "x"), EValue (ValInt 2)))
; SBreak
; SContinue
] )
])
;;
let%test "Parse for" =
apply_parser
parse_block
{|{
for (int i = 1;i < 5; i = i+1)
{
i = i + 1;
}
}|}
= Ok
(SBlock
[ SFor
( Some
(SDecl
(Var (TypeVar (TypeBase TypeInt), Id "i"), Some (EValue (ValInt 1))))
, Some (EBinOp (OpLess, EId (Id "i"), EValue (ValInt 5)))
, Some
(EBinOp
( OpAssign
, EId (Id "i")
, EBinOp (OpAdd, EId (Id "i"), EValue (ValInt 1)) ))
, SBlock
[ SExpr
(EBinOp
( OpAssign
, EId (Id "i")
, EBinOp (OpAdd, EId (Id "i"), EValue (ValInt 1)) ))
] )
])
;;
let%test "Parse if" =
apply_parser
parse_block
{|{if (x == 5)
x=1;
else if (x == 2)
{
x=2;
}
}|}
= Ok
(SBlock
[ SIf
( EBinOp (OpEqual, EId (Id "x"), EValue (ValInt 5))
, SExpr (EBinOp (OpAssign, EId (Id "x"), EValue (ValInt 1)))
, Some
(SIf
( EBinOp (OpEqual, EId (Id "x"), EValue (ValInt 2))
, SBlock
[ SExpr (EBinOp (OpAssign, EId (Id "x"), EValue (ValInt 2))) ]
, None )) )
])
;;
let%test "Parse field 1" =
apply_parser parse_field_member {|public int X;|}
= Ok (VarField ([ MPublic ], TypeVar (TypeBase TypeInt), Id "X", None))
;;
let%test "Parse field 2" =
apply_parser parse_field_member {|public int X = 1;|}
= Ok
(VarField
( [ MPublic ]
, TypeVar (TypeBase TypeInt)
, Id "X"
, Some (EBinOp (OpAssign, EId (Id "X"), EValue (ValInt 1))) ))
;;
let%test "Parse method 1" =
apply_parser parse_method_member {|public int Func() {}|}
= Ok (Method ([ MPublic ], TypeBase TypeInt, Id "Func", Params [], SBlock []))
;;
let%test "Parse method 2" =
apply_parser
parse_method_member
{|public int Func()
{
return 2;
}|}
= Ok
(Method
( [ MPublic ]
, TypeBase TypeInt
, Id "Func"
, Params []
, SBlock [ SReturn (Some (EValue (ValInt 2))) ] ))
;;
let%test "Parse method 3" =
apply_parser
parse_method_member
{|public int Factorial(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * Factorial(n - 1);
}
}|}
= Ok
(Method
( [ MPublic ]
, TypeBase TypeInt
, Id "Factorial"
, Params [ Var (TypeVar (TypeBase TypeInt), Id "n") ]
, SBlock
[ SIf
( EBinOp (OpEqual, EId (Id "n"), EValue (ValInt 0))
, SBlock [ SReturn (Some (EValue (ValInt 1))) ]
, Some
(SBlock
[ SReturn
(Some
(EBinOp
( OpMul
, EId (Id "n")
, EFuncCall
( EId (Id "Factorial")
, Args
[ EBinOp (OpSub, EId (Id "n"), EValue (ValInt 1))
] ) )))
]) )
] ))
;;
let%test "Parse class 1" =
apply_parser
parse_class
{|
public class Sample {}|}
= Ok (Class ([ MPublic ], Id "Sample", []))
;;
let%test "Parse class 2" =
apply_parser
parse_class
{|
public class Sample {
public int X;
public int Y = 1;
}|}
= Ok
(Class
( [ MPublic ]
, Id "Sample"
, [ VarField ([ MPublic ], TypeVar (TypeBase TypeInt), Id "X", None)
; VarField
( [ MPublic ]
, TypeVar (TypeBase TypeInt)
, Id "Y"
, Some (EBinOp (OpAssign, EId (Id "Y"), EValue (ValInt 1))) )
] ))
;;
let%test "Parse class 3" =
apply_parser
parse_class
{|
public class Sample {
public int X;
public int add(int x) {
X = X + x;
}
}|}
= Ok
(Class
( [ MPublic ]
, Id "Sample"
, [ VarField ([ MPublic ], TypeVar (TypeBase TypeInt), Id "X", None)
; Method
( [ MPublic ]
, TypeBase TypeInt
, Id "add"
, Params [ Var (TypeVar (TypeBase TypeInt), Id "x") ]
, SBlock
[ SExpr
(EBinOp
( OpAssign
, EId (Id "X")
, EBinOp (OpAdd, EId (Id "X"), EId (Id "x")) ))
] )
] ))
;;
let%test "Parse factorial" =
apply_parser
parse_prog
{|
public class Program
{
public static void Main() {}
public int Factorial(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * Factorial(n - 1);
}
}
}
|}
= Ok
(Program
(Class
( [ MPublic ]
, Id "Program"
, [ Method ([ MPublic; MStatic ], TypeVoid, Id "Main", Params [], SBlock [])
; Method
( [ MPublic ]
, TypeBase TypeInt
, Id "Factorial"
, Params [ Var (TypeVar (TypeBase TypeInt), Id "n") ]
, SBlock
[ SIf
( EBinOp (OpEqual, EId (Id "n"), EValue (ValInt 0))
, SBlock [ SReturn (Some (EValue (ValInt 1))) ]
, Some
(SBlock
[ SReturn
(Some
(EBinOp
( OpMul
, EId (Id "n")
, EFuncCall
( EId (Id "Factorial")
, Args
[ EBinOp
( OpSub
, EId (Id "n")
, EValue (ValInt 1) )
] ) )))
]) )
] )
] )))
;;
let%test "Short: Parse program with weird whitespace" =
let program =
{|
class Program
{
static int Main()
{
return 42 ;
}
}
|}
in
match apply_parser parse_prog program with
| Ok _ -> true
| Error _ -> false
;;
let%test "Short: Parse checking fields" =
let program =
{|
class Program {
int b = 9;
int c = b * 67;
int a = (50 % 2) + b - c;
bool r = (a != b * c) || (a >= b) && (a == c +90);
string s = "ok";
char h = 'a';
void M() {
a = 5;
r = s!= "kkkk" && (190%22 == 100 * -2/5) ;
}
}
|}
in
match apply_parser parse_prog program with
| Ok _ -> true
| Error _ -> false
;;