OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / libgo / go / go / printer / testdata / expressions.golden
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package expressions
6
7 type T struct {
8         x, y, z int
9 }
10
11 var (
12         a, b, c, d, e                                           int
13         under_bar                                               int
14         longIdentifier1, longIdentifier2, longIdentifier3       int
15         t0, t1, t2                                              T
16         s                                                       string
17         p                                                       *int
18 )
19
20
21 func _() {
22         // no spaces around simple or parenthesized expressions
23         _ = (a + 0)
24         _ = a + b
25         _ = a + b + c
26         _ = a + b - c
27         _ = a - b - c
28         _ = a + (b * c)
29         _ = a + (b / c)
30         _ = a - (b % c)
31         _ = 1 + a
32         _ = a + 1
33         _ = a + b + 1
34         _ = s[a]
35         _ = s[a:]
36         _ = s[:b]
37         _ = s[1:2]
38         _ = s[a:b]
39         _ = s[0:len(s)]
40         _ = s[0] << 1
41         _ = (s[0] << 1) & 0xf
42         _ = s[0]<<2 | s[1]>>4
43         _ = "foo" + s
44         _ = s + "foo"
45         _ = 'a' + 'b'
46         _ = len(s) / 2
47         _ = len(t0.x) / a
48
49         // spaces around expressions of different precedence or expressions containing spaces
50         _ = a + -b
51         _ = a - ^b
52         _ = a / *p
53         _ = a + b*c
54         _ = 1 + b*c
55         _ = a + 2*c
56         _ = a + c*2
57         _ = 1 + 2*3
58         _ = s[1 : 2*3]
59         _ = s[a : b-c]
60         _ = s[0:]
61         _ = s[a+b]
62         _ = s[:b-c]
63         _ = s[a+b:]
64         _ = a[a<<b+1]
65         _ = a[a<<b+1:]
66         _ = s[a+b : len(s)]
67         _ = s[len(s):-a]
68         _ = s[a : len(s)+1]
69         _ = s[a:len(s)+1] + s
70
71         // spaces around operators with equal or lower precedence than comparisons
72         _ = a == b
73         _ = a != b
74         _ = a > b
75         _ = a >= b
76         _ = a < b
77         _ = a <= b
78         _ = a < b && c > d
79         _ = a < b || c > d
80
81         // spaces around "long" operands
82         _ = a + longIdentifier1
83         _ = longIdentifier1 + a
84         _ = longIdentifier1 + longIdentifier2*longIdentifier3
85         _ = s + "a longer string"
86
87         // some selected cases
88         _ = a + t0.x
89         _ = a + t0.x + t1.x*t2.x
90         _ = a + b + c + d + e + 2*3
91         _ = a + b + c + 2*3 + d + e
92         _ = (a + b + c) * 2
93         _ = a - b + c - d + (a + b + c) + d&e
94         _ = under_bar - 1
95         _ = Open(dpath+"/file", O_WRONLY|O_CREAT, 0666)
96         _ = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx)
97 }
98
99
100 func _() {
101         a + b
102         a + b + c
103         a + b*c
104         a + (b * c)
105         (a + b) * c
106         a + (b * c * d)
107         a + (b*c + d)
108
109         1 << x
110         -1 << x
111         1<<x - 1
112         -1<<x - 1
113
114         f(a + b)
115         f(a + b + c)
116         f(a + b*c)
117         f(a + (b * c))
118         f(1<<x-1, 1<<x-2)
119
120         1<<d.logWindowSize - 1
121
122         buf = make(x, 2*cap(b.buf)+n)
123
124         dst[i*3+2] = dbuf[0] << 2
125         dst[i*3+2] = dbuf[0]<<2 | dbuf[1]>>4
126
127         b.buf = b.buf[0 : b.off+m+n]
128         b.buf = b.buf[0 : b.off+m*n]
129         f(b.buf[0 : b.off+m+n])
130
131         signed += ' ' * 8
132         tw.octal(header[148:155], chksum)
133
134         x > 0 && i >= 0
135
136         x1, x0 := x>>w2, x&m2
137         z0 = t1<<w2 + t0
138         z1 = (t1 + t0>>w2) >> w2
139         q1, r1 := x1/d1, x1%d1
140         r1 = r1*b2 | x0>>w2
141         x1 = (x1 << z) | (x0 >> (uint(w) - z))
142         x1 = x1<<z | x0>>(uint(w)-z)
143
144         buf[0 : len(buf)+1]
145         buf[0 : n+1]
146
147         a, b = b, a
148         a = b + c
149         a = b*c + d
150         a*b + c
151         a - b - c
152         a - (b - c)
153         a - b*c
154         a - (b * c)
155         a * b / c
156         a / *b
157         x[a|^b]
158         x[a / *b]
159         a & ^b
160         a + +b
161         a - -b
162         x[a*-b]
163         x[a + +b]
164         x ^ y ^ z
165         b[a>>24] ^ b[(a>>16)&0xFF] ^ b[(a>>8)&0xFF] ^ b[a&0xFF]
166         len(longVariableName) * 2
167
168         token(matchType + xlength<<lengthShift + xoffset)
169 }
170
171
172 func f(x int, args ...int) {
173         f(0, args...)
174         f(1, args)
175         f(2, args[0])
176
177         // make sure syntactically legal code remains syntactically legal
178         f(3, 42 ...)    // a blank must remain between 42 and ...
179         f(4, 42....)
180         f(5, 42....)
181         f(6, 42.0...)
182         f(7, 42.0...)
183         f(8, .42...)
184         f(9, .42...)
185         f(10, 42e0...)
186         f(11, 42e0...)
187
188         _ = 42 .x       // a blank must remain between 42 and .x
189         _ = 42..x
190         _ = 42..x
191         _ = 42.0.x
192         _ = 42.0.x
193         _ = .42.x
194         _ = .42.x
195         _ = 42e0.x
196         _ = 42e0.x
197
198         // a blank must remain between the binary operator and the 2nd operand
199         _ = x / *y
200         _ = x < -1
201         _ = x < <-1
202         _ = x + +1
203         _ = x - -1
204         _ = x & &x
205         _ = x & ^x
206
207         _ = f(x / *y, x < -1, x < <-1, x + +1, x - -1, x & &x, x & ^x)
208 }
209
210
211 func _() {
212         _ = T{}
213         _ = struct{}{}
214         _ = [10]T{}
215         _ = [...]T{}
216         _ = []T{}
217         _ = map[int]T{}
218 }
219
220
221 // one-line structs/interfaces in composite literals (up to a threshold)
222 func _() {
223         _ = struct{}{}
224         _ = struct{ x int }{0}
225         _ = struct{ x, y, z int }{0, 1, 2}
226         _ = struct{ int }{0}
227         _ = struct {
228                 s struct {
229                         int
230                 }
231         }{struct{ int }{0}}     // compositeLit context not propagated => multiLine result
232 }
233
234
235 func _() {
236         // do not modify literals
237         _ = "tab1       tab2    tab3    end"    // string contains 3 tabs
238         _ = "tab1 tab2 tab3 end"        // same string with 3 blanks - may be unaligned because editors see tabs in strings
239         _ = ""                          // this comment should be aligned with the one on the previous line
240         _ = ``
241         _ = `
242 `
243         _ = `foo
244                 bar`
245         _ = `three spaces before the end of the line starting here:   
246 they must not be removed`
247 }
248
249
250 func _() {
251         // one-line function literals (body is on a single line)
252         _ = func() {}
253         _ = func() int { return 0 }
254         _ = func(x, y int) bool { m := (x + y) / 2; return m < 0 }
255
256         // multi-line function literals (body is not on one line)
257         _ = func() {
258         }
259         _ = func() int {
260                 return 0
261         }
262         _ = func(x, y int) bool {
263                 m := (x + y) / 2
264                 return x < y
265         }
266
267         f(func() {
268         })
269         f(func() int {
270                 return 0
271         })
272         f(func(x, y int) bool {
273                 m := (x + y) / 2
274                 return x < y
275         })
276 }
277
278
279 func _() {
280         _ = [][]int{
281                 []int{1},
282                 []int{1, 2},
283                 []int{1, 2, 3},
284         }
285         _ = [][]int{
286                 {1},
287                 []int{1, 2},
288                 []int{1, 2, 3},
289         }
290         _ = [][]int{
291                 {1},
292                 {1, 2},
293                 {1, 2, 3},
294         }
295         _ = [][]int{{1}, {1, 2}, {1, 2, 3}}
296 }
297
298
299 // various multi-line expressions
300 func _() {
301         // do not add extra indentation to multi-line string lists
302         _ = "foo" + "bar"
303         _ = "foo" +
304                 "bar" +
305                 "bah"
306         _ = []string{
307                 "abc" +
308                         "def",
309                 "foo" +
310                         "bar",
311         }
312 }
313
314
315 const _ = F1 +
316         `string = "%s";` +
317         `ptr = *;` +
318         `datafmt.T2 = s ["-" p "-"];`
319
320
321 const _ = `datafmt "datafmt";` +
322         `default = "%v";` +
323         `array = *;` +
324         `datafmt.T3 = s  {" " a a / ","};`
325
326
327 const _ = `datafmt "datafmt";` +
328         `default = "%v";` +
329         `array = *;` +
330         `datafmt.T3 = s  {" " a a / ","};`
331
332
333 func _() {
334         _ = F1 +
335                 `string = "%s";` +
336                 `ptr = *;` +
337                 `datafmt.T2 = s ["-" p "-"];`
338
339         _ =
340                 `datafmt "datafmt";` +
341                         `default = "%v";` +
342                         `array = *;` +
343                         `datafmt.T3 = s  {" " a a / ","};`
344
345         _ = `datafmt "datafmt";` +
346                 `default = "%v";` +
347                 `array = *;` +
348                 `datafmt.T3 = s  {" " a a / ","};`
349 }
350
351
352 func _() {
353         // respect source lines in multi-line expressions
354         _ = a +
355                 b +
356                 c
357         _ = a < b ||
358                 b < a
359         _ = "933262154439441526816992388562667004907159682643816214685929" +
360                 "638952175999932299156089414639761565182862536979208272237582" +
361                 "51185210916864000000000000000000000000"        // 100!
362         _ = "170141183460469231731687303715884105727"   // prime
363 }
364
365
366 // Alignment after overlong lines
367 const (
368         _       = "991"
369         _       = "2432902008176640000" // 20!
370         _       = "933262154439441526816992388562667004907159682643816214685929" +
371                 "638952175999932299156089414639761565182862536979208272237582" +
372                 "51185210916864000000000000000000000000"        // 100!
373         _       = "170141183460469231731687303715884105727"     // prime
374 )
375
376
377 // Correct placement of operators and comments in multi-line expressions
378 func _() {
379         _ = a + // comment
380                 b +     // comment
381                 c
382         _ = "a" +
383                 "b" +   // comment
384                 "c"
385         _ = "ba0408" + "7265717569726564"       // field 71, encoding 2, string "required"
386 }
387
388
389 // Correct placement of terminating comma/closing parentheses in multi-line calls.
390 func _() {
391         f(1,
392                 2,
393                 3)
394         f(1,
395                 2,
396                 3,
397         )
398         f(1,
399                 2,
400                 3)      // comment
401         f(1,
402                 2,
403                 3,      // comment
404         )
405         f(1,
406                 2,
407                 3)      // comment
408         f(1,
409                 2,
410                 3,      // comment
411         )
412 }
413
414
415 // Align comments in multi-line lists of single-line expressions.
416 var txpix = [NCOL]draw.Color{
417         draw.Yellow,            // yellow
418         draw.Cyan,              // cyan
419         draw.Green,             // lime green
420         draw.GreyBlue,          // slate
421         draw.Red,               /* red */
422         draw.GreyGreen,         /* olive green */
423         draw.Blue,              /* blue */
424         draw.Color(0xFF55AAFF), /* pink */
425         draw.Color(0xFFAAFFFF), /* lavender */
426         draw.Color(0xBB005DFF), /* maroon */
427 }
428
429
430 func same(t, u *Time) bool {
431         // respect source lines in multi-line expressions
432         return t.Year == u.Year &&
433                 t.Month == u.Month &&
434                 t.Day == u.Day &&
435                 t.Hour == u.Hour &&
436                 t.Minute == u.Minute &&
437                 t.Second == u.Second &&
438                 t.Weekday == u.Weekday &&
439                 t.ZoneOffset == u.ZoneOffset &&
440                 t.Zone == u.Zone
441 }
442
443
444 func (p *parser) charClass() {
445         // respect source lines in multi-line expressions
446         if cc.negate && len(cc.ranges) == 2 &&
447                 cc.ranges[0] == '\n' && cc.ranges[1] == '\n' {
448                 nl := new(_NotNl)
449                 p.re.add(nl)
450         }
451 }
452
453
454 func addState(s []state, inst instr, match []int) {
455         // handle comments correctly in multi-line expressions
456         for i := 0; i < l; i++ {
457                 if s[i].inst.index() == index &&        // same instruction
458                         s[i].match[0] < pos {   // earlier match already going; leftmost wins
459                         return s
460                 }
461         }
462 }
463
464 func (self *T) foo(x int) *T    { return self }
465
466 func _()        { module.Func1().Func2() }
467
468 func _() {
469         _ = new(T).
470                 foo(1).
471                 foo(2).
472                 foo(3)
473
474         _ = new(T).
475                 foo(1).
476                 foo(2). // inline comments
477                 foo(3)
478
479         _ = new(T).foo(1).foo(2).foo(3)
480
481         // handle multiline argument list correctly
482         _ = new(T).
483                 foo(
484                         1).
485                 foo(2)
486
487         _ = new(T).foo(
488                 1).foo(2)
489
490         _ = Array[3+
491                 4]
492
493         _ = Method(1, 2,
494                 3)
495
496         _ = new(T).
497                 foo().
498                 bar().(*Type)
499
500         _ = new(T).
501                 foo().
502                 bar().(*Type).
503                 baz()
504
505         _ = new(T).
506                 foo().
507                 bar()["idx"]
508
509         _ = new(T).
510                 foo().
511                 bar()["idx"].
512                 baz()
513
514         _ = new(T).
515                 foo().
516                 bar()[1:2]
517
518         _ = new(T).
519                 foo().
520                 bar()[1:2].
521                 baz()
522
523         _ = new(T).
524                 Field.
525                 Array[3+
526                         4].
527                 Table["foo"].
528                 Blob.(*Type).
529                 Slices[1:4].
530                 Method(1, 2,
531                         3).
532                 Thingy
533
534         _ = a.b.c
535         _ = a.
536                 b.
537                 c
538         _ = a.b().c
539         _ = a.
540                 b().
541                 c
542         _ = a.b[0].c
543         _ = a.
544                 b[0].
545                 c
546         _ = a.b[0:].c
547         _ = a.
548                 b[0:].
549                 c
550         _ = a.b.(T).c
551         _ = a.
552                 b.(T).
553                 c
554 }