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

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

(syntax: (macro_error [macro <code>.any])
  (function (_ compiler)
    (case ((macro.expansion macro) compiler)
      {#try.Failure error}
      {#try.Success [compiler (list (code.text error))]}
      
      {#try.Success _}
      {#try.Failure "OOPS!"})))

(def: .public 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 [/.spliced]
                       (\ (list.equivalence nat.equivalence) =
                          (list left mid right)
                          (`` (list (~~ (/.spliced [left mid right]))))))
              (_.cover [/.amount]
                       (case (/.amount [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")))))
              )))
      ))