diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-05-15 16:48:04 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-05-15 16:48:04 -0300 |
| commit | 93d93a0bfbc30f1176e973e1238b51e560eeb233 (patch) | |
| tree | e2865f4a4e8f10fc8db6edd1b26020b292392be9 /lvm.c | |
| parent | 9e1f94fc1c3e8d4a8e68185223d03dafc1ee8050 (diff) | |
| download | lua-93d93a0bfbc30f1176e973e1238b51e560eeb233.tar.gz lua-93d93a0bfbc30f1176e973e1238b51e560eeb233.tar.bz2 lua-93d93a0bfbc30f1176e973e1238b51e560eeb233.zip | |
first implementation of `for' over tables
Diffstat (limited to 'lvm.c')
| -rw-r--r-- | lvm.c | 37 |
1 files changed, 33 insertions, 4 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.104 2000/04/19 13:36:25 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.105 2000/05/08 19:32:53 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 | */ |
| @@ -11,6 +11,7 @@ | |||
| 11 | 11 | ||
| 12 | #define LUA_REENTRANT | 12 | #define LUA_REENTRANT |
| 13 | 13 | ||
| 14 | #include "lapi.h" | ||
| 14 | #include "lauxlib.h" | 15 | #include "lauxlib.h" |
| 15 | #include "ldebug.h" | 16 | #include "ldebug.h" |
| 16 | #include "ldo.h" | 17 | #include "ldo.h" |
| @@ -634,12 +635,40 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
| 634 | if (ttype(top-3) != TAG_NUMBER) | 635 | if (ttype(top-3) != TAG_NUMBER) |
| 635 | lua_error(L, "`for' index must be a number"); | 636 | lua_error(L, "`for' index must be a number"); |
| 636 | index = nvalue(top-3)+step; | 637 | index = nvalue(top-3)+step; |
| 637 | if ((step>0) ? index<=limit : index>=limit) { | 638 | if ((step>0) ? index>limit : index<limit) |
| 639 | top -= 3; /* end loop: remove control variables */ | ||
| 640 | else { | ||
| 638 | nvalue(top-3) = index; | 641 | nvalue(top-3) = index; |
| 639 | pc += GETARG_S(i); | 642 | pc += GETARG_S(i); |
| 640 | } | 643 | } |
| 641 | else /* end of `for': remove control variables */ | 644 | break; |
| 642 | top -= 3; | 645 | } |
| 646 | |||
| 647 | case OP_LFORPREP: { | ||
| 648 | if (ttype(top-1) != TAG_TABLE) | ||
| 649 | lua_error(L, "`for' table must be a table"); | ||
| 650 | top += 3; /* counter + index,value */ | ||
| 651 | ttype(top-3) = TAG_NUMBER; | ||
| 652 | nvalue(top-3) = 0.0; /* counter */ | ||
| 653 | ttype(top-2) = ttype(top-1) = TAG_NIL; | ||
| 654 | pc += GETARG_S(i); | ||
| 655 | break; | ||
| 656 | } | ||
| 657 | |||
| 658 | case OP_LFORLOOP: { | ||
| 659 | int n; | ||
| 660 | top -= 2; /* remove old index,value */ | ||
| 661 | LUA_ASSERT(L, ttype(top-2) == TAG_TABLE, "invalid table"); | ||
| 662 | LUA_ASSERT(L, ttype(top-1) == TAG_NUMBER, "invalid counter"); | ||
| 663 | L->top = top; | ||
| 664 | n = luaA_next(L, avalue(top-2), (int)nvalue(top-1)); | ||
| 665 | if (n == 0) /* end loop? */ | ||
| 666 | top -= 2; /* remove table and counter */ | ||
| 667 | else { | ||
| 668 | nvalue(top-1) = (Number)n; | ||
| 669 | top += 2; /* new index,value */ | ||
| 670 | pc += GETARG_S(i); /* repeat loop */ | ||
| 671 | } | ||
| 643 | break; | 672 | break; |
| 644 | } | 673 | } |
| 645 | 674 | ||
