diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-12 15:57:19 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-12 15:57:19 -0300 |
commit | f9cf402fbd1ba2ab00f5aa5f7d0ff5c9c0580dd5 (patch) | |
tree | 7bed49a7ff5c730cdfdef7c3a43b71e039d4442b /lvm.c | |
parent | 0c3fe2c44be155b11b52230e36f45e688bae5ce2 (diff) | |
download | lua-f9cf402fbd1ba2ab00f5aa5f7d0ff5c9c0580dd5.tar.gz lua-f9cf402fbd1ba2ab00f5aa5f7d0ff5c9c0580dd5.tar.bz2 lua-f9cf402fbd1ba2ab00f5aa5f7d0ff5c9c0580dd5.zip |
first implementation of FOR
Diffstat (limited to 'lvm.c')
-rw-r--r-- | lvm.c | 30 |
1 files changed, 29 insertions, 1 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.99 2000/04/04 20:48:44 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.100 2000/04/07 13:13:11 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -630,6 +630,34 @@ StkId luaV_execute (lua_State *L, const Closure *cl, register StkId base) { | |||
630 | pc++; | 630 | pc++; |
631 | break; | 631 | break; |
632 | 632 | ||
633 | case OP_FORPREP: | ||
634 | if (tonumber(top-1)) | ||
635 | lua_error(L, "`for' step must be a number"); | ||
636 | if (tonumber(top-2)) | ||
637 | lua_error(L, "`for' limit must be a number"); | ||
638 | if (tonumber(top-3)) | ||
639 | lua_error(L, "`for' initial value must be a number"); | ||
640 | nvalue(top-3) -= nvalue(top-1); /* to be undone by first FORLOOP */ | ||
641 | pc += GETARG_S(i); | ||
642 | break; | ||
643 | |||
644 | case OP_FORLOOP: { | ||
645 | Number step = nvalue(top-1); | ||
646 | Number limit = nvalue(top-2); | ||
647 | Number index; | ||
648 | LUA_ASSERT(L, ttype(top-1) == TAG_NUMBER, "invalid step"); | ||
649 | LUA_ASSERT(L, ttype(top-2) == TAG_NUMBER, "invalid limit"); | ||
650 | if (tonumber(top-3)) lua_error(L, "`for' index must be a number"); | ||
651 | index = nvalue(top-3)+step; | ||
652 | if ((step>0) ? index<=limit : index>=limit) { | ||
653 | nvalue(top-3) = index; | ||
654 | pc += GETARG_S(i); | ||
655 | } | ||
656 | else /* end of `for': remove control variables */ | ||
657 | top -= 3; | ||
658 | break; | ||
659 | } | ||
660 | |||
633 | case OP_CLOSURE: | 661 | case OP_CLOSURE: |
634 | L->top = top; | 662 | L->top = top; |
635 | luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i)); | 663 | luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i)); |