~subsetpark/ec

5f2065e7cc6bb4fd362d3861bfd2a5cdc32f128f — Zach Smith 8 months ago e85c985
Add if
3 files changed, 24 insertions(+), 6 deletions(-)

M src/adverbs.janet
M src/operations.janet
M src/parser.janet
M src/adverbs.janet => src/adverbs.janet +22 -5
@@ 8,6 8,14 @@
                    :type ,(keyword name)
                    :fun-ref (fn ,name [stack] ,;body))))

(defn- do-apply
  [stack quotation]
  (if (calc/Quotation*? quotation)
      (do (calc/check-arity stack quotation)
        (each elem (calc/data quotation)
          (calc/push stack elem)))
      (calc/push stack quotation)))

(defadv apply-quotation 1
  ```
  q -- x


@@ 15,11 23,7 @@
  `1 1 (+) apply` is equivalent to `1 1 +`.
  ```
  (let [quotation (calc/pop stack)]
    (if (calc/Quotation*? quotation)
      (do (calc/check-arity stack quotation)
        (each elem (calc/data quotation)
          (calc/push stack elem)))
      (calc/push stack quotation))))
    (do-apply stack quotation)))

(defadv distribute 0
  ```


@@ 149,3 153,16 @@
        shape (calc/get-shape v)
        new-elem (calc/wrap ;(map calc/make-element shape))]
    (calc/push stack new-elem)))

(defadv if 3
  ```
  qt qf p -- x
  If `p` = 0, applies the quotation `qf`.
     else applies `qt`.
 ```
  (let [p (calc/pop stack)
        qf (calc/pop stack)
        qt (calc/pop stack)]
    (case (p :value)
      0 (do-apply stack qf)
      (do-apply stack qt))))

M src/operations.janet => src/operations.janet +1 -1
@@ 17,7 17,7 @@
       `x y -- p
       Comparison predicate.
       Push 1 if x %s y;
       else 0.
              else 0.
       ` ,(string cmp))))

(defop add 2 1 +)

M src/parser.janet => src/parser.janet +1 -0
@@ 31,6 31,7 @@
                 "dup" adverbs/dup
                 "arity" adverbs/arity
                 "shape" adverbs/shape
                 "if" adverbs/if

                 "sqr" compose/square})