/
chaining.co
178 lines (133 loc) · 2.74 KB
/
chaining.co
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Basic chained function calls.
identityWrap = (x) -> -> x
eq true, identityWrap(identityWrap(true))()()
# Chained accesses split on dot + newline, backwards and forwards.
$ = {}
$.self = $
$.begin = $.do = $.end = -> this
ok $.
begin().
do().
self.
self.
end().
self
# Leading dots introduce dummy blocks and/or close implicit indentations.
r = $.self
.begin()
.do()
.do -> 0; 1
.begin()
.do ->
2
3
.end 4, ->
.end()
eq r, $
# Paren-free method chains
eq \56, if \0 is 10.toString 2 .slice 3
\4 .replace /\d/ 5 .concat 6
eq 1,
Array 0, 1 ?.1
# Ensure that indented array literals don't trigger whitespace rewriting.
eq 2 [
[[[[],
[]],
[[]]]],
[]].length
eq 'Hello', String(
"""
Hello
""")
eq msg = 'special accessors should also continue lines',
msg
.~toString
?.valueOf()()
eq 0,
[0]
?.0
# Bracketless Accesses
a = [0]
eq 0, a.0
eq 0, a."0"
eq 0, a."#{0}"
eq 0, a.(0)
eq 0, [a].0.0
eq a.* = 1, a.1
eq '0'.0, '10'.1
eq 1, [-> it]. 0 1
eq 1, [-> it].'0' 1
# `prototype`/`constructor` shorhands
eq Array::toString, Array.prototype.toString
eq 12345..toString, 123.constructor.toString
eq 0 (:::0)::
eq 0 (..:0)..
# Length Star
a = [[1], 2, 3]
eq 3, a[*-1]
eq 1, a[0][*-*]
eq 2, a[--*-1]
eq 2, a.length
compileThrows 'stray star' 1 '[*-1]'
# Binding Access
parent =
child:
method: -> @member
member: 42
eq 42, do(0; parent.child.~method)
eq 42, do(0; parent.child~"me#{'th'}od")
eq 42, parent.child. ~ [\method] null
eq 42, parent.child.~{method}.method!
compileThrows 'invalid assign' 1 'o~m=g'
# Dots have to workaround syntax error when accessing a simple number.
eq '0 .go;' , Coco.compile '0.go', {+bare}
# Brackets don't.
eq "0['do'];", Coco.compile '0.do', {+bare}
# Array/Object Slice
eq '2,3', '' + [3,2][1,0]
eq '2,3', '' + [0,1,2,3][*-2,*-1]
eq '2,3', '' + {2,3}<[2 3]>
eq '-Infinity,Infinity', '' + Number[\NEGATIVE_INFINITY, \POSITIVE_INFINITY]
k = \y
o = {\x \y \z}{x, (k), 2: z}
eq \x o.x
eq \y o.y
eq \z o.2
a = [0 1 2][[0 1], {2}]
eq '0,1' ''+a.0
eq '0,1' ''+a[...0]
eq 2 a.1.2
compileThrows 'calling a slice' 1 'a{0}()'
compileThrows 'empty slice' 1 'o{}'
compileThrows 'empty slice' 1 'o[,,]'
if 0 then @front{ne,ss}
# Automatic Dot Insertion
eq @toString, @\toString
eq @toString, @"to#{\S}tring"
{ok}\ok 1
[ok]0 1
eq 0 [[0]]0.0
eq null [null]?0
eq void {0}?1?p
v = void
x = y: {\z}
eq void v?=y.z
eq void v
eq \z x?=y.z
eq \z x
# Semiautovivification
o = {}
o.@a.@@b .push 0 1
o.a@c@@d .push 2 3
o?.@a?.@@b?@e?@@f.4 = 5
eq '0,1' ''+o.a.b
eq '2,3' ''+o.a.c.d
eq 5 o.a.b.e.f.4
# Bang Call
eq '' String!
(-> ok true)!
f = -> null
eq null f?!
eq void f!?p
f = void
eq void f?!