Skip to content

Commit

Permalink
implement thread macros
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Dec 15, 2023
1 parent c5d1372 commit 5030b27
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
29 changes: 29 additions & 0 deletions dasy/builtin/macros.hy
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,32 @@
(.append forms (.read dasy stream))
(except [EOFError] (break))))
`(splice ~@forms)))

;; ->
(defmacro arrow [args #*body]
;; TODO: Get rid of this dynamic import
(import hy.models [Expression])
(let [first-exp (get body 0)
rest (cut body 1 None)
body (if (isinstance first-exp Expression)
`(~(get first-exp 0) ~args ~@(cut first-exp 1 None))
`(~first-exp ~args))]
(for [exp rest]
(setv body (if (isinstance exp Expression)
`(~(get exp 0) ~body ~@(cut exp 1 None))
`(~exp ~body))))
body))

;; ->>
(defmacro arroww [args #*body]
(import hy.models [Expression])
(let [first-exp (get body 0)
rest (cut body 1 None)
body (if (isinstance first-exp Expression)
`(~(get first-exp 0) ~@(cut first-exp 1 None) ~args)
`(~first-exp ~args))]
(for [exp rest]
(setv body (if (isinstance exp Expression)
`(~(get exp 0) ~@(cut exp 1 None) ~body)
`(~exp ~body))))
body))
2 changes: 2 additions & 0 deletions dasy/parser/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"*!": "unsafe_mul",
"/!": "unsafe_div",
"def": "annassign",
"->": "arrow",
"->>": "arroww",
}

SRC = ""
Expand Down
9 changes: 6 additions & 3 deletions examples/simple_auction.dasy
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
;; auction start time can be in the past, present, or future
(set self/auctionStart auction_start)
;; auction end time should be in the future
(set self/auctionEnd (+ self/auctionStart bidding_time)))
(->> bidding_time
(+ self.auctionStart)
(set self/auctionEnd)))

;; Bid on the auction with the value sent with the transaction
;; the value will only be refunded if the auction is not won
Expand All @@ -37,8 +39,9 @@
;; Check if bid is high enough
(assert (> msg/value self/highestBid))
;; Track the refund for the previous highest bidder
(+= (subscript self/pendingReturns self/highestBidder) self/highestBid)
;; (set (subscript self/pendingReturns self/highestBidder) (+ 1 (subscript self/pendingReturns self/highestBidder)))
(-> self/pendingReturns
(subscript self/highestBidder)
(+= self/highestBid))
;; Track new high bid
(set self/highestBidder msg/sender)
(set self/highestBid msg/value))
Expand Down

0 comments on commit 5030b27

Please sign in to comment.