Skip to content

Commit 96ac156

Browse files
committed
core: Support data mode sequence insertion for scalars
1 parent b2203d5 commit 96ac156

7 files changed

Lines changed: 63 additions & 12 deletions

File tree

core/src/yamlscript/constructor.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,13 @@
259259
(let [nodes (map #(construct-node %1 ctx) group)
260260
node (if (:dmap (first group))
261261
(if (> (count group) 1)
262-
(Lst (vec (cons (Sym 'concat) (vec nodes))))
262+
(Lst (vec (cons (Sym '+concat) (vec nodes))))
263263
(first nodes))
264264
(Vec (vec nodes)))
265265
new (if new (conj new node) [node])]
266266
new))
267267
nil nodes)]
268-
(Lst (vec (cons (Sym 'concat) vect))))))
268+
(Lst (vec (cons (Sym '+concat) vect))))))
269269

270270
(defn construct-vec [node ctx]
271271
(or

core/src/ys/std.clj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@
205205
([C] #(in? C %1))
206206
([x C] (has? C x)))
207207

208+
(defn +concat [& C]
209+
(apply concat (map #(cond
210+
(sequential? %1) %1
211+
(or
212+
(string? %1)
213+
(number? %1)
214+
(boolean? %1)
215+
(map? %1)
216+
(set? %1)) [%1]
217+
:else nil) C)))
218+
208219
(defn +merge [M]
209220
(if (:-<< M)
210221
(let [m (dissoc M :-<<)

core/src/ys/taptest.clj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[clojure.string :as str]
55
[ys.ys :as ys]
66
[ys.yaml :as yaml]
7+
[yamlscript.global :as global]
78
[yamlscript.common])
89
(:refer-clojure
910
:exclude [test]))
@@ -72,7 +73,11 @@
7273
code (get test "code")
7374
what (get test "what")
7475
_ (check-string code "code" count)
75-
code (str "!ys-0\n" code "\n")]
76+
mode (get test "mode")
77+
code (cond
78+
(= "data" mode) (str "!ys-0:\n" code "\n")
79+
(= "bare" mode) code
80+
:else (str "!ys-0\n" code "\n"))]
7681
(try (if (= "out" what)
7782
(with-out-str (ys/eval code))
7883
(ys/eval code))
@@ -128,6 +133,7 @@
128133
tests2)))
129134

130135
(defn- init-test [test]
136+
(swap! global/opts assoc :unordered true)
131137
(let [keys (set (keys test))
132138
what (get test "what")
133139
form (get test "form")]
@@ -151,8 +157,11 @@
151157

152158
(defn- normalize [got test]
153159
(let [what (get test "what")
154-
form (get test "form")]
160+
form (get test "form")
161+
mode (get test "mode")]
155162
(cond
163+
(= "data" mode) (pr-str got)
164+
(= "bare" mode) (pr-str got)
156165
form (if (fn? form)
157166
(form got test)
158167
(die "taptest: 'form' must be a function"))

core/test/compiler-stack.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@
13821382
- :when true: ddd
13831383
- eee
13841384
clojure: |
1385-
(concat ["aaa"] (when true bbb) ["ccc"] (when true ddd) ["eee"])
1385+
(+concat ["aaa"] (when true bbb) ["ccc"] (when true ddd) ["eee"])
13861386
13871387
13881388
- name: Multiple code insertions in data mode with mode swap variants
@@ -1407,9 +1407,9 @@
14071407
{:map [{:expr ":when true"} {:seq ({:str "bbb"})}]}
14081408
{:str "ccc"})}
14091409
clojure: |
1410-
(concat
1410+
(+concat
14111411
["aaa"]
1412-
(concat bbb ["bbb"] (when true bbb) (when true ["bbb"]))
1412+
(+concat bbb ["bbb"] (when true bbb) (when true ["bbb"]))
14131413
["ccc"])
14141414
14151415
@@ -1425,7 +1425,7 @@
14251425
- 9
14261426
- 10
14271427
clojure: |
1428-
(concat [0 1] [2 3 4] [5 5] [6 7 8] [9 10])
1428+
(+concat [0 1] [2 3 4] [5 5] [6 7 8] [9 10])
14291429
14301430
14311431
- name: Two '::' in a row in a data mode sequence
@@ -1436,15 +1436,15 @@
14361436
- :when y:: [6, 7, 8]
14371437
- 9
14381438
clojure: |
1439-
(concat [1] (concat (when x [2 3 4]) (when y [6 7 8])) [9])
1439+
(+concat [1] (+concat (when x [2 3 4]) (when y [6 7 8])) [9])
14401440
14411441
14421442
- name: Single '::' in a data mode sequence
14431443
yamlscript: |
14441444
!ys-0:
14451445
- :when x: +[2 3 4]
14461446
clojure: |
1447-
(concat (when x [2 3 4]))
1447+
(+concat (when x [2 3 4]))
14481448
14491449
14501450
- name: Two '::' in a row in a data mode mapping

core/test/compiler.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,7 @@
15651565
- bar
15661566
- baz
15671567
clojure: |
1568-
(concat ["foo"] (when (> (rand 2) 1) ["bar"]) ["baz"])
1568+
(+concat ["foo"] (when (> (rand 2) 1) ["bar"]) ["baz"])
15691569
15701570
15711571
- name: Fix bug in seq insertion in data mode
@@ -1637,3 +1637,14 @@
16371637
(.foo 123)
16381638
(.bar foo baz)
16391639
(.baz (.bar (.foo 123) baz))
1640+
1641+
1642+
- name: Scalar insertion in data mode sequence
1643+
yamlscript: |
1644+
!ys-0:
1645+
- foo
1646+
- :: 3 .. 5
1647+
- :: 42
1648+
- bar
1649+
clojure: |
1650+
(+concat ["foo"] (+concat (rng 3 5) 42) ["bar"])

ys/test/data-mode.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env ys-0
2+
3+
use ys::taptest: :all
4+
5+
hash =::
6+
FOO: 1
7+
FOO_BAR: 2
8+
9+
test::
10+
- name: Data mode sequence insertions using '::'
11+
code: |
12+
- foo
13+
- :: 3 .. 5
14+
- :: 42
15+
- :: +{'a' 1 'b' 2}
16+
- bar
17+
mode: data
18+
want: ("foo" 3 4 5 42 {"a" 1, "b" 2} "bar")
19+
20+
done:

ys/test/std.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ test::
212212
want:: inc
213213

214214
- code: qw(one :two "three" 4 true false nil {} [])
215-
want:: +['one' ':two' "three" '4' 'true' 'false' 'nil' '(%)' '[]']
215+
want:: +['one' ':two' "three" '4' 'true' 'false' 'nil' '{}' '[]']
216216

217217

218218
#-------------------------------------------------------------------------------

0 commit comments

Comments
 (0)