aboutsummaryrefslogtreecommitdiff
path: root/test/syntax.ml
blob: 3b7c2f90f9966c6a10db91ab79f372f2b215a969 (plain)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
module Tree = Qsp_syntax.Tree
module Ast = Tree.Ast
module Check = Qsp_syntax.Check
module S = Qsp_syntax.S
module T = Qsp_syntax.T

let location_id, e1 = Check.build (module Tree)

module Parser = Check.Make (struct
  let t = [| e1 |]
end)

let _position = (Lexing.dummy_pos, Lexing.dummy_pos)

type 'a location = 'a * 'a Ast.statement list [@@deriving eq, show]

let get_location :
    (S.pos location, Qsp_syntax.Report.t) result -> S.pos location = function
  | Ok e -> e
  | Error e ->
      let msg = Format.asprintf "%a" Qsp_syntax.Report.pp e in
      raise (Failure msg)

(** Run the parser with the given expression and return the result *)
let parse : string -> (S.pos location, Qsp_syntax.Report.t) result =
 fun content ->
  let lexing =
    Sedlexing.Latin1.from_string content |> Qparser.Lexbuf.from_lexbuf
  in
  Qparser.Analyzer.parse (module Parser) lexing
  |> Result.map (fun (location, _report) ->
         (* Uncatched excteptions here, but we are in the tests…
            If it’s fail here I have an error in the code. *)
         Array.get location 0 |> Check.get location_id |> Option.get)

let location : S.pos location Alcotest.testable =
  let equal = equal_location (fun _ _ -> true) in
  let pp =
    pp_location (fun formater _ -> Format.fprintf formater "_position")
  in
  Alcotest.testable pp equal

let test_empty_location () =
  let expected = (_position, [])
  and actual = get_location @@ parse {|# Location
------- |}
  and msg = "Empty location" in

  Alcotest.(check' location ~msg ~expected ~actual)

let test_location_without_space () =
  let expected = (_position, [])
  and actual = get_location @@ parse {|#Location
------- |}
  and msg = "Empty location" in

  Alcotest.(check' location ~msg ~expected ~actual)

let test_location_without_database () =
  let expected = (_position, [])
  and actual = get_location @@ parse {|# $Location
------- |}
  and msg = "Location without database" in

  let () = Alcotest.(check' location ~msg ~expected ~actual) in

  let actual = get_location @@ parse {|# !Location
------- |} in
  let () = Alcotest.(check' location ~msg ~expected ~actual) in

  let actual = get_location @@ parse {|# ^Location
------- |} in
  Alcotest.(check' location ~msg ~expected ~actual)

let _test_instruction : string -> S.pos Ast.statement list -> unit =
 fun literal expected ->
  let expected = (_position, expected)
  and _location = Printf.sprintf {|# Location
%s
------- |} literal in
  let actual = get_location @@ parse _location and msg = literal in

  Alcotest.(check' location ~msg ~expected ~actual)

let test_numeric_expression () =
  _test_instruction "123" [ Expression (Integer (_position, "123")) ]

let test_negative_numeric_expression () =
  _test_instruction "-123"
    [
      Tree.Ast.Expression
        (Tree.Ast.Op (_position, T.Neg, Tree.Ast.Integer (_position, "123")));
    ]

let test_negative_numeric_expression2 () =
  let index = None in
  let var = { Ast.pos = _position; name = "CURTIMESUN"; index } in
  _test_instruction "-(780-CurTimeSun)"
    Ast.
      [
        Expression
          (Op
             ( _position,
               Neg,
               BinaryOp (_position, Minus, Integer (_position, "780"), Ident var)
             ));
      ]

let test_str_variable () =
  let index = None in
  let var = { Ast.pos = _position; name = "$VALUE"; index } in
  _test_instruction "$value" [ Expression (Ident var) ]

let test_variable () =
  let index = None in
  let var = { Ast.pos = _position; name = "VALUE"; index } in
  _test_instruction "value" [ Expression (Ident var) ]

let test_indexed_variable () =
  let index = Some Ast.(Integer (_position, "1")) in
  let var = { Ast.pos = _position; name = "VALUE"; index } in
  _test_instruction "value[1]" [ Expression (Ident var) ]

let test_let_literal () =
  let index = None in
  let var = { Ast.pos = _position; name = "VALUE"; index } in
  _test_instruction "let value = '123'"
    Ast.
      [
        Declaration (_position, var, Eq', Literal (_position, [ T.Text "123" ]));
      ]

let test_set_array_append () =
  let var = { Ast.pos = _position; name = "$VALUE"; index = None } in
  _test_instruction "set $value[] = ''"
    Ast.
      [ Declaration (_position, var, Eq', Literal (_position, [ T.Text "" ])) ]

let test_direct_assignation () =
  let index = None in
  let var = { Ast.pos = _position; name = "VALUE"; index } in
  _test_instruction "value = '123'"
    Ast.
      [
        Declaration (_position, var, Eq', Literal (_position, [ T.Text "123" ]));
      ]

let test_command_assignation () =
  let index = None in
  let st_1 = { Ast.pos = _position; name = "ST_1"; index } in
  _test_instruction "st_1 = input 'Enter the amount'"
    Ast.
      [
        Declaration
          ( _position,
            st_1,
            Eq',
            Function
              ( _position,
                Input,
                [ Literal (_position, [ T.Text "Enter the amount" ]) ] ) );
      ]

let test_assignation2 () =
  let index = None in
  let var = { Ast.pos = _position; name = "VALUE"; index } in
  _test_instruction "set value += 123"
    Ast.[ Declaration (_position, var, Inc, Integer (_position, "123")) ]

let test_multilie_literal () =
  let index = None in
  let var = { Ast.pos = _position; name = "VALUE"; index } in
  _test_instruction {| 
  value = {
$a = '123'
}|}
    Ast.
      [
        Declaration
          (_position, var, Eq', Literal (_position, [ T.Text "\n$a = '123'\n" ]));
      ]

let test_nested_literal () =
  _test_instruction
    {| 
  value = {

      {

      }
}|}
    [
      Ast.Declaration
        ( _position,
          { Ast.pos = _position; name = "VALUE"; index = None },
          Qsp_syntax.T.Eq',
          Ast.Literal (_position, [ T.Text "\n\n      {\n\n      }\n" ]) );
    ]

let test_concat_literal () =
  _test_instruction {| 
  '123'
  +'456'
|}
    [
      Ast.Expression (Ast.Literal (_position, [ T.Text "123" ]));
      Ast.Expression
        (Ast.Op
           ( _position,
             Qsp_syntax.T.Add,
             Ast.Literal (_position, [ T.Text "456" ]) ));
    ]

let test_literal () =
  _test_instruction "'123'"
    [ Expression (Literal (_position, [ T.Text "123" ])) ]

let test_qutoted_literal () =
  _test_instruction "'12''3'"
    [ Expression (Literal (_position, [ T.Text "12'3" ])) ]

let test_multiline1 () =
  let content = {|
apples  = 5
pears   = 10
|} in

  let index = None in
  let apples = { Ast.pos = _position; name = "APPLES"; index }
  and pears = { Ast.pos = _position; name = "PEARS"; index }
  and value_5 = Ast.Integer (_position, "5")
  and value_10 = Ast.Integer (_position, "10") in
  _test_instruction content
    Ast.
      [
        Declaration (_position, apples, Eq', value_5);
        Declaration (_position, pears, Eq', value_10);
      ]

let test_multiline2 () =
  let content = "apples = 5 & pears = 10" in

  let index = None in
  let apples = { Ast.pos = _position; name = "APPLES"; index }
  and pears = { Ast.pos = _position; name = "PEARS"; index }
  and value_5 = Ast.Integer (_position, "5")
  and value_10 = Ast.Integer (_position, "10") in
  _test_instruction content
    [
      Declaration (_position, apples, Eq', value_5);
      Declaration (_position, pears, Eq', value_10);
    ]

let test_equality () =
  let content = "apples = 5 = pears" in
  let index = None in
  let apples = { Ast.pos = _position; name = "APPLES"; index }
  and pears = { Ast.pos = _position; name = "PEARS"; index }
  and value_5 = Ast.Integer (_position, "5") in
  _test_instruction content
    [
      Declaration
        (_position, apples, Eq', BinaryOp (_position, Eq, value_5, Ident pears));
    ]

let test_plus () =
  let content = {|
apples = 5 + pears
|} in
  let index = None in
  let apples = { Ast.pos = _position; name = "APPLES"; index }
  and pears = { Ast.pos = _position; name = "PEARS"; index }
  and value_5 = Ast.Integer (_position, "5") in
  _test_instruction content
    [
      Declaration
        ( _position,
          apples,
          Eq',
          BinaryOp (_position, Plus, value_5, Ident pears) );
    ]

let test_plus_litt () =
  let content = {|
'five'+ pears
|} in
  let index = None in
  let pears = { Ast.pos = _position; name = "PEARS"; index } in
  _test_instruction content
    [
      Ast.(
        Expression
          (BinaryOp
             ( _position,
               Plus,
               Literal (_position, [ T.Text "five" ]),
               Ident pears )));
    ]

let test_concat () =
  let content = {|
$firstName + ' ' + $lastName
|} in
  _test_instruction content
    [
      Tree.Ast.Expression
        (Tree.Ast.BinaryOp
           ( _position,
             Plus,
             Tree.Ast.BinaryOp
               ( _position,
                 Plus,
                 Tree.Ast.Ident
                   {
                     Tree.Ast.pos = _position;
                     name = "$FIRSTNAME";
                     index = None;
                   },
                 Tree.Ast.Literal (_position, [ T.Text " " ]) ),
             Tree.Ast.Ident
               { Tree.Ast.pos = _position; name = "$LASTNAME"; index = None } ));
    ]

let test_mod () =
  _test_instruction {|2 mod 1|}
    [
      Tree.Ast.Expression
        (Tree.Ast.BinaryOp
           ( _position,
             T.Mod,
             Tree.Ast.Integer (_position, "2"),
             Tree.Ast.Integer (_position, "1") ));
    ]

let test_comment () = _test_instruction "! Comment" [ Comment _position ]

let test_comment2 () =
  let index = None in
  let a = { Ast.pos = _position; name = "A"; index }
  and value_0 = Ast.Integer (_position, "0") in
  _test_instruction "a = 0 &! Comment"
    Ast.[ Declaration (_position, a, Eq', value_0); Comment _position ]

let test_comment3 () = _test_instruction {|!!1234
  |} [ Comment _position ]

(** The exclamation mark here is an operation and not a comment *)
let test_comment4 () =
  let index = None in
  let a = { Ast.pos = _position; name = "A"; index }
  and value_0 = Ast.Integer (_position, "0") in
  _test_instruction "a = rand(0, 1) ! 0"
    [
      Ast.(
        Declaration
          ( _position,
            a,
            Eq',
            BinaryOp
              ( _position,
                Neq,
                Function
                  ( _position,
                    Rand,
                    [ Integer (_position, "0"); Integer (_position, "1") ] ),
                value_0 ) ));
    ]

let test_comment5 () =
  _test_instruction "a = rand() &! Comment"
    [
      Ast.Declaration
        ( _position,
          { Ast.pos = _position; name = "A"; index = None },
          Qsp_syntax.T.Eq',
          Ast.Function (_position, Rand, []) );
      Ast.Comment _position;
    ]

let test_comment6 () =
  _test_instruction
    "gs 'stat' &!! It should be here, because some of the strigs have to be \
     initialized"
    [
      Ast.Call
        ( _position,
          Qsp_syntax.T.Gosub,
          [ Ast.Literal (_position, [ T.Text "stat" ]) ] );
      Ast.Comment _position;
    ]

let test_long_comment () =
  _test_instruction
    {| !'this part of the comment is inside
single quotes' but "this is still part
of the same comment because sometimes
life is unfair." Oh yeah, {curly brackets
also count}. This is still the same comment. |}
    [ Comment _position ]

(** This test ensure that the unary operator is applied to the whole expression
 *)
let test_precedence () =
  let index = None in
  let x = Ast.Ident { Ast.pos = _position; name = "X"; index }
  and y = Ast.Ident { Ast.pos = _position; name = "Y"; index } in
  _test_instruction "no x = y"
    Ast.[ Expression (Op (_position, No, BinaryOp (_position, Eq, x, y))) ]

(** This test ensure that a ! is not considered as a comment in an 
    expression *)
let test_precedence2 () =
  let index = None in
  let x = { Ast.pos = _position; name = "X"; index }
  and y = Ast.Ident { Ast.pos = _position; name = "Y"; index } in
  _test_instruction "x = y ! 0"
    Ast.
      [
        Declaration
          ( _position,
            x,
            Eq',
            BinaryOp (_position, Neq, y, Integer (_position, "0")) );
      ]

let test_if () =
  let index = Some Ast.(Integer (_position, "0")) in
  let args = Ast.(Ident { pos = _position; name = "$ARGS"; index })
  and expr1 = Ast.(Literal (_position, [ T.Text "blockA" ]))
  and expr2 =
    Ast.(Expression (Literal (_position, [ T.Text "You are in block A" ])))
  in
  _test_instruction {| if $ARGS[0] = 'blockA':
  'You are in block A'
end |}
    Ast.
      [
        If
          {
            loc = _position;
            then_ = (_position, BinaryOp (_position, Eq, args, expr1), [ expr2 ]);
            elifs = [];
            else_ = [];
          };
      ]

let test_if2 () =
  let index = Some Ast.(Integer (_position, "0")) in
  let args = Ast.(Ident { pos = _position; name = "$ARGS"; index })
  and expr1 = Ast.(Literal (_position, [ T.Text "blockA" ]))
  and expr2 =
    Ast.(Expression (Literal (_position, [ T.Text "You are in block A" ])))
  in
  _test_instruction {| if $ARGS[0] = 'blockA':
  'You are in block A'
end if|}
    Ast.
      [
        If
          {
            loc = _position;
            then_ = (_position, BinaryOp (_position, Eq, args, expr1), [ expr2 ]);
            elifs = [];
            else_ = [];
          };
      ]

let test_if_chained () =
  let value_0 = Ast.Integer (_position, "0") in
  _test_instruction {| if 0:
  0
end &! -- |}
    Ast.
      [
        If
          {
            loc = _position;
            then_ = (_position, value_0, [ Expression value_0 ]);
            elifs = [];
            else_ = [];
          };
        Comment _position;
      ]

let test_if_equality () =
  _test_instruction {|
if 0 = 0:
end &! --
|}
    [
      Ast.If
        {
          loc = _position;
          then_ =
            ( _position,
              Ast.BinaryOp
                ( _position,
                  Qsp_syntax.T.Eq,
                  Ast.Integer (_position, "0"),
                  Ast.Integer (_position, "0") ),
              [] );
          elifs = [];
          else_ = [];
        };
      Ast.Comment _position;
    ]

let test_if_inline () =
  let value_0 = Ast.Integer (_position, "0") in
  _test_instruction "if 0: 0 else 0"
    Ast.
      [
        If
          {
            loc = _position;
            then_ = (_position, value_0, [ Expression value_0 ]);
            elifs = [];
            else_ = [ Expression value_0 ];
          };
      ]

let test_if_inline_comment () =
  let value_0 = Ast.Integer (_position, "0") in
  _test_instruction "if 0: 0 else 0 &! comment "
    Ast.
      [
        If
          {
            loc = _position;
            then_ = (_position, value_0, [ Expression value_0 ]);
            elifs = [];
            else_ = [ Expression value_0 ];
          };
        Comment _position;
      ]

let test_if_inline_comment2 () =
  _test_instruction "if 0: 1 & !! Comment"
    [
      Ast.If
        {
          loc = _position;
          then_ =
            ( _position,
              Ast.Integer (_position, "0"),
              [
                Ast.Expression (Ast.Integer (_position, "1"));
                Ast.Comment _position;
              ] );
          elifs = [];
          else_ = [];
        };
    ]

let test_if_inline_act () =
  _test_instruction "if 1 and hour >= 8: minut += 1 & act 'go': gt 'go'"
    [
      Ast.If
        {
          loc = _position;
          then_ =
            ( _position,
              Ast.BinaryOp
                ( _position,
                  Qsp_syntax.T.And,
                  Ast.Integer (_position, "1"),
                  Ast.BinaryOp
                    ( _position,
                      Qsp_syntax.T.Gte,
                      Ast.Ident
                        { Ast.pos = _position; name = "HOUR"; index = None },
                      Ast.Integer (_position, "8") ) ),
              [
                Ast.Declaration
                  ( _position,
                    { Ast.pos = _position; name = "MINUT"; index = None },
                    Qsp_syntax.T.Inc,
                    Ast.Integer (_position, "1") );
                Ast.Act
                  {
                    loc = _position;
                    label = Ast.Literal (_position, [ T.Text "go" ]);
                    statements =
                      [
                        Ast.Call
                          ( _position,
                            Qsp_syntax.T.Goto,
                            [ Ast.Literal (_position, [ T.Text "go" ]) ] );
                      ];
                  };
              ] );
          elifs = [];
          else_ = [];
        };
    ]

let test_if_multiline () =
  _test_instruction {|if 1  _
  and _hour >= 8: 1|}
    [
      Ast.If
        {
          loc = _position;
          then_ =
            ( _position,
              Ast.BinaryOp
                ( _position,
                  Qsp_syntax.T.And,
                  Ast.Integer (_position, "1"),
                  Ast.BinaryOp
                    ( _position,
                      Qsp_syntax.T.Gte,
                      Ast.Ident
                        { Ast.pos = _position; name = "_HOUR"; index = None },
                      Ast.Integer (_position, "8") ) ),
              [ Tree.Ast.Expression (Tree.Ast.Integer (_position, "1")) ] );
          elifs = [];
          else_ = [];
        };
    ]

let test_if_inline_act2 () =
  _test_instruction "if 1: act 'go': gt 'go' &! comment "
    [
      Ast.If
        {
          loc = _position;
          then_ =
            ( _position,
              Ast.Integer (_position, "1"),
              [
                Ast.Act
                  {
                    loc = _position;
                    label = Ast.Literal (_position, [ T.Text "go" ]);
                    statements =
                      [
                        Ast.Call
                          ( _position,
                            Qsp_syntax.T.Goto,
                            [ Ast.Literal (_position, [ T.Text "go" ]) ] );
                        Ast.Comment _position;
                      ];
                  };
              ] );
          elifs = [];
          else_ = [];
        };
    ]

let test_precedence3 () =
  let index = Some Ast.(Integer (_position, "0")) in
  let args = Ast.(Ident { pos = _position; name = "$ARGS"; index })
  and expr1 = Ast.(Literal (_position, [ T.Text "blockA" ]))
  and expr2 =
    Ast.(Expression (Literal (_position, [ T.Text "You are in block A" ])))
  and expr3 = Ast.(Expression (Integer (_position, "0"))) in
  _test_instruction {| if $ARGS[0] = 'blockA': 'You are in block A' & 0|}
    Ast.
      [
        If
          {
            loc = _position;
            then_ =
              ( _position,
                BinaryOp (_position, Eq, args, expr1),
                [ expr2; expr3 ] );
            elifs = [];
            else_ = [];
          };
      ]

let test_gs () =
  _test_instruction "gs '123'"
    [
      Ast.(
        Call
          ( _position,
            Qsp_syntax.T.Gosub,
            [ Literal (_position, [ T.Text "123" ]) ] ));
    ]

let test_gt () =
  _test_instruction "gt $curloc"
    [
      Ast.Call
        ( _position,
          Qsp_syntax.T.Goto,
          [ Ast.Ident { Ast.pos = _position; name = "$CURLOC"; index = None } ]
        );
    ]

let test_nl () =
  _test_instruction "*NL 'It'"
    [
      Ast.Call
        ( _position,
          Qsp_syntax.T.Nl',
          [ Ast.Literal (_position, [ T.Text "It" ]) ] );
    ]

let test_function () =
  _test_instruction "iif(123, 1, 0)"
    [
      Ast.(
        Expression
          (Function
             ( _position,
               Iif,
               [
                 Integer (_position, "123");
                 Integer (_position, "1");
                 Integer (_position, "0");
               ] )));
    ]

(** Include a space before the parameters *)
let test_function2 () =
  _test_instruction "rand (0, 1)"
    [
      Ast.(
        Expression
          (Function
             ( _position,
               Rand,
               [ Integer (_position, "0"); Integer (_position, "1") ] )));
    ]

let test_precedence4 () =
  _test_instruction "trim()" Ast.[ Expression (Function (_position, Trim, [])) ]

(** This should not be a keyword without arguments, followed by an expression *)
let test_precedence5 () =
  _test_instruction "clear()" Ast.[ Call (_position, Qsp_syntax.T.Clear, []) ]

let test_precedence6 () =
  _test_instruction "(1 = 0 and 2 ! 3)"
    [
      Ast.Expression
        (Ast.BinaryOp
           ( _position,
             And,
             Ast.BinaryOp
               ( _position,
                 Eq,
                 Ast.Integer (_position, "1"),
                 Ast.Integer (_position, "0") ),
             Ast.BinaryOp
               ( _position,
                 Neq,
                 Ast.Integer (_position, "2"),
                 Ast.Integer (_position, "3") ) ));
    ]

(** An identifier cannot start by a number *0 is a product and not an
    identifier *)
let test_operator () =
  let index = None in
  let a = { Ast.pos = _position; name = "A"; index }
  and value_0 = Ast.Integer (_position, "0") in
  _test_instruction "a *0"
    Ast.[ Expression (BinaryOp (_position, Product, Ident a, value_0)) ]

let test_operator2 () =
  let value_0 = Ast.Integer (_position, "0") in
  _test_instruction "0 *rand()"
    Ast.
      [
        Expression
          (BinaryOp (_position, Product, value_0, Function (_position, Rand, [])));
      ]

let test_dyneval () =
  _test_instruction "dyneval ''"
    [
      Ast.Expression
        (Ast.Function
           (_position, Dyneval, [ Ast.Literal (_position, [ T.Text "" ]) ]));
    ]

(** The parens after input are considered as arguments for the function, not a
    following expression. 

    This expression is a boolean.
*)
let test_input () =
  _test_instruction "( input('') = '' )"
    [
      Tree.Ast.Expression
        (Tree.Ast.BinaryOp
           ( _position,
             Eq,
             Tree.Ast.Function
               ( _position,
                 Input,
                 [ Tree.Ast.Literal (_position, [ T.Text "" ]) ] ),
             Tree.Ast.Literal (_position, [ T.Text "" ]) ));
    ]

let test_mutiple_inline_ifs () =
  _test_instruction "if 1 > 0: 1 else if 1 < 0: 0"
    [
      Tree.Ast.If
        {
          loc = _position;
          then_ =
            ( _position,
              Tree.Ast.BinaryOp
                ( _position,
                  Gt,
                  Tree.Ast.Integer (_position, "1"),
                  Tree.Ast.Integer (_position, "0") ),
              [ Tree.Ast.Expression (Tree.Ast.Integer (_position, "1")) ] );
          elifs = [];
          else_ =
            [
              Tree.Ast.If
                {
                  loc = _position;
                  then_ =
                    ( _position,
                      Tree.Ast.BinaryOp
                        ( _position,
                          Lt,
                          Tree.Ast.Integer (_position, "1"),
                          Tree.Ast.Integer (_position, "0") ),
                      [
                        Tree.Ast.Expression (Tree.Ast.Integer (_position, "0"));
                      ] );
                  elifs = [];
                  else_ = [];
                };
            ];
        };
    ]

(** The boolean comparaison has greater precedence than arithmetic operator *)
let test_precedence7 () =
  _test_instruction "(1 + 1 = '')"
    [
      Tree.Ast.Expression
        (Tree.Ast.BinaryOp
           ( _position,
             Eq,
             Tree.Ast.BinaryOp
               ( _position,
                 Plus,
                 Tree.Ast.Integer (_position, "1"),
                 Tree.Ast.Integer (_position, "1") ),
             Tree.Ast.Literal (_position, [ T.Text "" ]) ));
    ]

(** The OR operator has greater precedence than boolean comparaison *)
let test_precedence8 () =
  _test_instruction "(0 = 1 or 0 = 1)"
    [
      Tree.Ast.Expression
        (Tree.Ast.BinaryOp
           ( _position,
             Or,
             Tree.Ast.BinaryOp
               ( _position,
                 Eq,
                 Tree.Ast.Integer (_position, "0"),
                 Tree.Ast.Integer (_position, "1") ),
             Tree.Ast.BinaryOp
               ( _position,
                 Eq,
                 Tree.Ast.Integer (_position, "0"),
                 Tree.Ast.Integer (_position, "1") ) ));
    ]

(** Test showing the - should be considered as an operator and cannot be 
    aggregated inside the integer value. *)
let minus_operator () =
  _test_instruction {|day-7|}
    [
      Tree.Ast.Expression
        (Tree.Ast.BinaryOp
           ( _position,
             T.Minus,
             Tree.Ast.Ident
               { Tree.Ast.pos = _position; name = "DAY"; index = None },
             Tree.Ast.Integer (_position, "7") ));
    ]

(** STATTXT was considered as a function and raised an error in the syntax *)
let test_stattxt () =
  _test_instruction "$value = $stattxt"
    [
      Tree.Ast.Declaration
        ( _position,
          { Tree.Ast.pos = _position; name = "$VALUE"; index = None },
          T.Eq',
          Tree.Ast.Ident
            { Tree.Ast.pos = _position; name = "$STATTXT"; index = None } );
    ]

let test =
  ( "Syntax",
    [
      Alcotest.test_case "Location" `Quick test_empty_location;
      Alcotest.test_case "Location" `Quick test_location_without_space;
      Alcotest.test_case "Location" `Quick test_location_without_database;
      Alcotest.test_case " Numeric expression" `Quick test_numeric_expression;
      Alcotest.test_case "-Numeric expression" `Quick
        test_negative_numeric_expression;
      Alcotest.test_case "-Numeric expression2" `Quick
        test_negative_numeric_expression2;
      Alcotest.test_case "Minus op" `Quick minus_operator;
      Alcotest.test_case "$Variable expression" `Quick test_str_variable;
      Alcotest.test_case " Variable expression" `Quick test_variable;
      Alcotest.test_case "Indexed Variable expression" `Quick
        test_indexed_variable;
      Alcotest.test_case "Let instruction" `Quick test_let_literal;
      Alcotest.test_case "Set array_append" `Quick test_set_array_append;
      Alcotest.test_case "Variable_assignation" `Quick test_direct_assignation;
      Alcotest.test_case "Command assignation" `Quick test_command_assignation;
      Alcotest.test_case "Variable_assignation2" `Quick test_assignation2;
      Alcotest.test_case "Literal" `Quick test_literal;
      Alcotest.test_case "Literal2" `Quick test_qutoted_literal;
      Alcotest.test_case "Literal3" `Quick test_multilie_literal;
      Alcotest.test_case "Concat Literal" `Quick test_concat_literal;
      Alcotest.test_case "Nested Literal" `Quick test_nested_literal;
      Alcotest.test_case "Multiline1" `Quick test_multiline1;
      Alcotest.test_case "Multiline2" `Quick test_multiline2;
      Alcotest.test_case "Equality" `Quick test_equality;
      Alcotest.test_case "Plus" `Quick test_plus;
      Alcotest.test_case "Plus_litt" `Quick test_plus_litt;
      Alcotest.test_case "PlusChained" `Quick test_concat;
      Alcotest.test_case "Mod operator" `Quick test_mod;
      Alcotest.test_case "Comment" `Quick test_comment;
      Alcotest.test_case "Comment2" `Quick test_comment2;
      Alcotest.test_case "Comment3" `Quick test_comment3;
      Alcotest.test_case "Comment4" `Quick test_comment4;
      Alcotest.test_case "Comment5" `Quick test_comment5;
      Alcotest.test_case "Comment6" `Quick test_comment6;
      Alcotest.test_case "Multiline Comment" `Quick test_long_comment;
      Alcotest.test_case "If" `Quick test_if;
      Alcotest.test_case "If - end if" `Quick test_if2;
      Alcotest.test_case "If_chained" `Quick test_if_chained;
      Alcotest.test_case "If_equality" `Quick test_if_equality;
      Alcotest.test_case "If inline" `Quick test_if_inline;
      Alcotest.test_case "If inline &!" `Quick test_if_inline_comment;
      Alcotest.test_case "If inline & !!" `Quick test_if_inline_comment2;
      Alcotest.test_case "If : act" `Quick test_if_inline_act;
      Alcotest.test_case "If _ and " `Quick test_if_multiline;
      Alcotest.test_case "If : act: &!" `Quick test_if_inline_act2;
      Alcotest.test_case "Precedence1" `Quick test_precedence;
      Alcotest.test_case "Precedence2" `Quick test_precedence2;
      Alcotest.test_case "Precedence3" `Quick test_precedence3;
      Alcotest.test_case "Call gs" `Quick test_gs;
      Alcotest.test_case "Call gt" `Quick test_gt;
      Alcotest.test_case "Call nl" `Quick test_nl;
      Alcotest.test_case "Function iif" `Quick test_function;
      Alcotest.test_case "Function rand" `Quick test_function2;
      Alcotest.test_case "Precedence4" `Quick test_precedence4;
      Alcotest.test_case "Precedence5" `Quick test_precedence5;
      Alcotest.test_case "Precedence6" `Quick test_precedence6;
      Alcotest.test_case "Operator" `Quick test_operator;
      Alcotest.test_case "Operator2" `Quick test_operator2;
      Alcotest.test_case "Dyneval" `Quick test_dyneval;
      Alcotest.test_case "Input" `Quick test_input;
      Alcotest.test_case "inline if else if" `Quick test_mutiple_inline_ifs;
      Alcotest.test_case "Precedence7" `Quick test_precedence7;
      Alcotest.test_case "Precedence8" `Quick test_precedence8;
      Alcotest.test_case "stattxt" `Quick test_stattxt;
    ] )