Skip to content

Commit 6996ee7

Browse files
committed
feat(macros): add ++ and -- macros
1 parent 115fe68 commit 6996ee7

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎Macros.ark‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,27 @@
131131
(macro unpackPair (pair outx outy) {
132132
(let outx ($as-is (@ pair 0)))
133133
(let outy ($as-is (@ pair 1))) })
134+
135+
# @brief Increment a variable, by generating a `set`
136+
# @param value symbol to increment
137+
# =begin
138+
# (mut n 0)
139+
# (++ n)
140+
# (print n) # 1
141+
# (++ n)
142+
# (print n) # 2
143+
# =end
144+
# @author https://github.com/SuperFola
145+
(macro ++ (value) (set value (+ 1 value)))
146+
147+
# @brief Decrement a variable, by generating a `set`
148+
# @param value symbol to decrement
149+
# =begin
150+
# (mut n 0)
151+
# (-- n)
152+
# (print n) # -1
153+
# (-- n)
154+
# (print n) # -2
155+
# =end
156+
# @author https://github.com/SuperFola
157+
(macro -- (value) (set value (- value 1)))

‎tests/macros-tests.ark‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,14 @@
4949
(test:eq i 10)
5050

5151
(until true
52-
(test:expect false "this shouldn't trigger")) })})
52+
(test:expect false "this shouldn't trigger")) })
53+
54+
(test:case "++ and --" {
55+
(mut i 0)
56+
(test:eq (++ i) 1)
57+
(test:eq (++ i) 2)
58+
(test:eq (++ i) 3)
59+
(test:eq (-- i) 2)
60+
(test:eq (-- i) 1)
61+
(test:eq (-- i) 0)
62+
(test:eq (-- i) -1) }) })

0 commit comments

Comments
 (0)