This commit is contained in:
jilen 2024-11-05 17:17:59 +08:00
parent cf6aa999ae
commit 1878d59821
23 changed files with 78 additions and 62 deletions

View file

@ -1,4 +1,4 @@
version = "3.8.1"
version = "3.8.3"
style = defaultWithAlign
runner.dialect=scala3
maxColumn = 80

View file

@ -1,9 +1,8 @@
name := "minisql"
scalaVersion := "3.5.0-RC4"
scalaVersion := "3.5.2"
libraryDependencies ++= Seq(
)
scalacOptions ++= Seq("-experimental")
scalacOptions ++= Seq("-experimental", "-language:experimental.namedTuples")

View file

@ -1 +1 @@
sbt.version=1.10.0
sbt.version=1.10.2

View file

@ -1 +0,0 @@
jilen@jilendeiMac.2465

View file

@ -1,28 +0,0 @@
package minisql
import scala.collection.immutable.{Map => IMap}
import scala.quoted.*
import minisql.ast.{*, given}
import scala.deriving.*
import scala.compiletime.*
sealed trait Query[T] {
def ast: Ast
}
case class Column
case class EntityQuery[A](ast: Entity) extends Query
inline def compile(inline e: Ast): Unit = ${ compileImpl('e) }
private def compileImpl(e: Expr[Ast])(using Quotes) = {
import quotes.reflect.*
e.value match {
case Some(v) =>
report.info("Static:" + v.toString())
case None =>
report.info("Dynamic")
}
'{ () }
}

View file

@ -68,6 +68,9 @@ private given FromExpr[Property] with {
)
} =>
Some(Property(a, n, r, v))
case o =>
println(s"Canot extrat ${o.show}:${x}")
None
}
}
@ -103,6 +106,9 @@ private given FromExpr[Query] with {
Some(Take(b, n))
case '{ SortBy(${ Expr(b) }, ${ Expr(p) }, ${ Expr(s) }, ${ Expr(o) }) } =>
Some(SortBy(b, p, s, o))
case o =>
println(s"Cannot extract ${o.show}")
None
}
}
@ -220,17 +226,37 @@ private given FromExpr[If] with {
}
}
private[minisql] given astFromExpr: FromExpr[Ast] = new FromExpr[Ast] {
private def isAstType[t](using Quotes, Type[t]) = {
import quotes.reflect.*
val t1 = TypeRepr.of[t].dealias.simplified
println(s"isAstType ${TypeRepr.of[t]} ===> ${t1}")
t1 <:< TypeRepr.of[Ast]
private def extractTerm(using Quotes)(x: quotes.reflect.Term) = {
import quotes.reflect.*
def unwrapTerm(t: Term): Term = t match {
case Inlined(_, _, o) => unwrapTerm(o)
case Block(Nil, last) => last
case Typed(t, _) =>
unwrapTerm(t)
case Select(t, "$asInstanceOf$") =>
unwrapTerm(t)
case TypeApply(t, _) =>
unwrapTerm(t)
case o => o
}
val o = unwrapTerm(x)
println(s"From ========== ${x.show}")
println(s"To ========== ${o.show}")
o
}
extension (e: Expr[Any]) {
def toTerm(using Quotes) = {
import quotes.reflect.*
e.asTerm
}
}
given astFromExpr: FromExpr[Ast] = new FromExpr[Ast] {
def unapply(e: Expr[Ast])(using Quotes): Option[Ast] = {
e match {
extractTerm(e.toTerm).asExpr match {
case '{ $x: Query } => x.value
case '{ $x: ScalarValueLift } => x.value
case '{ $x: Property } => x.value
@ -242,26 +268,7 @@ private[minisql] given astFromExpr: FromExpr[Ast] = new FromExpr[Ast] {
case '{ $x: Action } => x.value
case '{ $x: If } => x.value
case '{ $x: Infix } => x.value
case '{ ($x: Ast).asInstanceOf } =>
unapply(x.asInstanceOf)
case '{ (${ x }: t1).asInstanceOf[t2] } if isAstType[t2] =>
unapply(x.asInstanceOf)
case o =>
import quotes.reflect.*
def unwrapInline(x: Term): Unit = x match {
case Inlined(_, bs, t) =>
unwrapInline(t)
case Typed(t, _) =>
unwrapInline(t)
case TypeApply(t, _) =>
unwrapInline(t)
// case Select(x, "$asInstanceOf$") =>
// unwrapInline(x)
case o =>
println(s"unwrapped term(${o.getClass}): ${o.show}")
}
unwrapInline(o.asTerm)
None
case o => None
}
}
}

View file

@ -0,0 +1,39 @@
package minisql.dsl
import scala.quoted.*
import scala.compiletime.*
import scala.compiletime.ops.string.*
trait Quoted[E] {
type Ast
def ast: Ast
def liftings: Map[String, Any]
}
object Quoted {
def apply[_Ast <: String, E](
_ast: Ast,
_liftings: Map[String, Any]
): Quoted[_Ast, E] =
new Quoted[_Ast] {
type Ast = _Ast
def ast = _ast
def liftings = _liftings
}
inline def transform[A1 <: String, A2 <: String, A3 <: String](
inline q1: Quoted[A1],
inline q2: Quoted[A2]
)(inline f: (A1, A2) => A3): Quoted[A3] =
Quoted[A3](q1.liftings ++ q2.liftings)
}
case class Foo()
opaque type Query[E] <: Quoted[E] = Quoted[E]
opaque type EntityQuery[E] <: Quoted[E] & Query[E] = Quoted[E]
extension [E](e: EntityQuery[E]) {
inline def map[E1](inline f: E => E1): EntityQuery[E1] = Quoted[]()
}