+ Mapping types

+ public/indexed as tags
- Lost the check for public/indexed
This commit is contained in:
alari 2018-09-08 00:12:56 +02:00
parent 146e3433e3
commit 6880db4d11
10 changed files with 120 additions and 32 deletions

60
.scalafmt.conf Normal file
View File

@ -0,0 +1,60 @@
docstrings = JavaDoc
maxColumn = 120
align = none
align {
openParenCallSite = false
openParenDefnSite = false
tokens = [
"%", "%%", "%%%", ":=", "~="
]
}
assumeStandardLibraryStripMargin = true
includeCurlyBraceInSelectChains = false
continuationIndent {
callSite = 2
defnSite = 2
extendSite = 4
}
danglingParentheses = true
newlines {
alwaysBeforeTopLevelStatements = true
sometimesBeforeColonInMethodReturnType = true
penalizeSingleSelectMultiArgList = false
alwaysBeforeElseAfterCurlyIf = false
neverInResultType = false
}
spaces {
afterKeywordBeforeParen = true
}
binPack {
parentConstructors = true
literalArgumentLists = true
}
optIn {
breaksInsideChains = false
breakChainOnFirstMethodDot = true
configStyleArguments = true
}
runner {
optimizer {
forceConfigStyleOnOffset = 150
forceConfigStyleMinArgCount = 2
}
}
rewrite {
rules = [
SortImports
]
}

1
project/plugins.sbt Normal file
View File

@ -0,0 +1 @@
addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "1.4.0")

View File

@ -5,19 +5,21 @@ import types._
object MakeVyperApp extends App {
val data = new DataDef(
("address" address) ::
("owner" `public`(address)) :: HNil
)
val struct = new StructType(
"someStruct",
("address" address) ::
("owner" address) ::
("size" -> uint256) ::
("time" -> int128) :: HNil
)
val data = new DataDef(
("address" address) ::
("owner" `public`(address)) ::
("holders" (address ~>> bool)) ::
("struct" `public`(struct)) :: HNil
)
println(data.toVyper)
println(struct.toVyper)

View File

@ -1,12 +1,13 @@
package fluence.hackethberlin.types
import shapeless.{::, HList, HNil}
import shapeless._
import shapeless.tag._
sealed trait DataVyper[T] {
def toVyperDefinitions(data: T): List[String]
}
object DataVyper {
sealed trait LowPriorityDataVyperImplicits {
implicit object hnilDataVyper extends DataVyper[HNil] {
override def toVyperDefinitions(data: HNil): List[String] = Nil
@ -26,3 +27,23 @@ object DataVyper {
}
}
}
object DataVyper extends LowPriorityDataVyperImplicits {
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[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
}
}
}

View File

@ -0,0 +1,5 @@
package fluence.hackethberlin.types
case class Mapping[K <: PrimitiveType, V <: Type](ktype: K, vtype: V) extends Type {
override def toVyper: String = s"${vtype.toVyper}[${ktype.toVyper}]"
}

View File

@ -1,11 +0,0 @@
package fluence.hackethberlin.types
case class ModifiedType[T <: Type](modifier: String, ttype: T) extends Type {
override def toVyper: String = s"$modifier(${ttype.toVyper})"
}
object ModifiedType {
trait Defs {
def `public`[T <: Type](ttype: T): ModifiedType[T] = ModifiedType("public", ttype)
}
}

View File

@ -1,15 +1,19 @@
package fluence.hackethberlin.types
abstract sealed class PrimitiveType(name: String) extends PlainType {
abstract sealed class PrimitiveType(name: String) extends Type {
self
override def toVyper: String = name
def ~>>[V <: Type](vtype: V): Mapping[self.type, V] = Mapping(self, vtype)
}
object PrimitiveType {
trait Defs {
object address extends PrimitiveType("address")
object bool extends PrimitiveType("bool")
object int128 extends PrimitiveType("int128")
object uint256 extends PrimitiveType("uint256")
object decimal extends PrimitiveType("decimal")
case object address extends PrimitiveType("address")
case object bool extends PrimitiveType("bool")
case object int128 extends PrimitiveType("int128")
case object uint256 extends PrimitiveType("uint256")
case object decimal extends PrimitiveType("decimal")
}
}

View File

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

View File

@ -2,6 +2,4 @@ package fluence.hackethberlin.types
trait Type {
def toVyper: String
}
trait PlainType extends Type
}

View File

@ -1,3 +1,11 @@
package fluence.hackethberlin
package object types extends PrimitiveType.Defs with ModifiedType.Defs {}
import shapeless.tag
package object types extends PrimitiveType.Defs {
sealed trait Public
sealed trait Indexed
val `public`: tag.Tagger[Public] = tag[Public]
val indexed: tag.Tagger[Indexed] = tag[Indexed]
}