Skip to content

Commit cd41783

Browse files
author
Andy C
committed
[stdlib/ysh/stream] Got some things working, grep-like test cases
It seems like it's working! Testing out io->eval(pos_args=, vars=) I think io->evalToDict() might be different, and won't take those arguments. Both of them take a value.Command, not a CommandFrag.
1 parent 641fcc2 commit cd41783

File tree

2 files changed

+103
-12
lines changed

2 files changed

+103
-12
lines changed

spec/ysh-proc.test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ sum = 53
794794

795795
#### Stateful proc with counter
796796
shopt --set ysh:upgrade
797+
797798
proc invokeCounter(; self, inc) {
798799
setvar self.i += inc
799800
echo "counter = $[self.i]"

stdlib/ysh/stream.ysh

Lines changed: 102 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,26 @@ source $LIB_OSH/byo-server.sh
1111
source $LIB_YSH/args.ysh
1212

1313
proc slurp-by (; num_lines) {
14-
# TODO: (stdin)
15-
for line in (stdin) {
16-
echo TODO
14+
var buf = []
15+
for line in (io.stdin) {
16+
call buf->append(line)
17+
if (len(buf) === num_lines) {
18+
json write (buf, space=0)
19+
20+
# TODO:
21+
#call buf->clear()
22+
setvar buf = []
23+
}
24+
}
25+
if (buf) {
26+
json write (buf, space=0)
1727
}
1828
}
1929

30+
proc test-slurp-by {
31+
seq 8 | slurp-by (3)
32+
}
33+
2034
# Note:
2135
# - these are all the same algorithm
2236
# - also word, block, etc. are all optional
@@ -46,33 +60,109 @@ proc test-each-line {
4660
# ysh-tool test stream.ysh
4761
#
4862
# Col
49-
50-
5163
}
5264

5365
proc each-row (; ; block) {
5466
echo TODO
5567
}
5668

57-
proc split-by (; ifs=null; block) {
58-
echo TODO
69+
proc split-by (; delim; ifs=null; block) {
70+
71+
# TODO: provide the option to bind names? Or is that a separate thing?
72+
# The output of this is "ragged"
73+
74+
for line in (io.stdin) {
75+
#pp (line)
76+
var parts = line.split(delim)
77+
pp (parts)
78+
79+
# variable number
80+
call io->eval(block, dollar0=line, pos_args=parts)
81+
}
5982
}
6083

61-
proc if-split-by (; ifs=null; block) {
84+
proc chop () {
85+
### alias for split-by
6286
echo TODO
6387
}
6488

65-
proc chop () {
66-
### alias for if-split-by
89+
proc test-split-by {
90+
var z = 'z' # test out scoping
91+
var count = 0 # test out mutation
92+
93+
# TODO: need split by space
94+
# Where the leading and trailing are split
95+
# if-split-by(' ') doesn't work well
96+
97+
line-data | split-by (/s+/) {
98+
99+
# how do we deal with nonexistent?
100+
# should we also bind _parts or _words?
101+
102+
echo "$z | $0 | $1 | $z"
103+
104+
setvar count += 1
105+
}
106+
echo "count = $count"
107+
}
108+
109+
proc must-split-by (; ; ifs=null; block) {
110+
### like if-split-by
111+
67112
echo TODO
68113
}
69114

115+
proc if-match (; pattern; ; block) {
116+
### like 'grep' but with submatches
117+
118+
for line in (io.stdin) {
119+
var m = line.search(pattern)
120+
if (m) {
121+
#pp asdl_ (m)
122+
#var groups = m.groups()
123+
124+
# Should we also pass _line?
125+
call io->eval(block, dollar0=m.group(0))
126+
}
127+
}
128+
129+
# always succeeds - I think must-match is the one that can fail
130+
}
131+
70132
proc must-match (; pattern; block) {
133+
### like if-match
134+
71135
echo TODO
72136
}
73137

74-
proc if-match (; pattern; block) {
75-
echo TODO
138+
proc line-data {
139+
# note: trailing ''' issue, I should probably get rid of the last line
140+
141+
echo '''
142+
prefix 30 foo
143+
oils
144+
/// 42 bar'''
145+
}
146+
147+
proc test-if-match {
148+
var z = 'z' # test out scoping
149+
var count = 0 # test out mutation
150+
151+
# Test cases should be like:
152+
# grep: print the matches, or just count them
153+
# sed: print a new line based on submatches
154+
# awk: re-arrange the cols, and also accumulate counters
155+
156+
var pat = /<capture d+> s+ <capture w+>/
157+
line-data | if-match (pat) {
158+
echo "$z $0 $z"
159+
# TODO: need pos_args
160+
161+
#echo "-- $2 $1 --"
162+
163+
setvar count += 1
164+
}
165+
echo "count = $count"
76166
}
77167

78168
# Protocol:

0 commit comments

Comments
 (0)