blob: eca7eac029e001b1a47cad3cbaf687db69fcc947 (
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
|
(.module:
[lux (#- Definition)
["." meta]
[abstract
[equivalence (#+ Equivalence)]
[monad (#+ do)]]
[control
["." exception (#+ exception:)]
["<>" parser
["<.>" code (#+ Parser)]]]
[data
["." sum]
["." product]
["." bit]
["." name]
["." text
["%" format (#+ format)]]
[collection
["." list]]]
[macro
["." code]]
[meta
["." location]]]
["." // (#+ Annotations)
["#." check (#+ Check)]])
(type: #export Definition
{#name Text
#value (Either Check
Code)
#anns Annotations
#export? Bit})
(def: #export equivalence
(Equivalence Definition)
($_ product.equivalence
text.equivalence
($_ sum.equivalence
//check.equivalence
code.equivalence
)
(list.equivalence (product.equivalence name.equivalence
code.equivalence))
bit.equivalence
))
(def: extension
"lux def")
(def: (write_tag [module short])
(-> Name Code)
(` [(~ (code.text module))
(~ (code.text short))]))
(def: (write_annotations value)
(-> Annotations Code)
(case value
#.Nil
(` #.Nil)
(#.Cons [name value] tail)
(` (#.Cons [(~ (..write_tag name))
(~ value)]
(~ (write_annotations 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: #export (write (^slots [#name #value #anns #export?]))
(-> Definition Code)
(` ((~ (code.text ..extension))
(~ (code.local_identifier name))
(~ (case value
(#.Left check)
(//check.write check)
(#.Right value)
value))
[(~ ..dummy) (#.Record (~ (..write_annotations 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 #.Nil))
(<code>.form (do <>.monad
[_ (<code>.tag! (name_of #.Cons))
[head tail] (<>.and (<code>.tuple (<>.and tag_parser <code>.any))
recur)]
(wrap [head tail])))
))))
(def: #export (parser compiler)
{#.doc "A reader that first macro-expands and then analyses the input Code, to ensure it's a definition."}
(-> Lux (Parser Definition))
(do {! <>.monad}
[raw <code>.any
me_raw (|> raw
meta.expand_all
(meta.run compiler)
<>.lift)]
(<| (<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: #export (lacks_type! {definition Definition})
(exception.report
["Definition" (%.code (..write definition))]))
(def: #export (typed compiler)
{#.doc "Only works for typed definitions."}
(-> Lux (Parser Definition))
(do <>.monad
[definition (..parser compiler)
_ (case (get@ #value definition)
(#.Left _)
(wrap [])
(#.Right _)
(<>.lift (exception.throw ..lacks_type! [definition])))]
(wrap definition)))
|