forked from scalameta/scalameta
/
Unary.scala
63 lines (49 loc) · 1.57 KB
/
Unary.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
package scala.meta.internal.trees
import scala.meta.tokens.Token
private[meta] sealed trait Unary {
def op: String
}
private[meta] object Unary {
private val numericOpMap = Seq[Numeric](Plus, Minus, Tilde).map(x => x.op -> x).toMap
val opMap = numericOpMap ++ Seq[Unary](Not).map(x => x.op -> x)
def unapply(token: Token.Ident): Option[(String, Unary)] = {
val op = token.text
opMap.get(op).map(op -> _)
}
sealed trait Numeric extends Unary {
def apply(value: BigInt): BigInt
// could return None if not applicable (such as `~`)
def apply(value: BigDecimal): Option[BigDecimal]
}
object Numeric {
def unapply(token: Token.Ident): Option[Numeric] =
numericOpMap.get(token.text)
}
sealed trait Logical extends Unary {
def apply(value: Boolean): Boolean
}
case object Noop extends Numeric {
val op = ""
def apply(value: BigInt): BigInt = value
def apply(value: BigDecimal): Option[BigDecimal] = Some(value)
}
case object Plus extends Numeric {
val op = "+"
def apply(value: BigInt): BigInt = value
def apply(value: BigDecimal): Option[BigDecimal] = Some(value)
}
case object Minus extends Numeric {
val op = "-"
def apply(value: BigInt): BigInt = -value
def apply(value: BigDecimal): Option[BigDecimal] = Some(-value)
}
case object Tilde extends Numeric {
val op = "~"
def apply(value: BigInt): BigInt = ~value
def apply(value: BigDecimal): Option[BigDecimal] = None
}
case object Not extends Logical {
val op = "!"
def apply(value: Boolean): Boolean = !value
}
}