mirror of
https://github.com/fluencelabs/hackethberlin
synced 2025-04-24 17:02:18 +00:00
FuncDef with simple body
This commit is contained in:
parent
f759c64868
commit
1165c9a8fc
@ -2,11 +2,26 @@ package fluence.hackethberlin
|
||||
|
||||
sealed trait Expr[T <: types.Type] {
|
||||
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 {
|
||||
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 =
|
||||
(" " * depth) + name
|
||||
spaces(depth) + "return "+ret.toInlineVyper
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,22 @@
|
||||
package fluence.hackethberlin
|
||||
|
||||
import fluence.hackethberlin.types.DataVyper
|
||||
import fluence.hackethberlin.types.{DataVyper, StructType}
|
||||
import shapeless._
|
||||
|
||||
class FuncDef[Args <: HList: DataVyper, Ret <: types.Type](
|
||||
name: String,
|
||||
argsDef: Args,
|
||||
ret: Option[Ret],
|
||||
body: StructType[Args] ⇒ Expr[Ret],
|
||||
decorators: Set[Decorator] = Set.empty
|
||||
) {
|
||||
|
||||
def toVyper: String =
|
||||
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] =
|
||||
new FuncDef[Args, Ret](name, argsDef, ret, decorators + decorator)
|
||||
new FuncDef[Args, Ret](name, argsDef, ret, body, decorators + decorator)
|
||||
}
|
||||
|
||||
object FuncDef {
|
||||
@ -24,13 +25,13 @@ object FuncDef {
|
||||
name: String,
|
||||
argsDef: Args,
|
||||
ret: Ret
|
||||
): FuncDef[Args, Ret] =
|
||||
new FuncDef(name, argsDef, Some(ret))
|
||||
)(body: StructType[Args] ⇒ Expr.Return[Ret]): FuncDef[Args, Ret] =
|
||||
new FuncDef(name, argsDef, Some(ret), body)
|
||||
|
||||
def apply[Args <: HList: DataVyper](
|
||||
name: String,
|
||||
argsDef: Args
|
||||
): FuncDef[Args, types.Void] =
|
||||
new FuncDef(name, argsDef, None)
|
||||
)(body: StructType[Args] ⇒ Expr[types.Void]): FuncDef[Args, types.Void] =
|
||||
new FuncDef(name, argsDef, None, body)
|
||||
|
||||
}
|
||||
|
@ -2,9 +2,7 @@ package fluence.hackethberlin
|
||||
|
||||
import shapeless._
|
||||
import types._
|
||||
import record._
|
||||
import Decorator._
|
||||
import shapeless.labelled.FieldType
|
||||
import syntax.singleton._
|
||||
|
||||
object MakeVyperApp extends App {
|
||||
@ -27,18 +25,15 @@ object MakeVyperApp extends App {
|
||||
|
||||
val func = FuncDef(
|
||||
"myFunc",
|
||||
('address ->> address) :: HNil,
|
||||
uint256
|
||||
)
|
||||
|
||||
|
||||
val strDataDef = ('address ->> address) :: HNil
|
||||
('addr ->> address) :: HNil,
|
||||
address
|
||||
)(args ⇒ args.ref('addr).toReturn)
|
||||
|
||||
val recordStruct = new StructType(
|
||||
('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)
|
||||
|
||||
|
@ -5,8 +5,10 @@ import shapeless.{HList, Witness}
|
||||
import shapeless.ops.record.Selector
|
||||
|
||||
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(", ")}}"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user