// FIXME: unify with almost identical checkVec() above
static int vbound(naContext ctx, naRef v, naRef ir, int end)
{
- int i = IS_NIL(ir) ? (end ? -1 : 0) : numify(ctx, ir);
- if(i < 0) i += naVec_size(v);
- if(i < 0 || i >= naVec_size(v))
+ int sz=naVec_size(v), i = IS_NIL(ir) ? (end ? -1 : 0) : numify(ctx, ir);
+ if(IS_NIL(ir) && !sz) return i;
+ if(i < 0) i += sz;
+ if(i < 0 || i >= sz)
naRuntimeError(ctx, "slice index %d out of bounds (size: %d)",
- i, naVec_size(v));
+ i, sz);
return i;
}
{
if(!t || t->type == TOK_EMPTY)
return;
- if(t->type != TOK_COLON)
+ if(t->type != TOK_COLON || !LEFT(t))
naParseError(p, "bad hash/object initializer", t->line);
if(LEFT(t)->type == TOK_SYMBOL) genScalarConstant(p, LEFT(t));
else if(LEFT(t)->type == TOK_LITERAL) genExpr(p, LEFT(t));
static void genSlice(struct Parser* p, struct Token* t)
{
+ if(!t) naParseError(p, "empty slice expression", -1);
if(t->type == TOK_COLON) {
- genExpr(p, LEFT(t));
- genExpr(p, RIGHT(t));
+ if(LEFT(t)) genExpr(p, LEFT(t)); else emit(p, OP_PUSHNIL);
+ if(RIGHT(t)) genExpr(p, RIGHT(t)); else emit(p, OP_PUSHNIL);
emit(p, OP_SLICE2);
} else {
genExpr(p, t);