shapeless records for data def

This commit is contained in:
alari 2018-09-08 12:45:46 +02:00
parent c4411d879d
commit d0dbaa71f7
4 changed files with 46 additions and 32 deletions

View File

@ -0,0 +1,9 @@
package fluence.hackethberlin
sealed trait Expr[T <: types.Type] {
def toVyper(depth: Int): String
}
object Expr {
case class Ref[T]()
}

View File

@ -3,35 +3,45 @@ package fluence.hackethberlin
import shapeless._
import types._
import Decorator._
import syntax.singleton._
object MakeVyperApp extends App {
val struct = new StructType(
("address" address) ::
("owner" address) ::
("size" -> uint256) ::
("time" -> int128) :: HNil
('address ->> address) ::
('owner ->> address) ::
('size ->> uint256) ::
('time ->> int128) :: HNil
)
val data = new DataDef(
("address" address) ::
("owner" `public`(address)) ::
("holders" (address ~>> bool)) ::
("structMap" `public`(uint256 ~>> struct)) ::
("struct" `public`(struct)) ::
("struct2" struct) :: HNil
('address ->> address) ::
('owner ->> `public`(address)) ::
('holders ->> (address ~>> bool)) ::
('structMap ->> `public`(uint256 ~>> struct)) ::
('struct ->> `public`(struct)) ::
('struct2 ->> struct) :: HNil
)
val func = FuncDef(
"myFunc",
("address" address) :: HNil,
('address ->> address) :: HNil,
uint256
)
val strDataDef = ('address ->> address) :: HNil
val recordStruct = new StructType(
('record_address ->> address) :: ('other_some ->> uint256) :: HNil
)
println(recordStruct.toVyper)
println(data.toVyper)
println(func.toVyper)
println((`@public` @: func).toVyper)
}
}

View File

@ -1,6 +1,7 @@
package fluence.hackethberlin.types
import shapeless._
import shapeless.labelled.FieldType
import shapeless.tag._
sealed trait DataVyper[T] {
@ -22,12 +23,10 @@ sealed trait LowPriorityDataVyperImplicits {
dh.toVyperDefinitions(data.head) ::: dt.toVyperDefinitions(data.tail)
}
implicit def pairDataVyper[T <: Type]: DataVyper[(String, T)] =
new DataVyper[(String, T)] {
override def toVyperDefinitions(pair: (String, T)): List[String] = {
val (name, ttype) = pair
s"$name: ${ttype.toVyper}" :: Nil
}
implicit def recDataVyper[K <: Symbol, V <: Type](implicit wk: Witness.Aux[K]): DataVyper[FieldType[K, V]] =
new DataVyper[FieldType[K, V]]{
override def toVyperDefinitions(data: FieldType[K, V]): List[String] =
s"${wk.value.name}: ${data.toVyper}" :: Nil
}
}
@ -35,20 +34,16 @@ object DataVyper extends LowPriorityDataVyperImplicits {
def apply[T](implicit dataVyper: DataVyper[T]): DataVyper[T] = dataVyper
implicit def pairDataPublicVyper[T <: Type]: DataVyper[(String, T @@ Public)] =
new DataVyper[(String, T @@ Public)] {
override def toVyperDefinitions(pair: (String, T @@ Public)): List[String] = {
val (name, ttype) = pair
s"$name: public(${ttype.toVyper})" :: Nil
}
implicit def pairDataIndexedVyper[K <: Symbol, T <: Type](implicit wk: Witness.Aux[K]): DataVyper[FieldType[K, T @@ Indexed]] =
new DataVyper[FieldType[K, T @@ Indexed]] {
override def toVyperDefinitions(data: FieldType[K, T @@ Indexed]): List[String] =
s"${wk.value.name}: indexed(${data.toVyper})" :: Nil
}
implicit def pairDataIndexedVyper[T <: Type]: DataVyper[(String, T @@ Indexed)] =
new DataVyper[(String, T @@ Indexed)] {
override def toVyperDefinitions(pair: (String, T @@ Indexed)): List[String] = {
val (name, ttype) = pair
s"$name: indexed(${ttype.toVyper})" :: Nil
}
implicit def pairDataPublicVyper[K <: Symbol, T <: Type](implicit wk: Witness.Aux[K]): DataVyper[FieldType[K, T @@ Public]] =
new DataVyper[FieldType[K, T @@ Public]] {
override def toVyperDefinitions(data: FieldType[K, T @@ Public]): List[String] =
s"${wk.value.name}: public(${data.toVyper})" :: Nil
}
}

View File

@ -1,7 +1,7 @@
package fluence.hackethberlin.types
import shapeless.{HList, LUBConstraint}
import shapeless.HList
class StructType[D <: HList](dataDef: D)(implicit dv: DataVyper[D], c: LUBConstraint[D, (String, Type)]) extends Type {
class StructType[D <: HList](dataDef: D)(implicit dv: DataVyper[D]) extends Type {
override def toVyper: String = s"{${dv.toVyperDefinitions(dataDef).mkString(", ")}}"
}