From 5030b27e1f5e15058f517d1e08205521750276b5 Mon Sep 17 00:00:00 2001 From: z80 Date: Fri, 15 Dec 2023 03:02:16 -0500 Subject: [PATCH] implement thread macros --- dasy/builtin/macros.hy | 29 +++++++++++++++++++++++++++++ dasy/parser/parse.py | 2 ++ examples/simple_auction.dasy | 9 ++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/dasy/builtin/macros.hy b/dasy/builtin/macros.hy index 3c9ffc8..bc9b0e9 100644 --- a/dasy/builtin/macros.hy +++ b/dasy/builtin/macros.hy @@ -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)) diff --git a/dasy/parser/parse.py b/dasy/parser/parse.py index 5a0022e..46e621e 100644 --- a/dasy/parser/parse.py +++ b/dasy/parser/parse.py @@ -35,6 +35,8 @@ "*!": "unsafe_mul", "/!": "unsafe_div", "def": "annassign", + "->": "arrow", + "->>": "arroww", } SRC = "" diff --git a/examples/simple_auction.dasy b/examples/simple_auction.dasy index f7e838c..0cb658a 100644 --- a/examples/simple_auction.dasy +++ b/examples/simple_auction.dasy @@ -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 @@ -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))