]> Cypherpunks repositories - gostls13.git/commitdiff
exp/eval: Implement x[lo:]
authorEvan Shaw <chickencha@gmail.com>
Fri, 16 Apr 2010 01:04:24 +0000 (18:04 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 16 Apr 2010 01:04:24 +0000 (18:04 -0700)
R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/908044

src/pkg/exp/eval/expr.go
src/pkg/exp/eval/expr_test.go

index 15520830ffa6f88ac0313150bfe74b24d708a4ef..8651b07806a2e33661f8f5e1a2203c28127c5d03 100644 (file)
@@ -589,14 +589,16 @@ func (a *exprCompiler) compile(x ast.Expr, callCtx bool) *expr {
                return ei.compileIndexExpr(l, r)
 
        case *ast.SliceExpr:
-               end := x.End
-               if end == nil {
-                       // TODO: set end to len(x.X)
-                       panic("unimplemented")
-               }
+               var hi *expr
                arr := a.compile(x.X, false)
                lo := a.compile(x.Index, false)
-               hi := a.compile(end, false)
+               if x.End == nil {
+                       // End was omitted, so we need to compute len(x.X)
+                       ei := &exprInfo{a.compiler, x.Pos()}
+                       hi = ei.compileBuiltinCallExpr(a.block, lenType, []*expr{arr})
+               } else {
+                       hi = a.compile(x.End, false)
+               }
                if arr == nil || lo == nil || hi == nil {
                        return nil
                }
index 10c7f6be5281bed6965d323b414d86d05f26221e..f7f367d5e90fdfbcf22739a1e54164709678516e 100644 (file)
@@ -85,6 +85,15 @@ var exprTests = []test{
        RErr("s[-i]", "negative index"),
        RErr("s[3]", "index 3 exceeds"),
 
+       Val("ai[0:2]", vslice{varray{1, 2}, 2, 2}),
+       Val("ai[0:1]", vslice{varray{1, 2}, 1, 2}),
+       Val("ai[0:]", vslice{varray{1, 2}, 2, 2}),
+       Val("ai[i:]", vslice{varray{2}, 1, 1}),
+
+       Val("sli[0:2]", vslice{varray{1, 2, 3}, 2, 3}),
+       Val("sli[0:i]", vslice{varray{1, 2, 3}, 1, 3}),
+       Val("sli[1:]", vslice{varray{2, 3}, 1, 2}),
+
        CErr("1(2)", "cannot call"),
        CErr("fn(1,2)", "too many"),
        CErr("fn()", "not enough"),
@@ -112,8 +121,14 @@ var exprTests = []test{
        Val("len(s)", 3),
        Val("len(ai)", 2),
        Val("len(&ai)", 2),
+       Val("len(ai[0:])", 2),
+       Val("len(ai[1:])", 1),
+       Val("len(ai[2:])", 0),
        Val("len(aai)", 2),
        Val("len(sli)", 2),
+       Val("len(sli[0:])", 2),
+       Val("len(sli[1:])", 1),
+       Val("len(sli[2:])", 0),
        // TODO(austin) Test len of map
        CErr("len(0)", opTypes),
        CErr("len(i)", opTypes),