diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-04-07 11:48:53 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-04-07 11:48:53 -0300 |
commit | c9e3d321828dd095d46e295873bc991f0c9ed3d7 (patch) | |
tree | 1efce0077f163b1bc0b733327e582e73fe8ac8a3 | |
parent | 00050b8a6b11c3ac2032ca147d236743b90f5168 (diff) | |
download | lua-c9e3d321828dd095d46e295873bc991f0c9ed3d7.tar.gz lua-c9e3d321828dd095d46e295873bc991f0c9ed3d7.tar.bz2 lua-c9e3d321828dd095d46e295873bc991f0c9ed3d7.zip |
first implementation of "$if";
new function "findstring" (useful in good places)
-rw-r--r-- | auxlib.c | 14 | ||||
-rw-r--r-- | auxlib.h | 9 | ||||
-rw-r--r-- | fallback.c | 91 | ||||
-rw-r--r-- | lex.c | 87 | ||||
-rw-r--r-- | table.c | 8 | ||||
-rw-r--r-- | table.h | 3 |
6 files changed, 139 insertions, 73 deletions
@@ -1,13 +1,25 @@ | |||
1 | char *rcs_auxlib="$Id: auxlib.c,v 1.2 1997/03/18 15:30:50 roberto Exp roberto $"; | 1 | char *rcs_auxlib="$Id: auxlib.c,v 1.3 1997/04/06 14:08:08 roberto Exp roberto $"; |
2 | 2 | ||
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <stdarg.h> | 4 | #include <stdarg.h> |
5 | #include <string.h> | ||
5 | 6 | ||
6 | #include "lua.h" | 7 | #include "lua.h" |
7 | #include "auxlib.h" | 8 | #include "auxlib.h" |
8 | #include "luadebug.h" | 9 | #include "luadebug.h" |
9 | 10 | ||
10 | 11 | ||
12 | |||
13 | int luaI_findstring (char *name, char *list[]) | ||
14 | { | ||
15 | int i; | ||
16 | for (i=0; list[i]; i++) | ||
17 | if (strcmp(list[i], name) == 0) | ||
18 | return i; | ||
19 | return -1; /* name not found */ | ||
20 | } | ||
21 | |||
22 | |||
11 | void luaL_arg_check(int cond, int numarg, char *extramsg) | 23 | void luaL_arg_check(int cond, int numarg, char *extramsg) |
12 | { | 24 | { |
13 | char *funcname; | 25 | char *funcname; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: auxlib.h,v 1.1 1997/03/18 15:30:50 roberto Exp $ | 2 | ** $Id: auxlib.h,v 1.2 1997/04/06 14:08:08 roberto Exp roberto $ |
3 | */ | 3 | */ |
4 | 4 | ||
5 | #ifndef auxlib_h | 5 | #ifndef auxlib_h |
@@ -20,4 +20,11 @@ double luaL_check_number (int numArg); | |||
20 | double luaL_opt_number (int numArg, double def); | 20 | double luaL_opt_number (int numArg, double def); |
21 | void luaL_verror (char *fmt, ...); | 21 | void luaL_verror (char *fmt, ...); |
22 | 22 | ||
23 | |||
24 | |||
25 | /* -- private part (only for Lua modules */ | ||
26 | |||
27 | int luaI_findstring (char *name, char *list[]); | ||
28 | |||
29 | |||
23 | #endif | 30 | #endif |
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_fallback="$Id: fallback.c,v 2.2 1997/04/04 22:24:51 roberto Exp roberto $"; | 6 | char *rcs_fallback="$Id: fallback.c,v 2.3 1997/04/06 14:08:08 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <string.h> | 9 | #include <string.h> |
@@ -102,18 +102,9 @@ char *luaI_eventname[] = { /* ORDER IM */ | |||
102 | 102 | ||
103 | 103 | ||
104 | 104 | ||
105 | static int findstring (char *name, char *list[]) | ||
106 | { | ||
107 | int i; | ||
108 | for (i=0; list[i]; i++) | ||
109 | if (strcmp(list[i], name) == 0) | ||
110 | return i; | ||
111 | return -1; /* name not found */ | ||
112 | } | ||
113 | |||
114 | static int luaI_checkevent (char *name, char *list[]) | 105 | static int luaI_checkevent (char *name, char *list[]) |
115 | { | 106 | { |
116 | int e = findstring(name, list); | 107 | int e = luaI_findstring(name, list); |
117 | if (e < 0) | 108 | if (e < 0) |
118 | luaL_verror("`%s' is not a valid event name", name); | 109 | luaL_verror("`%s' is not a valid event name", name); |
119 | return e; | 110 | return e; |
@@ -197,11 +188,8 @@ void luaI_settag (int tag, TObject *o) | |||
197 | case LUA_T_ARRAY: | 188 | case LUA_T_ARRAY: |
198 | o->value.a->htag = tag; | 189 | o->value.a->htag = tag; |
199 | break; | 190 | break; |
200 | case LUA_T_USERDATA: | ||
201 | o->value.ts->tag = tag; | ||
202 | break; | ||
203 | default: | 191 | default: |
204 | luaL_verror("cannot change tag of a %s", luaI_typenames[-ttype(o)]); | 192 | luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]); |
205 | } | 193 | } |
206 | } | 194 | } |
207 | 195 | ||
@@ -318,45 +306,52 @@ static void fillvalids (IMS e, TObject *func) | |||
318 | 306 | ||
319 | void luaI_setfallback (void) | 307 | void luaI_setfallback (void) |
320 | { | 308 | { |
321 | int e; | 309 | static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL}; |
322 | TObject oldfunc; | 310 | TObject oldfunc; |
323 | lua_CFunction replace; | 311 | lua_CFunction replace; |
324 | char *name = luaL_check_string(1); | 312 | char *name = luaL_check_string(1); |
325 | lua_Object func = lua_getparam(2); | 313 | lua_Object func = lua_getparam(2); |
326 | luaI_initfallbacks(); | 314 | luaI_initfallbacks(); |
327 | luaL_arg_check(lua_isfunction(func), 2, "function expected"); | 315 | luaL_arg_check(lua_isfunction(func), 2, "function expected"); |
328 | if (strcmp(name, "error") == 0) { /* old error fallback */ | 316 | switch (luaI_findstring(name, oldnames)) { |
329 | oldfunc = errorim; | 317 | case 0: /* old error fallback */ |
330 | errorim = *luaI_Address(func); | 318 | oldfunc = errorim; |
331 | replace = errorFB; | 319 | errorim = *luaI_Address(func); |
332 | } | 320 | replace = errorFB; |
333 | else if (strcmp(name, "getglobal") == 0) { /* old getglobal fallback */ | 321 | break; |
334 | oldfunc = luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL]; | 322 | case 1: /* old getglobal fallback */ |
335 | luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL] = *luaI_Address(func); | 323 | oldfunc = luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL]; |
336 | replace = nilFB; | 324 | luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL] = *luaI_Address(func); |
337 | } | 325 | replace = nilFB; |
338 | else if ((e = findstring(name, luaI_eventname)) >= 0) { | 326 | break; |
339 | oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[e]; | 327 | case 2: { /* old arith fallback */ |
340 | fillvalids(e, luaI_Address(func)); | 328 | int i; |
341 | replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB; | 329 | oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_POW]; |
342 | } | 330 | for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */ |
343 | else if (strcmp(name, "arith") == 0) { /* old arith fallback */ | 331 | fillvalids(i, luaI_Address(func)); |
344 | int i; | 332 | replace = typeFB; |
345 | oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_POW]; | 333 | break; |
346 | for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */ | 334 | } |
347 | fillvalids(i, luaI_Address(func)); | 335 | case 3: { /* old order fallback */ |
348 | replace = typeFB; | 336 | int i; |
349 | } | 337 | oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_LT]; |
350 | else if (strcmp(name, "order") == 0) { /* old order fallback */ | 338 | for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */ |
351 | int i; | 339 | fillvalids(i, luaI_Address(func)); |
352 | oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_LT]; | 340 | replace = typeFB; |
353 | for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */ | 341 | break; |
354 | fillvalids(i, luaI_Address(func)); | 342 | } |
355 | replace = typeFB; | 343 | default: { |
356 | } | 344 | int e; |
357 | else { | 345 | if ((e = luaI_findstring(name, luaI_eventname)) >= 0) { |
358 | luaL_verror("`%s' is not a valid fallback name", name); | 346 | oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[e]; |
359 | replace = NULL; /* to avoid warnings */ | 347 | fillvalids(e, luaI_Address(func)); |
348 | replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB; | ||
349 | } | ||
350 | else { | ||
351 | luaL_verror("`%s' is not a valid fallback name", name); | ||
352 | replace = NULL; /* to avoid warnings */ | ||
353 | } | ||
354 | } | ||
360 | } | 355 | } |
361 | if (oldfunc.ttype != LUA_T_NIL) | 356 | if (oldfunc.ttype != LUA_T_NIL) |
362 | luaI_pushobject(&oldfunc); | 357 | luaI_pushobject(&oldfunc); |
@@ -1,4 +1,4 @@ | |||
1 | char *rcs_lex = "$Id: lex.c,v 2.44 1997/03/31 14:17:09 roberto Exp roberto $"; | 1 | char *rcs_lex = "$Id: lex.c,v 2.45 1997/04/01 21:23:20 roberto Exp roberto $"; |
2 | 2 | ||
3 | 3 | ||
4 | #include <ctype.h> | 4 | #include <ctype.h> |
@@ -22,27 +22,31 @@ char *rcs_lex = "$Id: lex.c,v 2.44 1997/03/31 14:17:09 roberto Exp roberto $"; | |||
22 | 22 | ||
23 | static int current; /* look ahead character */ | 23 | static int current; /* look ahead character */ |
24 | static Input input; /* input function */ | 24 | static Input input; /* input function */ |
25 | static int iflevel; /* level of nested $if's */ | ||
25 | 26 | ||
26 | 27 | ||
27 | void lua_setinput (Input fn) | 28 | void lua_setinput (Input fn) |
28 | { | 29 | { |
29 | current = '\n'; | 30 | current = '\n'; |
30 | lua_linenumber = 0; | 31 | lua_linenumber = 0; |
32 | iflevel = 0; | ||
31 | input = fn; | 33 | input = fn; |
32 | } | 34 | } |
33 | 35 | ||
34 | static void luaI_auxsyntaxerror (char *s, char *token) | 36 | static void luaI_auxsyntaxerror (char *s, char *token) |
35 | { | 37 | { |
38 | if (token == NULL) | ||
39 | luaL_verror("%s;\n> at line %d in file %s", | ||
40 | s, lua_linenumber, lua_parsedfile); | ||
41 | if (token[0] == 0) | ||
42 | token = "<eof>"; | ||
36 | luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s", | 43 | luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s", |
37 | s, token, lua_linenumber, lua_parsedfile); | 44 | s, token, lua_linenumber, lua_parsedfile); |
38 | } | 45 | } |
39 | 46 | ||
40 | void luaI_syntaxerror (char *s) | 47 | void luaI_syntaxerror (char *s) |
41 | { | 48 | { |
42 | char *token = luaI_buffer(1); | 49 | luaI_auxsyntaxerror(s, luaI_buffer(1)); |
43 | if (token[0] == 0) | ||
44 | token = "<eof>"; | ||
45 | luaI_auxsyntaxerror(s, token); | ||
46 | } | 50 | } |
47 | 51 | ||
48 | 52 | ||
@@ -82,24 +86,63 @@ void luaI_addReserved (void) | |||
82 | } | 86 | } |
83 | } | 87 | } |
84 | 88 | ||
85 | static int inclinenumber (int pragma_allowed) | 89 | |
90 | static void readname (char *buff) | ||
86 | { | 91 | { |
92 | int i = 0; | ||
93 | while (current == ' ') next(); | ||
94 | while (isalnum((unsigned char)current)) { | ||
95 | if (i >= MINBUFF) luaI_syntaxerror("pragma too long"); | ||
96 | buff[i++] = current; | ||
97 | next(); | ||
98 | } | ||
99 | buff[i] = 0; | ||
100 | if (!isalpha(buff[0]) || (current != 0 && !isspace(current))) | ||
101 | luaI_auxsyntaxerror("invalid pragma format", NULL); | ||
102 | } | ||
103 | |||
104 | static int inclinenumber (void) | ||
105 | { | ||
106 | static char *pragmas [] = {"debug", "nodebug", "endif", "ifnil", "if", NULL}; | ||
107 | int ifnil = 0; | ||
87 | ++lua_linenumber; | 108 | ++lua_linenumber; |
88 | if (pragma_allowed && current == '$') { /* is a pragma? */ | 109 | if (current == '$') { /* is a pragma? */ |
89 | char buff[MINBUFF+1]; | 110 | char buff[MINBUFF+1]; |
90 | int i = 0; | ||
91 | next(); /* skip $ */ | 111 | next(); /* skip $ */ |
92 | while (isalnum((unsigned char)current)) { | 112 | readname(buff); |
93 | if (i >= MINBUFF) luaI_syntaxerror("pragma too long"); | 113 | switch (luaI_findstring(buff, pragmas)) { |
94 | buff[i++] = current; | 114 | case 0: /* debug */ |
95 | next(); | 115 | lua_debug = 1; |
116 | break; | ||
117 | case 1: /* nodebug */ | ||
118 | lua_debug = 0; | ||
119 | break; | ||
120 | case 2: /* endif */ | ||
121 | if (--iflevel < 0) | ||
122 | luaI_auxsyntaxerror("too many $endif's", NULL); | ||
123 | break; | ||
124 | case 3: /* ifnil */ | ||
125 | ifnil = 1; | ||
126 | /* go through */ | ||
127 | case 4: { /* if */ | ||
128 | int thisiflevel = iflevel++; | ||
129 | readname(buff); | ||
130 | if ((ifnil && luaI_globaldefined(buff)) || | ||
131 | (!ifnil && !luaI_globaldefined(buff))) { /* skip the $if? */ | ||
132 | do { | ||
133 | if (current == '\n') { | ||
134 | next(); | ||
135 | inclinenumber(); | ||
136 | } | ||
137 | else if (current == 0) | ||
138 | luaI_auxsyntaxerror("input ends inside a $if", NULL); | ||
139 | else next(); | ||
140 | } while (iflevel > thisiflevel); | ||
141 | } | ||
142 | break; | ||
143 | } | ||
144 | default: luaI_auxsyntaxerror("invalid pragma", buff); | ||
96 | } | 145 | } |
97 | buff[i] = 0; | ||
98 | if (strcmp(buff, "debug") == 0) | ||
99 | lua_debug = 1; | ||
100 | else if (strcmp(buff, "nodebug") == 0) | ||
101 | lua_debug = 0; | ||
102 | else luaI_auxsyntaxerror("invalid pragma", buff); | ||
103 | } | 146 | } |
104 | return lua_linenumber; | 147 | return lua_linenumber; |
105 | } | 148 | } |
@@ -136,7 +179,7 @@ static int read_long_string (char *yytext, int buffsize) | |||
136 | continue; | 179 | continue; |
137 | case '\n': | 180 | case '\n': |
138 | save_and_next(); | 181 | save_and_next(); |
139 | inclinenumber(0); | 182 | inclinenumber(); |
140 | continue; | 183 | continue; |
141 | default: | 184 | default: |
142 | save_and_next(); | 185 | save_and_next(); |
@@ -167,7 +210,7 @@ int luaY_lex (void) | |||
167 | { | 210 | { |
168 | case '\n': | 211 | case '\n': |
169 | next(); | 212 | next(); |
170 | linelasttoken = inclinenumber(1); | 213 | linelasttoken = inclinenumber(); |
171 | continue; | 214 | continue; |
172 | 215 | ||
173 | case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */ | 216 | case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */ |
@@ -231,7 +274,7 @@ int luaY_lex (void) | |||
231 | case 'n': save('\n'); next(); break; | 274 | case 'n': save('\n'); next(); break; |
232 | case 't': save('\t'); next(); break; | 275 | case 't': save('\t'); next(); break; |
233 | case 'r': save('\r'); next(); break; | 276 | case 'r': save('\r'); next(); break; |
234 | case '\n': save_and_next(); inclinenumber(0); break; | 277 | case '\n': save_and_next(); inclinenumber(); break; |
235 | default : save_and_next(); break; | 278 | default : save_and_next(); break; |
236 | } | 279 | } |
237 | break; | 280 | break; |
@@ -339,6 +382,8 @@ int luaY_lex (void) | |||
339 | 382 | ||
340 | case 0: | 383 | case 0: |
341 | save(0); | 384 | save(0); |
385 | if (iflevel > 0) | ||
386 | luaI_syntaxerror("missing $endif"); | ||
342 | return 0; | 387 | return 0; |
343 | 388 | ||
344 | default: | 389 | default: |
@@ -3,7 +3,7 @@ | |||
3 | ** Module to control static tables | 3 | ** Module to control static tables |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_table="$Id: table.c,v 2.66 1997/04/04 15:35:37 roberto Exp roberto $"; | 6 | char *rcs_table="$Id: table.c,v 2.67 1997/04/06 14:08:08 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include "luamem.h" | 8 | #include "luamem.h" |
9 | #include "auxlib.h" | 9 | #include "auxlib.h" |
@@ -111,6 +111,12 @@ TaggedString *luaI_createfixedstring (char *name) | |||
111 | } | 111 | } |
112 | 112 | ||
113 | 113 | ||
114 | int luaI_globaldefined (char *name) | ||
115 | { | ||
116 | return ttype(&lua_table[luaI_findsymbolbyname(name)].object) != LUA_T_NIL; | ||
117 | } | ||
118 | |||
119 | |||
114 | /* | 120 | /* |
115 | ** Traverse symbol table objects | 121 | ** Traverse symbol table objects |
116 | */ | 122 | */ |
@@ -1,7 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | ** Module to control static tables | 2 | ** Module to control static tables |
3 | ** TeCGraf - PUC-Rio | 3 | ** TeCGraf - PUC-Rio |
4 | ** $Id: table.h,v 2.22 1997/02/26 17:38:41 roberto Unstable roberto $ | 4 | ** $Id: table.h,v 2.23 1997/03/31 14:02:58 roberto Exp roberto $ |
5 | */ | 5 | */ |
6 | 6 | ||
7 | #ifndef table_h | 7 | #ifndef table_h |
@@ -28,6 +28,7 @@ Word luaI_findsymbolbyname (char *name); | |||
28 | Word luaI_findsymbol (TaggedString *t); | 28 | Word luaI_findsymbol (TaggedString *t); |
29 | Word luaI_findconstant (TaggedString *t); | 29 | Word luaI_findconstant (TaggedString *t); |
30 | Word luaI_findconstantbyname (char *name); | 30 | Word luaI_findconstantbyname (char *name); |
31 | int luaI_globaldefined (char *name); | ||
31 | void luaI_nextvar (void); | 32 | void luaI_nextvar (void); |
32 | TaggedString *luaI_createfixedstring (char *str); | 33 | TaggedString *luaI_createfixedstring (char *str); |
33 | int lua_markobject (TObject *o); | 34 | int lua_markobject (TObject *o); |