This commit is contained in:
dmitry.shakhtarin 2018-09-09 10:15:49 +03:00
parent 0e6b69b995
commit 631f6260d6
4 changed files with 42 additions and 19 deletions

View File

@ -12,6 +12,8 @@ sealed trait Expr[T] {
sealed trait InlineExpr[T] extends Expr[T] with Expr.ToInlineVyper { sealed trait InlineExpr[T] extends Expr[T] with Expr.ToInlineVyper {
def toReturn: Free[Expr, T] = Free liftF Expr.Return[T](this) def toReturn: Free[Expr, T] = Free liftF Expr.Return[T](this)
def liftF: Free[Expr, T] = Free liftF this
def :=:(name: Symbol): Free[Expr, Expr.Ref[T]] = def :=:(name: Symbol): Free[Expr, Expr.Ref[T]] =
Free.liftF[Expr, Expr.Ref[T]](Expr.Assign[T](Expr.Ref[T](name.name, boxedValue), this)) Free.liftF[Expr, Expr.Ref[T]](Expr.Assign[T](Expr.Ref[T](name.name, boxedValue), this))
@ -55,8 +57,8 @@ object Expr {
body: () Free[Expr, Void] body: () Free[Expr, Void]
) extends InlineExpr[T] { ) extends InlineExpr[T] {
def bodyVyper: String = def bodyVyper: String =
body().foldMap(CodeChunk.fromExpr).run._1.toVyper(1) body().foldMap(CodeChunk.fromExpr).run._1.toVyper(2)
override def toVyper: String = s"$op " + right.toVyper + s":\n$bodyVyper\n" override def toVyper: String = s"$op " + right.toVyper + s":\n$bodyVyper"
override def toInlineVyper: String = toVyper override def toInlineVyper: String = toVyper
} }
@ -83,13 +85,13 @@ object Expr {
def `:===:`[A <: Type, B <: Type](a: InlineExpr[A], b: InlineExpr[B]): InlineExpr[bool.type] = def `:===:`[A <: Type, B <: Type](a: InlineExpr[A], b: InlineExpr[B]): InlineExpr[bool.type] =
Infix("==", a, b, bool) Infix("==", a, b, bool)
def `+:+`(a: InlineExpr[timestamp.type], b: InlineExpr[timedelta.type]): InlineExpr[timestamp.type] = def `+:+`[A <: timestamp.type, B <: timedelta.type](a: InlineExpr[A], b: InlineExpr[B]): InlineExpr[timestamp.type] =
Infix("+", a, b, timestamp) Infix("+", a, b, timestamp)
def `if`(expr: InlineExpr[bool.type], body: () Free[Expr, Void]): InlineExpr[Void] = def `if`(expr: InlineExpr[bool.type], body: () Free[Expr, Void]): InlineExpr[Void] =
Right("if", expr, Void) RightBody("if", expr, Void, body)
def `not`(expr: InlineExpr[bool.type]): InlineExpr[bool.type] = def `not`[A <: bool.type](expr: InlineExpr[A]): InlineExpr[bool.type] =
Right("not", expr, bool) Right("not", expr, bool)
def `assertt`(expr: InlineExpr[bool.type]): InlineExpr[bool.type] = def `assertt`(expr: InlineExpr[bool.type]): InlineExpr[bool.type] =
@ -98,6 +100,9 @@ object Expr {
def `<<`[A <: Type, B <: Type](a: InlineExpr[A], b: InlineExpr[B]): InlineExpr[bool.type] = def `<<`[A <: Type, B <: Type](a: InlineExpr[A], b: InlineExpr[B]): InlineExpr[bool.type] =
Infix("<", a, b, bool) Infix("<", a, b, bool)
def `>=`[A <: Type, B <: Type](a: InlineExpr[A], b: InlineExpr[B]): InlineExpr[bool.type] =
Infix(">=", a, b, bool)
def `>>`[A <: Type, B <: Type](a: InlineExpr[A], b: InlineExpr[B]): InlineExpr[bool.type] = def `>>`[A <: Type, B <: Type](a: InlineExpr[A], b: InlineExpr[B]): InlineExpr[bool.type] =
Infix(">", a, b, bool) Infix(">", a, b, bool)
} }

View File

@ -52,7 +52,7 @@ object FuncDef {
import syntax.singleton._ import syntax.singleton._
val send = { val send = {
ProductType(('_addr ->> address) :: ('_money ->> wei_value) :: HNil).funcDef( ProductType(('_addr ->> `public`(address)) :: ('_money ->> `public`(wei_value)) :: HNil).funcDef(
"send", "send",
Void Void
) { args ) { args

View File

@ -36,4 +36,6 @@ object ProductType {
def apply[D <: HList](dataDef: D)(implicit dv: DataVyper[D]): ProductType[D] = def apply[D <: HList](dataDef: D)(implicit dv: DataVyper[D]): ProductType[D] =
new ProductType[D](dataDef, dv) new ProductType[D](dataDef, dv)
def hNil: ProductType[HNil] = ProductType(HNil)
} }

View File

@ -5,7 +5,6 @@ import hackethberlin.types._
import shapeless._ import shapeless._
import Decorator._ import Decorator._
import cats.free.Free import cats.free.Free
import fluence.hackethberlin.Expr.Defs
import syntax.singleton._ import syntax.singleton._
object MakeVyperApp extends App { object MakeVyperApp extends App {
@ -52,6 +51,7 @@ object MakeVyperApp extends App {
sumArgs.funcDef("sum", uint256) { args sumArgs.funcDef("sum", uint256) { args
for { for {
c 'c :=: `++`(args.ref('a), args.ref('b)) c 'c :=: `++`(args.ref('a), args.ref('b))
_ Free.pure(`++`(args.ref('a), args.ref('b)))
d 'd :=: `++`(args.ref('b), c) d 'd :=: `++`(args.ref('b), c)
_ d :=: c _ d :=: c
sum `++`(args.ref('a), d).toReturn sum `++`(args.ref('a), d).toReturn
@ -71,19 +71,21 @@ object MakeVyperApp extends App {
// println(s"MMMMMACRO\n\n ${new MyContract("abc", 123).toAST.toVyper}") // println(s"MMMMMACRO\n\n ${new MyContract("abc", 123).toAST.toVyper}")
} }
object Auction { object Auction extends App {
import Expr.Defs._ import Expr.Defs._
val predef = ProductType( val predef = ProductType(
(Symbol("block.timestamp") ->> timestamp) :: (Symbol("block.timestamp") ->> timestamp) ::
(Symbol("msg.value") ->> uint256) :: (Symbol("msg.value") ->> wei_value) ::
(Symbol("msg.sender") ->> address) :: (Symbol("msg.sender") ->> address) ::
(Symbol("True") ->> bool) ::
HNil HNil
) )
val `block.timestamp` = predef.ref(Symbol("block.timestamp")) val `block.timestamp` = predef.ref(Symbol("block.timestamp"))
val `msg.value` = predef.ref(Symbol("msg.value")) val `msg.value` = predef.ref(Symbol("msg.value"))
val `msg.sender` = predef.ref(Symbol("msg.sender")) val `msg.sender` = predef.ref(Symbol("msg.sender"))
val `True` = predef.ref('True)
val data = ProductType( val data = ProductType(
('beneficiary ->> public(address)) :: ('beneficiary ->> public(address)) ::
@ -99,14 +101,13 @@ object Auction {
val auction_end = data.ref('auction_end) val auction_end = data.ref('auction_end)
val highest_bid = data.ref('highest_bid) val highest_bid = data.ref('highest_bid)
val highest_bidder = data.ref('highest_bidder) val highest_bidder = data.ref('highest_bidder)
val ended = data.ref('ended)
val initArgs = ProductType(('_beneficiary ->> address) :: ('_bidding_time ->> timedelta) :: HNil) val initArgs = ProductType(('_beneficiary ->> address) :: ('_bidding_time ->> timedelta) :: HNil)
val _beneficiary = initArgs.ref('_beneficiary) val _beneficiary = initArgs.ref('_beneficiary)
val _bidding_time = initArgs.ref('_bidding_time) val _bidding_time = initArgs.ref('_bidding_time)
println(_beneficiary.getClass)
val init = `@public` @: initArgs.funcDef( val init = `@public` @: initArgs.funcDef(
"__init__", "__init__",
Void Void
@ -115,27 +116,42 @@ object Auction {
_ <- Free.pure(Void) _ <- Free.pure(Void)
_ <- beneficiary :=: _beneficiary _ <- beneficiary :=: _beneficiary
_ <- auction_start :=: `block.timestamp` _ <- auction_start :=: `block.timestamp`
// _ <- auction_end :=: `+:+`(auction_start, _bidding_time) _ <- auction_end :=: `+:+`(auction_start, _bidding_time)
} yield Void } yield Void
} }
val bidIf: () Free[Expr, Void] = { () => val bidIf: () Free[Expr, Void] = { () =>
for { for {
_ <- Free.pure(Void) _ <- FuncDef.send(highest_bidder :: highest_bid :: HNil).liftF
// _ <- FuncDef.send(highest_bidder :: highest_bid :: HNil)
} yield Void } yield Void
} }
/*val bid = `@public` @: `@payable` @: ProductType(HNil: HList).funcDef( val bid = `@public` @: `@payable` @: ProductType.hNil.funcDef(
"bid", "bid",
Void Void
) { args ) { args
for { for {
_ <- `assertt`(`block.timestamp` `<<` auction_end) _ <- `assertt`(`<<`(`block.timestamp`, auction_end)).liftF
_ <- `assertt`(`msg.value` `>>` highest_bid) _ <- `assertt`(`>>`(`msg.value`, highest_bid)).liftF
_ <- `if`(`not`(highest_bid `:===:` `msg.value`), bidIf) _ <- `if`(`not`(`:===:`(highest_bid, `msg.value`)), bidIf).liftF
_ <- highest_bidder :=: `msg.sender` _ <- highest_bidder :=: `msg.sender`
_ <- highest_bid :=: `msg.value` _ <- highest_bid :=: `msg.value`
} yield Void } yield Void
}*/ }
val end_auction = `@public` @: ProductType.hNil.funcDef(
"end_auction",
Void
) { args
for {
_ <- `assertt`(`>=`(`block.timestamp`, auction_end)).liftF
_ <- `assertt`(`not`(ended)).liftF
_ <- ended :=: `True`
_ <- FuncDef.send(beneficiary :: highest_bid :: HNil).liftF
} yield Void
}
val contract = new Contract(data :: init :: bid :: end_auction :: HNil)
println(contract.toVyper)
} }