From: andy Date: Fri, 26 Sep 2008 19:18:19 +0000 (+0000) Subject: Fix parsing for degenerate cases like a[,] a[:] and {:}. The slicing syntax exposed... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=0180cae8c3f60cce6b518b25238ffd6480373104;p=simgear.git Fix parsing for degenerate cases like a[,] a[:] and {:}. The slicing syntax exposed the low-level generators to some new cases. --- diff --git a/simgear/nasal/code.c b/simgear/nasal/code.c index 44ff2a0e..11754b20 100644 --- a/simgear/nasal/code.c +++ b/simgear/nasal/code.c @@ -491,11 +491,12 @@ static void evalUnpack(naContext ctx, int count) // 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; } diff --git a/simgear/nasal/codegen.c b/simgear/nasal/codegen.c index 492b0f20..76050031 100644 --- a/simgear/nasal/codegen.c +++ b/simgear/nasal/codegen.c @@ -233,7 +233,7 @@ static void genHashElem(struct Parser* p, struct Token* t) { 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)); @@ -560,9 +560,10 @@ static void genAssign(struct Parser* p, struct Token* 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);