FuncDef with simple body

This commit is contained in:
alari 2018-09-08 14:40:32 +02:00
parent f759c64868
commit 1165c9a8fc
4 changed files with 34 additions and 21 deletions

View File

@ -2,11 +2,26 @@ package fluence.hackethberlin
sealed trait Expr[T <: types.Type] { sealed trait Expr[T <: types.Type] {
def toVyper(depth: Int): String def toVyper(depth: Int): String
protected def spaces(depth: Int): String = " " * depth
}
sealed trait InlineExpr[T <: types.Type] extends Expr[T]{
override def toVyper(depth: Int): String = spaces(depth) + toInlineVyper
def toInlineVyper: String
def toReturn: Expr.Return[T] = Expr.Return[T](this)
} }
object Expr { object Expr {
case class Ref[T <: types.Type](name: String) extends Expr[T] { case class Ref[T <: types.Type](name: String) extends InlineExpr[T] {
override def toInlineVyper: String = name
}
case class Return[T <: types.Type](ret: InlineExpr[T]) extends Expr[T] {
override def toVyper(depth: Int): String = override def toVyper(depth: Int): String =
(" " * depth) + name spaces(depth) + "return "+ret.toInlineVyper
} }
} }

View File

@ -1,21 +1,22 @@
package fluence.hackethberlin package fluence.hackethberlin
import fluence.hackethberlin.types.DataVyper import fluence.hackethberlin.types.{DataVyper, StructType}
import shapeless._ import shapeless._
class FuncDef[Args <: HList: DataVyper, Ret <: types.Type]( class FuncDef[Args <: HList: DataVyper, Ret <: types.Type](
name: String, name: String,
argsDef: Args, argsDef: Args,
ret: Option[Ret], ret: Option[Ret],
body: StructType[Args] Expr[Ret],
decorators: Set[Decorator] = Set.empty decorators: Set[Decorator] = Set.empty
) { ) {
def toVyper: String = def toVyper: String =
s"${decorators.map(_.toVyper).mkString("\n")}\ndef $name(${DataVyper[Args] s"${decorators.map(_.toVyper).mkString("\n")}\ndef $name(${DataVyper[Args]
.mkString(argsDef, ", ")})${ret.fold("")(" -> " + _.toVyper)}:\n body;\n" .mkString(argsDef, ", ")})${ret.fold("")(" -> " + _.toVyper)}:\n${body(new StructType[Args](argsDef)).toVyper(1)};\n"
def @:(decorator: Decorator): FuncDef[Args, Ret] = def @:(decorator: Decorator): FuncDef[Args, Ret] =
new FuncDef[Args, Ret](name, argsDef, ret, decorators + decorator) new FuncDef[Args, Ret](name, argsDef, ret, body, decorators + decorator)
} }
object FuncDef { object FuncDef {
@ -24,13 +25,13 @@ object FuncDef {
name: String, name: String,
argsDef: Args, argsDef: Args,
ret: Ret ret: Ret
): FuncDef[Args, Ret] = )(body: StructType[Args] Expr.Return[Ret]): FuncDef[Args, Ret] =
new FuncDef(name, argsDef, Some(ret)) new FuncDef(name, argsDef, Some(ret), body)
def apply[Args <: HList: DataVyper]( def apply[Args <: HList: DataVyper](
name: String, name: String,
argsDef: Args argsDef: Args
): FuncDef[Args, types.Void] = )(body: StructType[Args] Expr[types.Void]): FuncDef[Args, types.Void] =
new FuncDef(name, argsDef, None) new FuncDef(name, argsDef, None, body)
} }

View File

@ -2,9 +2,7 @@ package fluence.hackethberlin
import shapeless._ import shapeless._
import types._ import types._
import record._
import Decorator._ import Decorator._
import shapeless.labelled.FieldType
import syntax.singleton._ import syntax.singleton._
object MakeVyperApp extends App { object MakeVyperApp extends App {
@ -27,18 +25,15 @@ object MakeVyperApp extends App {
val func = FuncDef( val func = FuncDef(
"myFunc", "myFunc",
('address ->> address) :: HNil, ('addr ->> address) :: HNil,
uint256 address
) )(args args.ref('addr).toReturn)
val strDataDef = ('address ->> address) :: HNil
val recordStruct = new StructType( val recordStruct = new StructType(
('record_address ->> address) :: ('other_some ->> uint256) :: HNil ('record_address ->> address) :: ('other_some ->> uint256) :: HNil
) )
println(Console.RED + recordStruct.get('other_some) + Console.RESET) println(Console.RED + recordStruct.ref('other_some) + Console.RESET)
println(recordStruct.toVyper) println(recordStruct.toVyper)

View File

@ -5,8 +5,10 @@ import shapeless.{HList, Witness}
import shapeless.ops.record.Selector import shapeless.ops.record.Selector
class StructType[D <: HList](dataDef: D)(implicit dv: DataVyper[D]) extends Type { class StructType[D <: HList](dataDef: D)(implicit dv: DataVyper[D]) extends Type {
def get(k: Witness)(implicit selector : Selector[D, k.T], ev: k.T <:< Type): Expr.Ref[k.T] =
Expr.Ref[k.T]("field should be there")
override def toVyper: String = s"{${dv.toVyperDefinitions(dataDef).mkString(", ")}}" def ref[T <: Symbol, V <: Type](k: Witness.Aux[T])(implicit selector : Selector.Aux[D, T, V]): Expr.Ref[V] =
Expr.Ref[V](k.value.name)
override def toVyper: String =
s"{${dv.toVyperDefinitions(dataDef).mkString(", ")}}"
} }