diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-08-30 17:54:02 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-08-30 17:54:02 -0300 |
commit | 33d820d41d5fefdda90ccd77889a683290ab6c99 (patch) | |
tree | c46acb5a9d9c1d0caeb9080c5455f0ff5a8d7e5e /lua.c | |
parent | c3d72096c4841ebc04a0615917d71f22aaa5e5ff (diff) | |
download | lua-33d820d41d5fefdda90ccd77889a683290ab6c99.tar.gz lua-33d820d41d5fefdda90ccd77889a683290ab6c99.tar.bz2 lua-33d820d41d5fefdda90ccd77889a683290ab6c99.zip |
new syntax "= exp" to rpint exp + simplifications
Diffstat (limited to 'lua.c')
-rw-r--r-- | lua.c | 88 |
1 files changed, 50 insertions, 38 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.67 2001/06/06 18:00:19 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.68 2001/06/11 14:57:17 roberto Exp $ |
3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -24,6 +24,10 @@ static int isatty (int x) { return x==0; } /* assume stdin is a tty */ | |||
24 | #endif | 24 | #endif |
25 | 25 | ||
26 | 26 | ||
27 | #ifndef LUA_PROGNAME | ||
28 | #define LUA_PROGNAME l_s("lua: ") | ||
29 | #endif | ||
30 | |||
27 | 31 | ||
28 | #ifndef PROMPT | 32 | #ifndef PROMPT |
29 | #define PROMPT l_s("> ") | 33 | #define PROMPT l_s("> ") |
@@ -36,15 +40,6 @@ static int isatty (int x) { return x==0; } /* assume stdin is a tty */ | |||
36 | 40 | ||
37 | 41 | ||
38 | 42 | ||
39 | /* | ||
40 | ** global options | ||
41 | */ | ||
42 | struct Options { | ||
43 | int toclose; | ||
44 | int stacksize; | ||
45 | }; | ||
46 | |||
47 | |||
48 | static lua_State *L = NULL; | 43 | static lua_State *L = NULL; |
49 | 44 | ||
50 | 45 | ||
@@ -80,19 +75,21 @@ static void laction (int i) { | |||
80 | } | 75 | } |
81 | 76 | ||
82 | 77 | ||
83 | static int ldo (int (*f)(lua_State *l, const l_char *), const l_char *name) { | 78 | static int ldo (int (*f)(lua_State *l, const l_char *), const l_char *name, |
79 | int clear) { | ||
84 | int res; | 80 | int res; |
85 | handler h = lreset(); | 81 | handler h = lreset(); |
86 | int top = lua_gettop(L); | 82 | int top = lua_gettop(L); |
87 | res = f(L, name); /* dostring | dofile */ | 83 | res = f(L, name); /* dostring | dofile */ |
88 | lua_settop(L, top); /* remove eventual results */ | 84 | if (clear) |
85 | lua_settop(L, top); /* remove eventual results */ | ||
89 | signal(SIGINT, h); /* restore old action */ | 86 | signal(SIGINT, h); /* restore old action */ |
90 | /* Lua gives no message in such cases, so lua.c provides one */ | 87 | /* Lua gives no message in such cases, so lua.c provides one */ |
91 | if (res == LUA_ERRMEM) { | 88 | if (res == LUA_ERRMEM) { |
92 | fprintf(stderr, l_s("lua: memory allocation error\n")); | 89 | fprintf(stderr, LUA_PROGNAME l_s("memory allocation error\n")); |
93 | } | 90 | } |
94 | else if (res == LUA_ERRERR) | 91 | else if (res == LUA_ERRERR) |
95 | fprintf(stderr, l_s("lua: error in error message\n")); | 92 | fprintf(stderr, LUA_PROGNAME l_s("error in error message\n")); |
96 | return res; | 93 | return res; |
97 | } | 94 | } |
98 | 95 | ||
@@ -151,10 +148,10 @@ static int l_getargs (lua_State *l) { | |||
151 | 148 | ||
152 | 149 | ||
153 | static int file_input (const l_char *argv) { | 150 | static int file_input (const l_char *argv) { |
154 | int result = ldo(lua_dofile, argv); | 151 | int result = ldo(lua_dofile, argv, 1); |
155 | if (result) { | 152 | if (result) { |
156 | if (result == LUA_ERRFILE) { | 153 | if (result == LUA_ERRFILE) { |
157 | fprintf(stderr, l_s("lua: cannot execute file ")); | 154 | fprintf(stderr, LUA_PROGNAME l_s("cannot execute file ")); |
158 | perror(argv); | 155 | perror(argv); |
159 | } | 156 | } |
160 | return EXIT_FAILURE; | 157 | return EXIT_FAILURE; |
@@ -187,6 +184,8 @@ static const l_char *get_prompt (int prompt) { | |||
187 | static void manual_input (int version, int prompt) { | 184 | static void manual_input (int version, int prompt) { |
188 | if (version) print_version(); | 185 | if (version) print_version(); |
189 | for (;;) { | 186 | for (;;) { |
187 | int firstline = 1; | ||
188 | int toprint = 0; | ||
190 | fputs(get_prompt(prompt), stdout); /* show prompt */ | 189 | fputs(get_prompt(prompt), stdout); /* show prompt */ |
191 | for(;;) { | 190 | for(;;) { |
192 | l_char buffer[MAXINPUT]; | 191 | l_char buffer[MAXINPUT]; |
@@ -195,6 +194,11 @@ static void manual_input (int version, int prompt) { | |||
195 | printf(l_s("\n")); | 194 | printf(l_s("\n")); |
196 | return; | 195 | return; |
197 | } | 196 | } |
197 | if (firstline && buffer[0] == l_c('=')) { | ||
198 | buffer[0] = l_c(' '); | ||
199 | lua_pushstring(L, l_s("return ")); | ||
200 | toprint = 1; | ||
201 | } | ||
198 | l = strlen(buffer); | 202 | l = strlen(buffer); |
199 | if (buffer[l-1] == l_c('\n') && buffer[l-2] == l_c('\\')) { | 203 | if (buffer[l-1] == l_c('\n') && buffer[l-2] == l_c('\\')) { |
200 | buffer[l-2] = l_c('\n'); | 204 | buffer[l-2] = l_c('\n'); |
@@ -204,22 +208,29 @@ static void manual_input (int version, int prompt) { | |||
204 | lua_pushlstring(L, buffer, l); | 208 | lua_pushlstring(L, buffer, l); |
205 | break; | 209 | break; |
206 | } | 210 | } |
211 | firstline = 0; | ||
207 | } | 212 | } |
208 | lua_concat(L, lua_gettop(L)); | 213 | lua_concat(L, lua_gettop(L)); |
209 | ldo(lua_dostring, lua_tostring(L, -1)); | 214 | ldo(lua_dostring, lua_tostring(L, 1), 0); |
210 | lua_settop(L, 0); /* remove eventual results */ | 215 | if (toprint) { |
216 | lua_remove(L, 1); /* remove ran string */ | ||
217 | lua_getglobal(L, l_s("print")); | ||
218 | lua_insert(L, 1); | ||
219 | lua_call(L, lua_gettop(L)-1, 0); | ||
220 | } | ||
221 | else | ||
222 | lua_settop(L, 0); /* remove eventual results */ | ||
211 | } | 223 | } |
212 | } | 224 | } |
213 | 225 | ||
214 | 226 | ||
215 | static int handle_argv (l_char *argv[], struct Options *opt) { | 227 | static int handle_argv (l_char *argv[], int *toclose) { |
216 | if (opt->stacksize > 0) argv++; /* skip option `-s' (if present) */ | ||
217 | if (*argv == NULL) { /* no more arguments? */ | 228 | if (*argv == NULL) { /* no more arguments? */ |
218 | if (isatty(0)) { | 229 | if (isatty(0)) { |
219 | manual_input(1, 1); | 230 | manual_input(1, 1); |
220 | } | 231 | } |
221 | else | 232 | else |
222 | ldo(lua_dofile, NULL); /* executes stdin as a file */ | 233 | ldo(lua_dofile, NULL, 1); /* executes stdin as a file */ |
223 | } | 234 | } |
224 | else { /* other arguments; loop over them */ | 235 | else { /* other arguments; loop over them */ |
225 | int i; | 236 | int i; |
@@ -233,7 +244,7 @@ static int handle_argv (l_char *argv[], struct Options *opt) { | |||
233 | } | 244 | } |
234 | else switch (argv[i][1]) { /* option */ | 245 | else switch (argv[i][1]) { /* option */ |
235 | case 0: { | 246 | case 0: { |
236 | ldo(lua_dofile, NULL); /* executes stdin as a file */ | 247 | ldo(lua_dofile, NULL, 1); /* executes stdin as a file */ |
237 | break; | 248 | break; |
238 | } | 249 | } |
239 | case l_c('i'): { | 250 | case l_c('i'): { |
@@ -245,7 +256,7 @@ static int handle_argv (l_char *argv[], struct Options *opt) { | |||
245 | break; | 256 | break; |
246 | } | 257 | } |
247 | case l_c('c'): { | 258 | case l_c('c'): { |
248 | opt->toclose = 1; | 259 | *toclose = 1; |
249 | break; | 260 | break; |
250 | } | 261 | } |
251 | case l_c('v'): { | 262 | case l_c('v'): { |
@@ -258,8 +269,9 @@ static int handle_argv (l_char *argv[], struct Options *opt) { | |||
258 | print_message(); | 269 | print_message(); |
259 | return EXIT_FAILURE; | 270 | return EXIT_FAILURE; |
260 | } | 271 | } |
261 | if (ldo(lua_dostring, argv[i]) != 0) { | 272 | if (ldo(lua_dostring, argv[i], 1) != 0) { |
262 | fprintf(stderr, l_s("lua: error running argument `%.99s'\n"), argv[i]); | 273 | fprintf(stderr, LUA_PROGNAME l_s("error running argument `%.99s'\n"), |
274 | argv[i]); | ||
263 | return EXIT_FAILURE; | 275 | return EXIT_FAILURE; |
264 | } | 276 | } |
265 | break; | 277 | break; |
@@ -275,7 +287,9 @@ static int handle_argv (l_char *argv[], struct Options *opt) { | |||
275 | return file_input(argv[i]); /* stop scanning arguments */ | 287 | return file_input(argv[i]); /* stop scanning arguments */ |
276 | } | 288 | } |
277 | case l_c('s'): { | 289 | case l_c('s'): { |
278 | fprintf(stderr, l_s("lua: stack size (`-s') must be the first option\n")); | 290 | if (i == 0) break; /* option already handled */ |
291 | fprintf(stderr, | ||
292 | LUA_PROGNAME l_s("stack size (`-s') must be the first option\n")); | ||
279 | return EXIT_FAILURE; | 293 | return EXIT_FAILURE; |
280 | } | 294 | } |
281 | default: { | 295 | default: { |
@@ -289,17 +303,17 @@ static int handle_argv (l_char *argv[], struct Options *opt) { | |||
289 | } | 303 | } |
290 | 304 | ||
291 | 305 | ||
292 | static void getstacksize (int argc, l_char *argv[], struct Options *opt) { | 306 | static int getstacksize (int argc, l_char *argv[]) { |
307 | int stacksize = 0; | ||
293 | if (argc >= 2 && argv[1][0] == l_c('-') && argv[1][1] == l_c('s')) { | 308 | if (argc >= 2 && argv[1][0] == l_c('-') && argv[1][1] == l_c('s')) { |
294 | int stacksize = strtol(&argv[1][2], NULL, 10); | 309 | stacksize = strtol(&argv[1][2], NULL, 10); |
295 | if (stacksize <= 0) { | 310 | if (stacksize <= 0) { |
296 | fprintf(stderr, l_s("lua: invalid stack size ('%.20s')\n"), &argv[1][2]); | 311 | fprintf(stderr, LUA_PROGNAME l_s("invalid stack size ('%.20s')\n"), |
312 | &argv[1][2]); | ||
297 | exit(EXIT_FAILURE); | 313 | exit(EXIT_FAILURE); |
298 | } | 314 | } |
299 | opt->stacksize = stacksize; | ||
300 | } | 315 | } |
301 | else | 316 | return stacksize; |
302 | opt->stacksize = 0; /* no stack size */ | ||
303 | } | 317 | } |
304 | 318 | ||
305 | 319 | ||
@@ -320,15 +334,13 @@ static void openstdlibs (lua_State *l) { | |||
320 | 334 | ||
321 | 335 | ||
322 | int main (int argc, l_char *argv[]) { | 336 | int main (int argc, l_char *argv[]) { |
323 | struct Options opt; | ||
324 | int status; | 337 | int status; |
325 | opt.toclose = 0; | 338 | int toclose = 0; |
326 | getstacksize(argc, argv, &opt); /* handle option `-s' */ | 339 | L = lua_open(getstacksize(argc, argv)); /* create state */ |
327 | L = lua_open(opt.stacksize); /* create state */ | ||
328 | LUA_USERINIT(L); /* open libraries */ | 340 | LUA_USERINIT(L); /* open libraries */ |
329 | register_getargs(argv); /* create `getargs' function */ | 341 | register_getargs(argv); /* create `getargs' function */ |
330 | status = handle_argv(argv+1, &opt); | 342 | status = handle_argv(argv+1, &toclose); |
331 | if (opt.toclose) | 343 | if (toclose) |
332 | lua_close(L); | 344 | lua_close(L); |
333 | return status; | 345 | return status; |
334 | } | 346 | } |