aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/macro/syntax/definition.lux
blob: 7ebf281d4b977fabd9aea5880ed6478a35ae0eac (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
(.module:
  [library
   [lux (#- Definition)
    [abstract
     [equivalence (#+ Equivalence)]
     [monad (#+ do)]]
    [control
     ["." exception (#+ exception:)]
     ["<>" parser
      ["<.>" code (#+ Parser)]]]
    [data
     ["." sum]
     ["." product]
     ["." bit]
     ["." name]
     ["." text
      ["%" format]]
     [collection
      ["." list]]]
    ["." macro
     ["." code]]
    ["." meta
     ["." location]]]]
  ["." //
   ["#." annotations (#+ Annotations)]
   ["#." check (#+ Check)]])

(type: .public Definition
  {#.doc (example "Syntax for a constant definition.")}
  {#name Text
   #value (Either Check
                  Code)
   #anns Annotations
   #export? Bit})

(def: .public equivalence
  (Equivalence Definition)
  ($_ product.equivalence
      text.equivalence
      ($_ sum.equivalence
          //check.equivalence
          code.equivalence
          )
      //annotations.equivalence
      bit.equivalence
      ))

(def: extension
  "lux def")

(def: (tag_format [module short])
  (-> Name Code)
  (` [(~ (code.text module))
      (~ (code.text short))]))

(def: (annotations_format value)
  (-> Annotations Code)
  (case value
    #.End
    (` #.End)
    
    (#.Item [name value] tail)
    (` (#.Item [(~ (..tag_format name))
                (~ value)]
               (~ (annotations_format tail))))))

(def: dummy
  Code
  (` {#.module (~ (code.text (get@ #.module location.dummy)))
      #.line   (~ (code.nat (get@ #.line location.dummy)))
      #.column (~ (code.nat (get@ #.column location.dummy)))}))

(def: .public (format (^slots [#name #value #anns #export?]))
  (-> Definition Code)
  (` ((~ (code.text ..extension))
      (~ (code.local_identifier name))
      (~ (case value
           (#.Left check)
           (//check.format check)

           (#.Right value)
           value))
      [(~ ..dummy) (#.Record (~ (..annotations_format anns)))]
      (~ (code.bit export?)))))

(def: tag_parser
  (Parser Name)
  (<code>.tuple (<>.and <code>.text <code>.text)))

(def: annotations_parser
  (Parser Annotations)
  (<>.rec
   (function (_ recur)
     ($_ <>.or
         (<code>.tag! (name_of #.End))
         (<code>.form (do <>.monad
                        [_ (<code>.tag! (name_of #.Item))
                         [head tail] (<>.and (<code>.tuple (<>.and tag_parser <code>.any))
                                             recur)]
                        (in [head tail])))
         ))))

(def: .public (parser compiler)
  {#.doc "A reader that first macro-expands and then analyses the input Code, to ensure it is a definition."}
  (-> Lux (Parser Definition))
  (do {! <>.monad}
    [raw <code>.any
     me_raw (|> raw
                macro.full_expansion
                (meta.result compiler)
                <>.lifted)]
    (<| (<code>.local me_raw)
        <code>.form
        (<>.after (<code>.text! ..extension))
        ($_ <>.and
            <code>.local_identifier
            (<>.or //check.parser
                   <code>.any)
            (<| <code>.tuple
                (<>.after <code>.any)
                <code>.form
                (<>.after (<code>.this! (` #.Record)))
                ..annotations_parser)
            <code>.bit
            ))))

(exception: .public (lacks_type! {definition Definition})
  (exception.report
   ["Definition" (%.code (..format definition))]))

(def: .public (typed compiler)
  {#.doc "Only works for typed definitions."}
  (-> Lux (Parser Definition))
  (do <>.monad
    [definition (..parser compiler)
     _ (case (get@ #value definition)
         (#.Left _)
         (in [])

         (#.Right _)
         (<>.lifted (exception.except ..lacks_type! [definition])))]
    (in definition)))