Skip to content

Commit

Permalink
(and) and (or) now handle any number of parameters (#1251)
Browse files Browse the repository at this point in the history
* feat: generalized (and) and (or) to handle any number of parameters

* feat!: removed (and*) and (or*) macros

* chore: worked around compiler issue for unit test

* fix: unit test in ./test/macro.carp

Co-authored-by: guberatsie <[email protected]>
  • Loading branch information
guberathome and guberatsie authored Jun 20, 2021
1 parent 0c03836 commit 0d15a57
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 35 deletions.
41 changes: 28 additions & 13 deletions core/Macros.carp
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
;; Defining the meta data macros early so that they can be used by all the other code.

(defmacro and [x y]
(list 'if x y false))

(defmacro or [x y]
(list 'if x true y))

;; Defined early so that `doc` can accept a rest arg
(defndynamic map-internal [f xs acc]
(if (= 0 (length xs))
Expand Down Expand Up @@ -109,6 +102,34 @@
(defmacro private [name]
(eval (list 'meta-set! name "private" true)))

(hidden and-)
(defndynamic and- [xs]
; (defndynamic and- [xs] ; shorter but currently not entirely stable
; (if (= 0 (length xs))
; true
; (list 'if (car xs) (and- (cdr xs)) false) ))
(if (= 0 (length xs))
true
(if (= 1 (length xs))
(car xs)
(list 'if (car xs) (and- (cdr xs)) false) )))
(defmacro and [:rest xs]
(and- xs))

(hidden or-)
(defndynamic or- [xs]
; (if (= 0 (length xs)) ; shorter but currently not entirely stable
; false
; (list 'if (car xs) true (or- (cdr xs))) ))
(if (= 0 (length xs))
false
(if (= 1 (length xs))
(car xs)
(list 'if (car xs) true (or- (cdr xs))) )))
(defmacro or [:rest xs]
(or- xs))


(doc todo "sets the todo property for a binding.")
(defmacro todo [name value]
(eval (list 'meta-set! name "todo" value)))
Expand Down Expand Up @@ -210,12 +231,6 @@
(car forms)
(list func (car forms) (build-vararg func (cdr forms))))))

(defmacro and* [:rest forms]
(build-vararg 'and forms))

(defmacro or* [:rest forms]
(build-vararg 'or forms))

(defmacro ignore [form]
(list 'let (array '_ form) (list)))

Expand Down
2 changes: 1 addition & 1 deletion core/Quasiquote.carp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Example:
(macro-error "unquote-splicing takes exactly one argument.")
(reduce
(fn [acc elem]
(if (and* (list? elem)
(if (and (list? elem)
(= (length elem) 2)
(= (car elem) 'unquote-splicing))
(list 'append acc (cadr elem))
Expand Down
2 changes: 1 addition & 1 deletion examples/langtons_ant.carp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
1 (inc y)
3 (dec y)
y)]
(if (or* (< new-x 0)
(if (or (< new-x 0)
(< new-y 0)
(>= new-x @(State.width &state))
(>= new-y @(State.height &state)))
Expand Down
2 changes: 1 addition & 1 deletion test/array.carp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
five (Array.pop-back! &arr)
four (Array.pop-back! &arr)]
(assert-true test
(and* (= &exp &arr)
(and (= &exp &arr)
(= six 6)
(= five 5)
(= four 4))
Expand Down
30 changes: 15 additions & 15 deletions test/macros.carp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@

(defmacro test-map []
(let [mapped (Dynamic.map length '((a) (b c) (d e f)))]
(and* (= 1 (Dynamic.car mapped)) (= 2 (Dynamic.cadr mapped))
(and (= 1 (Dynamic.car mapped)) (= 2 (Dynamic.cadr mapped))
(= 3 (Dynamic.caddr mapped)))))

(defmacro test-zip []
(let [zipped (Dynamic.zip array '('a 'd) '('c 'o) '('e 'g))]
(and (= 'ace (Symbol.concat (eval (Dynamic.car zipped))))
(= 'dog (Symbol.concat (eval (Dynamic.cadr zipped)))))))
(and (= 'ace (Symbol.concat (eval (Dynamic.car zipped))))
(= 'dog (Symbol.concat (eval (Dynamic.cadr zipped)))))))

(defmodule TestDyn
(defndynamic x [] true))
Expand Down Expand Up @@ -217,18 +217,18 @@
(assert-false test
(test-= veit "veit")
"= macro works as expected across types")
(assert-false test
(and* true true false)
"and* macro works as expected I")
(assert-true test
(and* true true true)
"and* macro works as expected II")
(assert-false test
(or* false false false)
"or* macro works as expected I")
(assert-true test
(or* true false true)
"or* macro works as expected II")
(assert-false test
(and true true false)
"vararg-and macro works as expected I")
(assert-true test
(and true true true)
"vararg-and macro works as expected II")
(assert-false test
(or false false false)
"vararg-or macro works as expected I")
(assert-true test
(or true false true)
"vararg-or macro works as expected II")
(assert-equal test
"1 thing 2 things"
&(str* 1 " thing " 2 " things")
Expand Down
8 changes: 4 additions & 4 deletions test/short_circuiting.carp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@

(defn vararg-and []
(do (set! g 0)
(ignore (and* (inc-true) (inc-true) (inc-false) (inc-true) (inc-true))) ;; 3 expressions will evaluate.
(ignore (and (inc-true) (inc-true) (inc-false) (inc-true) (inc-true))) ;; 3 expressions will evaluate.
g))

(defn vararg-or []
(do (set! g 0)
(ignore (or* (inc-false) (inc-false) (inc-true) (inc-false) (inc-false))) ;; 3 expressions will evaluate.
(ignore (or (inc-false) (inc-false) (inc-true) (inc-false) (inc-false))) ;; 3 expressions will evaluate.
g))

(deftest test
Expand All @@ -61,9 +61,9 @@
(assert-equal test
3
(vararg-and)
"'and*' works")
"vararg-'and' works")
(assert-equal test
3
(vararg-or)
"'or*' works")
"vararg-'or' works")
)

0 comments on commit 0d15a57

Please sign in to comment.