Function definitions with Decorators

This commit is contained in:
alari 2018-09-08 11:42:13 +02:00
parent 291e8b70a2
commit 27740833d5
5 changed files with 44 additions and 1 deletions

View File

@ -13,7 +13,7 @@ startYear := Some(2018)
licenses += ("Apache-2.0", new URL("https://www.apache.org/licenses/LICENSE-2.0.txt"))
//headerLicense := Some(License.ALv2("2018", organizationName.value))
resolvers += Resolver.bintrayRepo("fluencelabs", "releases")
//scalafmtOnCompile := true,
scalafmtOnCompile := true
// see good explanation https://gist.github.com/djspiewak/7a81a395c461fd3a09a6941d4cd040f2
scalacOptions += "-Ypartial-unification"

View File

@ -0,0 +1,11 @@
package fluence.hackethberlin
sealed abstract class Decorator(name: String) {
def toVyper: String = s"@$name"
}
object Decorator {
case object `@public` extends Decorator("public")
case object `@payable` extends Decorator("payable")
case object `@constant` extends Decorator("constant")
}

View File

@ -0,0 +1,17 @@
package fluence.hackethberlin
import fluence.hackethberlin.types.DataVyper
import shapeless._
class FuncDef[Args <: HList: DataVyper](
name: String,
argsDef: Args,
decorators: Set[Decorator] = Set.empty
) {
def toVyper: String =
s"${decorators.map(_.toVyper).mkString("\n")}\ndef $name(${DataVyper[Args].mkString(argsDef, ", ")}):\n body;\n"
def @:(decorator: Decorator): FuncDef[Args] =
new FuncDef[Args](name, argsDef, decorators + decorator)
}

View File

@ -2,6 +2,7 @@ package fluence.hackethberlin
import shapeless._
import types._
import Decorator._
object MakeVyperApp extends App {
@ -21,6 +22,15 @@ object MakeVyperApp extends App {
("struct2" struct) :: HNil
)
val func = new FuncDef(
"myFunc",
("address" address) :: HNil
)
println(data.toVyper)
println(func.toVyper)
println((`@public` @: func).toVyper)
}

View File

@ -5,6 +5,9 @@ import shapeless.tag._
sealed trait DataVyper[T] {
def toVyperDefinitions(data: T): List[String]
def mkString(data: T, sep: String): String =
toVyperDefinitions(data).mkString(sep)
}
sealed trait LowPriorityDataVyperImplicits {
@ -30,6 +33,8 @@ sealed trait LowPriorityDataVyperImplicits {
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] = {