aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/macro/template.lux
blob: 12e503e33c5b203702d1954ce87fee0a248fc910 (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." try]
    ["." exception]]
   [data
    [collection
     ["." list]]]
   ["." macro
    [syntax (#+ syntax:)]
    ["." code]]
   [math
    ["." random (#+ Random)]
    [number
     ["." nat]]]]
  [\\
   ["." /]])

(/.let [(!pow/2 <scalar>)
        [(nat.* <scalar> <scalar>)]]
  (def: pow/2
    (-> Nat Nat)
    (|>> !pow/2)))

(syntax: (macro_error macro)
  (function (_ compiler)
    (case ((macro.expand macro) compiler)
      (#try.Failure error)
      (#try.Success [compiler (list (code.text error))])
      
      (#try.Success _)
      (#try.Failure "OOPS!"))))

(def: #export test
  Test
  (<| (_.covering /._)
      (do {! random.monad}
        [left random.nat
         mid random.nat
         right random.nat]
        (with_expansions [<module> (as_is [.5 -4 +3 2 #1 #0 #c b "a"])
                          <module>' ".5-4+32#1#0cba"
                          <short> (as_is ["a" b #c #0 #1 2 +3 -4 .5])
                          <short>' "abc#0#12+3-4.5"]
          ($_ _.and
              (_.cover [/.splice]
                       (\ (list.equivalence nat.equivalence) =
                          (list left mid right)
                          (`` (list (~~ (/.splice [left mid right]))))))
              (_.cover [/.count]
                       (case (/.count [left mid right])
                         3 true
                         _ false))
              (_.cover [/.text]
                       (case (/.text <short>)
                         <short>' true
                         _ false))
              (_.cover [/.identifier]
                       (and (case (`` (name_of (~~ (/.identifier <short>))))
                              ["" <short>'] true
                              _ false)
                            (case (`` (name_of (~~ (/.identifier <module> <short>))))
                              [<module>' <short>'] true
                              _ false)
                            ))
              (_.cover [/.tag]
                       (and (case (`` (name_of (~~ (/.tag <short>))))
                              ["" <short>'] true
                              _ false)
                            (case (`` (name_of (~~ (/.tag <module> <short>))))
                              [<module>' <short>'] true
                              _ false)
                            ))
              (_.cover [/.with_locals]
                       (/.with_locals [var0 var1]
                         (let [var0 left
                               var1 right]
                           (and (nat.= left var0)
                                (nat.= right var1)))))
              (do !
                [scalar random.nat]
                (_.cover [/.let]
                         (let [can_use_with_statements!
                               (nat.= ($_ nat.* scalar scalar)
                                      (..pow/2 scalar))]
                           (and can_use_with_statements!
                                (/.let [(pow/3 <scalar>)
                                        [($_ nat.* <scalar> <scalar> <scalar>)]

                                        (pow/9 <scalar>)
                                        [(pow/3 (pow/3 <scalar>))]]
                                  (let [can_use_with_expressions!
                                        (nat.= ($_ nat.* scalar scalar scalar)
                                               (pow/3 scalar))

                                        can_refer!
                                        (nat.= ($_ nat.*
                                                   scalar scalar scalar
                                                   scalar scalar scalar
                                                   scalar scalar scalar)
                                               (pow/9 scalar))

                                        can_shadow!
                                        (let [pow/3 (function (_ scalar)
                                                      ($_ nat.+ scalar scalar scalar))]
                                          (nat.= ($_ nat.+ scalar scalar scalar)
                                                 (pow/3 scalar)))]
                                    (and can_use_with_expressions!
                                         can_refer!
                                         can_shadow!)))
                                ))))
              (_.cover [/.irregular_arguments]
                       (/.let [(arity/3 <0> <1> <2>)
                               [""]]
                         (exception.match? /.irregular_arguments
                                           (macro_error (arity/3 "a" "b")))))
              )))
      ))