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

View File

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

View File

@ -1,7 +1,7 @@
package fluence.hackethberlin.types 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(", ")}}" override def toVyper: String = s"{${dv.toVyperDefinitions(dataDef).mkString(", ")}}"
} }