Skip to content

Commit

Permalink
astTypeRepr
Browse files Browse the repository at this point in the history
  • Loading branch information
erikerlandson committed Jan 31, 2023
1 parent 9cdb8c1 commit 834d62e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions parser/src/main/scala/coulomb/parser/parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import scala.quoted.*
import coulomb.*
import coulomb.syntax.*
import coulomb.units.si.*
import coulomb.units.us.*
import coulomb.conversion.*

import coulomb.rational.Rational
Expand Down Expand Up @@ -60,6 +61,24 @@ object test:

inline def m[T]: Map[String, String] = ${ meta.m[T] }

def s(astf: UnitAST, astt: UnitAST)(using staging.Compiler): Double =
staging.run {
import quotes.reflect.*

val tf = meta.astTypeRepr(astf)
val tt = meta.astTypeRepr(astt)

println(s"tf= ${tf.show}")
println(s"tt= ${tt.show}")

val r = (tf.asType, tt.asType) match
case ('[f], '[t]) =>
'{ coulomb.conversion.coefficients.coefficientDouble[f, t] }

println("hi")
r
}

// this compiles and "runs" but run-time fails trying to find
// coulomb implicits - so the staging compiler
// currently doesn't work for finding imported implicits
Expand Down Expand Up @@ -174,6 +193,22 @@ object meta:
}
f

def astTypeRepr(using Quotes)(
ast: UnitAST
): quotes.reflect.TypeRepr =
import quotes.reflect.*
ast match
case UnitAST.UnitType(path) => fqTypeRepr(path)
case UnitAST.Mul(l, r) =>
val ltr = astTypeRepr(l)
val rtr = astTypeRepr(r)
TypeRepr.of[coulomb.`*`].appliedTo(List(ltr, rtr))
case UnitAST.Div(n, d) =>
val ntr = astTypeRepr(n)
val dtr = astTypeRepr(d)
TypeRepr.of[coulomb.`/`].appliedTo(List(ntr, dtr))
case UnitAST.Pow(b, e) => astTypeRepr(b)

def symbolValueType(using Quotes)(
sym: quotes.reflect.Symbol
): quotes.reflect.TypeRepr =
Expand Down

0 comments on commit 834d62e

Please sign in to comment.