Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unary: define unary operations on numbers
- Loading branch information
Showing
3 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/Unary.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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[(String, Numeric)] = { | ||
val op = token.text | ||
numericOpMap.get(op).map(op -> _) | ||
} | ||
} | ||
|
||
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters