Skip to content

Commit bfe7c7a

Browse files
committed
Move scala.quoted.package to src-bootstrapped
1 parent 6f6751d commit bfe7c7a

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package scala
2+
3+
package object quoted {
4+
5+
/** Evaluate the contents of this expression and return the result.
6+
* It provides a new QuoteContext that is only valid within the scope the argument.
7+
*
8+
* Usage:
9+
* ```
10+
* val e: T = run { // (given qctx: QuoteContext) =>
11+
* expr
12+
* }
13+
* ```
14+
* where `expr: Expr[T]`
15+
*
16+
* This method should not be called in a context where there is already has a `QuoteContext`
17+
* such as within a `run` or a `withQuoteContext`.
18+
*/
19+
def run[T](expr: given QuoteContext => Expr[T]) given (toolbox: Toolbox): T = toolbox.run(expr given _)
20+
21+
/** Provide a new quote context within the scope of the argument that is only valid within the scope the argument.
22+
* Return the result of the argument.
23+
*
24+
* Usage:
25+
* ```
26+
* val e: T = withQuoteContext { // (given qctx: QuoteContext) =>
27+
* thunk
28+
* }
29+
* ```
30+
* where `thunk: T`
31+
*
32+
* This method should not be called in a context where there is already has a `QuoteContext`
33+
* such as within a `run` or a `withQuoteContext`.
34+
*/
35+
def withQuoteContext[T](thunk: given QuoteContext => T) given (toolbox: Toolbox): T = {
36+
var result: T = NoResult.asInstanceOf[T]
37+
def dummyRun given QuoteContext: Expr[Unit] = {
38+
result = thunk
39+
Expr.unitExpr
40+
}
41+
toolbox.run(dummyRun given _)
42+
assert(result != NoResult) // toolbox.run should have thrown an exception
43+
result
44+
}
45+
46+
private object NoResult
47+
48+
object autolift {
49+
given autoToExpr[T] as Conversion[T, Expr[T]] given Liftable[T], QuoteContext = _.toExpr
50+
}
51+
52+
implicit object ExprOps {
53+
def (x: T) toExpr[T: Liftable] given QuoteContext: Expr[T] = the[Liftable[T]].toExpr(x)
54+
55+
/** Lifts this sequence of expressions into an expression of a sequence
56+
*
57+
* Transforms a sequence of expression
58+
* `Seq(e1, e2, ...)` where `ei: Expr[T]`
59+
* to an expression equivalent to
60+
* `'{ Seq($e1, $e2, ...) }` typed as an `Expr[Seq[T]]`
61+
*
62+
* Usage:
63+
* ```scala
64+
* '{ List(${List(1, 2, 3).toExprOfSeq}: _*) } // equvalent to '{ List(1, 2, 3) }
65+
* ```
66+
*/
67+
def (seq: Seq[Expr[T]]) toExprOfSeq[T] given (tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
68+
import qctx.tasty._
69+
Repeated(seq.map(_.unseal).toList, tp.unseal).seal.asInstanceOf[Expr[Seq[T]]]
70+
}
71+
72+
/** Lifts this list of expressions into an expression of a list
73+
*
74+
* Transforms a list of expression
75+
* `List(e1, e2, ...)` where `ei: Expr[T]`
76+
* to an expression equivalent to
77+
* `'{ List($e1, $e2, ...) }` typed as an `Expr[List[T]]`
78+
*/
79+
def (list: List[Expr[T]]) toExprOfList[T] given Type[T], QuoteContext: Expr[List[T]] =
80+
throw new Exception("running on non bootstrapped library")
81+
82+
/** Lifts this sequence of expressions into an expression of a tuple
83+
*
84+
* Transforms a sequence of expression
85+
* `Seq(e1, e2, ...)` where `ei: Expr[_]`
86+
* to an expression equivalent to
87+
* `'{ ($e1, $e2, ...) }` typed as an `Expr[Tuple]`
88+
*/
89+
def (seq: Seq[Expr[_]]) toExprOfTuple given QuoteContext: Expr[Tuple] =
90+
throw new Exception("running on non bootstrapped library")
91+
92+
}
93+
94+
}

0 commit comments

Comments
 (0)