-
Notifications
You must be signed in to change notification settings - Fork 9
/
show.scala
239 lines (224 loc) · 8.73 KB
/
show.scala
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright 2022 Erik Erlandson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package coulomb.infra
object show:
import scala.quoted.*
import coulomb.*
import coulomb.define.*
import coulomb.infra.meta.*
def show[U](using Quotes, Type[U]): Expr[String] =
import quotes.reflect.*
val render = (tr: TypeRepr) => {
val AppliedType(_, List(_, a)) = tr: @unchecked
val ConstantType(StringConstant(abbv)) = a: @unchecked
abbv
}
val str = showrender(TypeRepr.of[U], render)
Expr(str)
def showFull[U](using Quotes, Type[U]): Expr[String] =
import quotes.reflect.*
val render = (tr: TypeRepr) => {
val AppliedType(_, List(n, _)) = tr: @unchecked
val ConstantType(StringConstant(name)) = n: @unchecked
name
}
val str = showrender(TypeRepr.of[U], render)
Expr(str)
def showrender(using
Quotes
)(
u: quotes.reflect.TypeRepr,
render: quotes.reflect.TypeRepr => String
): String =
import quotes.reflect.*
u match
// named unit defs take highest priority
case namedunit(nu) => render(nu)
// The policy goal here is that type aliases are never expanded
// (unless covered by some namedunit(_) above). However,
// scala's implicit resolution logic undermines this somewhat because it
// pre-dealiases type aliases, so the "outer" type is always dealiases
// by the time my metaprogramming sees it.
case typealias(_) => typestr(u)
case unitconst(v) =>
if (v.d == 1) v.n.toString
else s"${v.n.toString}/${v.d.toString}"
case flatmul(t) => termstr(t, render)
case AppliedType(op, List(lu, unitconst1()))
if (op =:= TypeRepr.of[/]) =>
showrender(lu, render)
case AppliedType(op, List(lu, ru)) if (op =:= TypeRepr.of[/]) =>
val (ls, rs) = (paren(lu, render), paren(ru, render))
s"${ls}/${rs}"
case AppliedType(op, List(b, e)) if (op =:= TypeRepr.of[^]) =>
val (bs, es) = (paren(b, render), powstr(e))
s"${bs}^${es}"
// treat any other type as if it is a BaseUnit,
// with its name and abbv defined as its type-string
case _ => typestr(u)
def termstr(using
Quotes
)(
terms: List[quotes.reflect.TypeRepr],
render: quotes.reflect.TypeRepr => String
): String =
import quotes.reflect.*
// values for terms constructed from 'flatmul' should have >= 2 terms to start
// so should terms should never be empty in this recursion
terms match
case term +: Nil => paren(term, render)
case term +: tail =>
val h = paren(term, render)
val t = termstr(tail, render)
// tail should never be empty (see above)
term match
case namedPU(_) => s"${h}${t}"
case _ => s"${h} ${t}"
case _ =>
report.error(s"unrecognized termstr pattern $terms")
""
object flatmul:
def unapply(using Quotes)(
u: quotes.reflect.TypeRepr
): Option[List[quotes.reflect.TypeRepr]] =
import quotes.reflect.*
u match
case AppliedType(op, List(lu, ru)) if (op =:= TypeRepr.of[*]) =>
val lflat = lu match
case flatmul(lf) => lf
case _ => List(lu)
val rflat = ru match
case flatmul(rf) => rf
case _ => List(ru)
Some(lflat ++ rflat)
case _ => None
def powstr(using Quotes)(p: quotes.reflect.TypeRepr): String =
import quotes.reflect.*
p match
case bigintTE(v) if (v >= 0) => v.toString
case bigintTE(v) if (v < 0) => s"(${v.toString})"
case rationalTE(v) => s"(${v.n.toString}/${v.d.toString})"
case _ => "!!!"
def paren(using
Quotes
)(
u: quotes.reflect.TypeRepr,
render: quotes.reflect.TypeRepr => String
): String =
val str = showrender(u, render)
if (atomic(u)) str else s"(${str})"
def atomic(using Quotes)(u: quotes.reflect.TypeRepr): Boolean =
import quotes.reflect.*
u match
case namedunit(_) => true
case unitconst(_) => true
case AppliedType(op, List(namedunit(_), _))
if (op =:= TypeRepr.of[^]) =>
true
case AppliedType(
op,
List(flatmul(namedPU(_) +: namedunit(_) +: Nil), _)
) if (op =:= TypeRepr.of[^]) =>
true
case flatmul(namedPU(_) +: namedunit(_) +: Nil) => true
case AppliedType(_, _) => false
case _ => true
object namedunit:
def unapply(using Quotes)(
u: quotes.reflect.TypeRepr
): Option[quotes.reflect.TypeRepr] =
import quotes.reflect.*
u match
case namedSU(nu) => Some(nu)
case namedBU(nu) => Some(nu)
case namedDU(nu) => Some(nu)
case _ => None
object namedSU:
def unapply(using Quotes)(
u: quotes.reflect.TypeRepr
): Option[quotes.reflect.TypeRepr] =
import quotes.reflect.*
Implicits.search(
TypeRepr
.of[ShowUnitAlias]
.appliedTo(List(u, TypeBounds.empty, TypeBounds.empty))
) match
case iss: ImplicitSearchSuccess =>
Some(
iss.tree.tpe.baseType(TypeRepr.of[NamedUnit].typeSymbol)
)
case _ => None
object namedPU:
def unapply(using Quotes)(
u: quotes.reflect.TypeRepr
): Option[quotes.reflect.TypeRepr] =
import quotes.reflect.*
Implicits.search(
TypeRepr
.of[DerivedUnit]
.appliedTo(
List(
u,
TypeRepr.of[1],
TypeBounds.empty,
TypeBounds.empty,
TypeBounds.empty
)
)
) match
case iss: ImplicitSearchSuccess =>
Some(
iss.tree.tpe.baseType(TypeRepr.of[NamedUnit].typeSymbol)
)
case _ => None
object namedBU:
def unapply(using Quotes)(
u: quotes.reflect.TypeRepr
): Option[quotes.reflect.TypeRepr] =
import quotes.reflect.*
Implicits.search(
TypeRepr
.of[BaseUnit]
.appliedTo(List(u, TypeBounds.empty, TypeBounds.empty))
) match
case iss: ImplicitSearchSuccess =>
Some(
iss.tree.tpe.baseType(TypeRepr.of[NamedUnit].typeSymbol)
)
case _ => None
object namedDU:
def unapply(using Quotes)(
u: quotes.reflect.TypeRepr
): Option[quotes.reflect.TypeRepr] =
import quotes.reflect.*
Implicits.search(
TypeRepr
.of[DerivedUnit]
.appliedTo(
List(
u,
TypeBounds.empty,
TypeBounds.empty,
TypeBounds.empty,
TypeBounds.empty
)
)
) match
case iss: ImplicitSearchSuccess =>
Some(
iss.tree.tpe.baseType(TypeRepr.of[NamedUnit].typeSymbol)
)
case _ => None