Commit 39a2e82e authored by Evan Shaw's avatar Evan Shaw Committed by Robert Griesemer

exp/eval: Implement x[lo:]

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/908044
parent a318f9d3
...@@ -589,14 +589,16 @@ func (a *exprCompiler) compile(x ast.Expr, callCtx bool) *expr { ...@@ -589,14 +589,16 @@ func (a *exprCompiler) compile(x ast.Expr, callCtx bool) *expr {
return ei.compileIndexExpr(l, r) return ei.compileIndexExpr(l, r)
case *ast.SliceExpr: case *ast.SliceExpr:
end := x.End var hi *expr
if end == nil {
// TODO: set end to len(x.X)
panic("unimplemented")
}
arr := a.compile(x.X, false) arr := a.compile(x.X, false)
lo := a.compile(x.Index, 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 { if arr == nil || lo == nil || hi == nil {
return nil return nil
} }
......
...@@ -85,6 +85,15 @@ var exprTests = []test{ ...@@ -85,6 +85,15 @@ var exprTests = []test{
RErr("s[-i]", "negative index"), RErr("s[-i]", "negative index"),
RErr("s[3]", "index 3 exceeds"), 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("1(2)", "cannot call"),
CErr("fn(1,2)", "too many"), CErr("fn(1,2)", "too many"),
CErr("fn()", "not enough"), CErr("fn()", "not enough"),
...@@ -112,8 +121,14 @@ var exprTests = []test{ ...@@ -112,8 +121,14 @@ var exprTests = []test{
Val("len(s)", 3), Val("len(s)", 3),
Val("len(ai)", 2), Val("len(ai)", 2),
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(aai)", 2),
Val("len(sli)", 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 // TODO(austin) Test len of map
CErr("len(0)", opTypes), CErr("len(0)", opTypes),
CErr("len(i)", opTypes), CErr("len(i)", opTypes),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment