diff options
Diffstat (limited to '')
| -rw-r--r-- | table.c | 77 | ||||
| -rw-r--r-- | table.h | 4 |
2 files changed, 60 insertions, 21 deletions
| @@ -1,10 +1,10 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** table.c | 2 | ** table.c |
| 3 | ** Module to control static tables | 3 | ** Module to control static tables |
| 4 | ** TeCGraf - PUC-Rio | ||
| 5 | ** 11 May 93 | ||
| 6 | */ | 4 | */ |
| 7 | 5 | ||
| 6 | char *rcs_table="$Id: $"; | ||
| 7 | |||
| 8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
| 9 | #include <string.h> | 9 | #include <string.h> |
| 10 | 10 | ||
| @@ -14,7 +14,7 @@ | |||
| 14 | #include "table.h" | 14 | #include "table.h" |
| 15 | #include "lua.h" | 15 | #include "lua.h" |
| 16 | 16 | ||
| 17 | #define streq(s1,s2) (strcmp(s1,s2)==0) | 17 | #define streq(s1,s2) (s1[0]==s2[0]&&strcmp(s1+1,s2+1)==0) |
| 18 | 18 | ||
| 19 | #ifndef MAXSYMBOL | 19 | #ifndef MAXSYMBOL |
| 20 | #define MAXSYMBOL 512 | 20 | #define MAXSYMBOL 512 |
| @@ -24,10 +24,27 @@ static Symbol tablebuffer[MAXSYMBOL] = { | |||
| 24 | {"tonumber",{T_CFUNCTION,{lua_obj2number}}}, | 24 | {"tonumber",{T_CFUNCTION,{lua_obj2number}}}, |
| 25 | {"next",{T_CFUNCTION,{lua_next}}}, | 25 | {"next",{T_CFUNCTION,{lua_next}}}, |
| 26 | {"nextvar",{T_CFUNCTION,{lua_nextvar}}}, | 26 | {"nextvar",{T_CFUNCTION,{lua_nextvar}}}, |
| 27 | {"print",{T_CFUNCTION,{lua_print}}} | 27 | {"print",{T_CFUNCTION,{lua_print}}}, |
| 28 | {"dofile",{T_CFUNCTION,{lua_internaldofile}}}, | ||
| 29 | {"dostring",{T_CFUNCTION,{lua_internaldostring}}} | ||
| 28 | }; | 30 | }; |
| 29 | Symbol *lua_table=tablebuffer; | 31 | Symbol *lua_table=tablebuffer; |
| 30 | Word lua_ntable=5; | 32 | Word lua_ntable=7; |
| 33 | |||
| 34 | struct List | ||
| 35 | { | ||
| 36 | Symbol *s; | ||
| 37 | struct List *next; | ||
| 38 | }; | ||
| 39 | |||
| 40 | static struct List o6={ tablebuffer+6, 0}; | ||
| 41 | static struct List o5={ tablebuffer+5, &o6 }; | ||
| 42 | static struct List o4={ tablebuffer+4, &o5 }; | ||
| 43 | static struct List o3={ tablebuffer+3, &o4 }; | ||
| 44 | static struct List o2={ tablebuffer+2, &o3 }; | ||
| 45 | static struct List o1={ tablebuffer+1, &o2 }; | ||
| 46 | static struct List o0={ tablebuffer+0, &o1 }; | ||
| 47 | static struct List *searchlist=&o0; | ||
| 31 | 48 | ||
| 32 | #ifndef MAXCONSTANT | 49 | #ifndef MAXCONSTANT |
| 33 | #define MAXCONSTANT 256 | 50 | #define MAXCONSTANT 256 |
| @@ -65,10 +82,19 @@ int lua_nfile; | |||
| 65 | */ | 82 | */ |
| 66 | int lua_findsymbol (char *s) | 83 | int lua_findsymbol (char *s) |
| 67 | { | 84 | { |
| 68 | int i; | 85 | struct List *l, *p; |
| 69 | for (i=0; i<lua_ntable; i++) | 86 | for (p=NULL, l=searchlist; l!=NULL; p=l, l=l->next) |
| 70 | if (streq(s,s_name(i))) | 87 | if (streq(s,l->s->name)) |
| 71 | return i; | 88 | { |
| 89 | if (p!=NULL) | ||
| 90 | { | ||
| 91 | p->next = l->next; | ||
| 92 | l->next = searchlist; | ||
| 93 | searchlist = l; | ||
| 94 | } | ||
| 95 | return (l->s-lua_table); | ||
| 96 | } | ||
| 97 | |||
| 72 | if (lua_ntable >= MAXSYMBOL-1) | 98 | if (lua_ntable >= MAXSYMBOL-1) |
| 73 | { | 99 | { |
| 74 | lua_error ("symbol table overflow"); | 100 | lua_error ("symbol table overflow"); |
| @@ -80,9 +106,13 @@ int lua_findsymbol (char *s) | |||
| 80 | lua_error ("not enough memory"); | 106 | lua_error ("not enough memory"); |
| 81 | return -1; | 107 | return -1; |
| 82 | } | 108 | } |
| 83 | s_tag(lua_ntable++) = T_NIL; | 109 | s_tag(lua_ntable) = T_NIL; |
| 84 | 110 | p = malloc(sizeof(*p)); | |
| 85 | return (lua_ntable-1); | 111 | p->s = lua_table+lua_ntable; |
| 112 | p->next = searchlist; | ||
| 113 | searchlist = p; | ||
| 114 | |||
| 115 | return lua_ntable++; | ||
| 86 | } | 116 | } |
| 87 | 117 | ||
| 88 | /* | 118 | /* |
| @@ -108,12 +138,12 @@ int lua_findenclosedconstant (char *s) | |||
| 108 | { | 138 | { |
| 109 | if (s[i] == '\\') | 139 | if (s[i] == '\\') |
| 110 | { | 140 | { |
| 111 | switch (s[++i]) | 141 | switch (s[i+1]) |
| 112 | { | 142 | { |
| 113 | case 'n': c[j++] = '\n'; break; | 143 | case 'n': c[j++] = '\n'; i++; break; |
| 114 | case 't': c[j++] = '\t'; break; | 144 | case 't': c[j++] = '\t'; i++; break; |
| 115 | case 'r': c[j++] = '\r'; break; | 145 | case 'r': c[j++] = '\r'; i++; break; |
| 116 | default : c[j++] = '\\'; c[j++] = c[i]; break; | 146 | default : c[j++] = '\\'; break; |
| 117 | } | 147 | } |
| 118 | } | 148 | } |
| 119 | else | 149 | else |
| @@ -295,6 +325,15 @@ int lua_addfile (char *fn) | |||
| 295 | } | 325 | } |
| 296 | 326 | ||
| 297 | /* | 327 | /* |
| 328 | ** Delete a file from file stack | ||
| 329 | */ | ||
| 330 | int lua_delfile (void) | ||
| 331 | { | ||
| 332 | lua_nfile--; | ||
| 333 | return 1; | ||
| 334 | } | ||
| 335 | |||
| 336 | /* | ||
| 298 | ** Return the last file name set. | 337 | ** Return the last file name set. |
| 299 | */ | 338 | */ |
| 300 | char *lua_filename (void) | 339 | char *lua_filename (void) |
| @@ -332,9 +371,9 @@ void lua_nextvar (void) | |||
| 332 | return; | 371 | return; |
| 333 | } | 372 | } |
| 334 | index++; | 373 | index++; |
| 335 | while (index < lua_ntable-1 && tag(&s_object(index)) == T_NIL) index++; | 374 | while (index < lua_ntable && tag(&s_object(index)) == T_NIL) index++; |
| 336 | 375 | ||
| 337 | if (index == lua_ntable-1) | 376 | if (index == lua_ntable) |
| 338 | { | 377 | { |
| 339 | lua_pushnil(); | 378 | lua_pushnil(); |
| 340 | lua_pushnil(); | 379 | lua_pushnil(); |
| @@ -1,8 +1,7 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** table.c | ||
| 3 | ** Module to control static tables | 2 | ** Module to control static tables |
| 4 | ** TeCGraf - PUC-Rio | 3 | ** TeCGraf - PUC-Rio |
| 5 | ** 11 May 93 | 4 | ** $Id: $ |
| 6 | */ | 5 | */ |
| 7 | 6 | ||
| 8 | #ifndef table_h | 7 | #ifndef table_h |
| @@ -33,6 +32,7 @@ void lua_markobject (Object *o); | |||
| 33 | char *lua_createstring (char *s); | 32 | char *lua_createstring (char *s); |
| 34 | void *lua_createarray (void *a); | 33 | void *lua_createarray (void *a); |
| 35 | int lua_addfile (char *fn); | 34 | int lua_addfile (char *fn); |
| 35 | int lua_delfile (void); | ||
| 36 | char *lua_filename (void); | 36 | char *lua_filename (void); |
| 37 | void lua_nextvar (void); | 37 | void lua_nextvar (void); |
| 38 | 38 | ||
