blob: af3b584b8490459c6c073849d14c57c8683d6672 (
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
|
(.module: {#.doc "Commons syntax readers."}
[lux #*
[control
monad
["p" parser ("parser/." Monad<Parser>)]]
[data
[name ("name/." Equivalence<Name>)]
["." product]
["." maybe]
[collection
["." list]]]]
["." //
["/." ///
["s" syntax (#+ syntax: Syntax)]]])
## Exports
(def: #export export
(Syntax Bit)
(p.either (p.after (s.this (' #export)) (parser/wrap #1))
(parser/wrap #0)))
## Declarations
(def: #export declaration
{#.doc (doc "A reader for declaration syntax."
"Such as:"
quux
(foo bar baz))}
(Syntax //.Declaration)
(p.either (p.and s.local-identifier
(parser/wrap (list)))
(s.form (p.and s.local-identifier
(p.many s.local-identifier)))))
## Annotations
(def: #export annotations
{#.doc "Reader for the common annotations syntax used by def: statements."}
(Syntax //.Annotations)
(s.record (p.some (p.and s.tag s.any))))
## Definitions
(def: check^
(Syntax [(Maybe Code) Code])
(p.either (s.form (do p.Monad<Parser>
[_ (s.this (' "lux check"))
type s.any
value s.any]
(wrap [(#.Some type) value])))
(p.and (parser/wrap #.None)
s.any)))
(def: _definition-anns-tag^
(Syntax Name)
(s.tuple (p.and s.text s.text)))
(def: (_definition-anns^ _)
(-> Any (Syntax //.Annotations))
(p.or (s.this (' #.Nil))
(s.form (do p.Monad<Parser>
[_ (s.this (' #.Cons))
[head tail] (p.and (s.tuple (p.and _definition-anns-tag^ s.any))
(_definition-anns^ []))]
(wrap [head tail])))
))
(def: (flat-list^ _)
(-> Any (Syntax (List Code)))
(p.either (do p.Monad<Parser>
[_ (s.this (' #.Nil))]
(wrap (list)))
(s.form (do p.Monad<Parser>
[_ (s.this (' #.Cons))
[head tail] (s.tuple (p.and s.any s.any))
tail (s.local (list tail) (flat-list^ []))]
(wrap (#.Cons head tail))))))
(do-template [<name> <type> <tag> <then>]
[(def: <name>
(Syntax <type>)
(<| s.tuple
(p.after s.any)
s.form
(do p.Monad<Parser>
[_ (s.this (' <tag>))]
<then>)))]
[tuple-meta^ (List Code) #.Tuple (flat-list^ [])]
[text-meta^ Text #.Text s.text]
)
(def: (find-definition-args meta-data)
(-> (List [Name Code]) (List Text))
(<| (maybe.default (list))
(: (Maybe (List Text)))
(case (list.find (|>> product.left (name/= ["lux" "func-args"])) meta-data)
(^multi (#.Some [_ value])
[(p.run (list value) tuple-meta^)
(#.Right [_ args])]
[(p.run args (p.some text-meta^))
(#.Right [_ args])])
(#.Some args)
_
#.None)))
(def: #export (definition compiler)
{#.doc "A reader that first macro-expands and then analyses the input Code, to ensure it's a definition."}
(-> Lux (Syntax //.Definition))
(do p.Monad<Parser>
[definition-raw s.any
me-definition-raw (|> definition-raw
////.expand-all
(////.run compiler)
s.lift)]
(s.local me-definition-raw
(s.form (do @
[_ (s.this (' "lux def"))
definition-name s.local-identifier
[?definition-type definition-value] check^
definition-anns s.any
definition-anns (s.local (list definition-anns)
(_definition-anns^ []))
#let [definition-args (find-definition-args definition-anns)]]
(wrap {#//.definition-name definition-name
#//.definition-type ?definition-type
#//.definition-anns definition-anns
#//.definition-value definition-value
#//.definition-args definition-args}))))))
(def: #export (typed-definition compiler)
{#.doc "A reader for definitions that ensures the input syntax is typed."}
(-> Lux (Syntax //.Definition))
(do p.Monad<Parser>
[_definition (definition compiler)
_ (case (get@ #//.definition-type _definition)
(#.Some _)
(wrap [])
#.None
(p.fail "Typed definition must have a type!"))]
(wrap _definition)))
(def: #export typed-input
{#.doc "Reader for the common typed-argument syntax used by many macros."}
(Syntax //.Typed-Input)
(s.record (p.and s.any s.any)))
(def: #export type-variables
{#.doc "Reader for the common type var/param used by many macros."}
(Syntax (List Text))
(s.tuple (p.some s.local-identifier)))
|