diff options
-rw-r--r-- | lua.c | 97 |
1 files changed, 17 insertions, 80 deletions
@@ -1,92 +1,29 @@ | |||
1 | /* | 1 | /* |
2 | ** lua.c | 2 | ** $Id: $ |
3 | ** Linguagem para Usuarios de Aplicacao | 3 | ** Lua stand-alone interpreter |
4 | ** See Copyright Notice in lua.h | ||
4 | */ | 5 | */ |
5 | 6 | ||
6 | char *rcs_lua="$Id: lua.c,v 1.18 1997/06/19 18:55:40 roberto Exp roberto $"; | ||
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <string.h> | 9 | #include <string.h> |
10 | 10 | ||
11 | #include "lualoc.h" | ||
12 | #include "lua.h" | 11 | #include "lua.h" |
13 | #include "auxlib.h" | 12 | #include "luadebug.h" |
14 | #include "lualib.h" | 13 | #include "lualib.h" |
15 | 14 | ||
16 | 15 | ||
17 | #ifdef _POSIX_SOURCE | 16 | #ifndef OLD_ANSI |
18 | #include <unistd.h> | 17 | #include <locale.h> |
19 | #else | 18 | #else |
20 | #define isatty(x) (x==0) /* assume stdin is a tty */ | 19 | #define setlocale(a,b) 0 |
21 | #endif | 20 | #endif |
22 | 21 | ||
23 | 22 | #ifdef _POSIX_SOURCE | |
24 | #define DEBUG 0 | 23 | #include <unistd.h> |
25 | |||
26 | static void testC (void) | ||
27 | { | ||
28 | #if DEBUG | ||
29 | #define getnum(s) ((*s++) - '0') | ||
30 | #define getname(s) (nome[0] = *s++, nome) | ||
31 | |||
32 | static int locks[10]; | ||
33 | lua_Object reg[10]; | ||
34 | char nome[2]; | ||
35 | char *s = luaL_check_string(1); | ||
36 | nome[1] = 0; | ||
37 | while (1) { | ||
38 | switch (*s++) { | ||
39 | case '0': case '1': case '2': case '3': case '4': | ||
40 | case '5': case '6': case '7': case '8': case '9': | ||
41 | lua_pushnumber(*(s-1) - '0'); | ||
42 | break; | ||
43 | |||
44 | case 'c': reg[getnum(s)] = lua_createtable(); break; | ||
45 | |||
46 | case 'P': reg[getnum(s)] = lua_pop(); break; | ||
47 | |||
48 | case 'g': { int n = getnum(s); reg[n] = lua_getglobal(getname(s)); break; } | ||
49 | |||
50 | case 'G': { int n = getnum(s); | ||
51 | reg[n] = lua_rawgetglobal(getname(s)); | ||
52 | break; | ||
53 | } | ||
54 | |||
55 | case 'l': locks[getnum(s)] = lua_ref(1); break; | ||
56 | case 'L': locks[getnum(s)] = lua_ref(0); break; | ||
57 | |||
58 | case 'r': { int n = getnum(s); reg[n] = lua_getref(locks[getnum(s)]); break; } | ||
59 | |||
60 | case 'u': lua_unref(locks[getnum(s)]); break; | ||
61 | |||
62 | case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; } | ||
63 | |||
64 | case '=': lua_setglobal(getname(s)); break; | ||
65 | |||
66 | case 's': lua_pushstring(getname(s)); break; | ||
67 | |||
68 | case 'o': lua_pushobject(reg[getnum(s)]); break; | ||
69 | |||
70 | case 'f': lua_call(getname(s)); break; | ||
71 | |||
72 | case 'i': reg[getnum(s)] = lua_gettable(); break; | ||
73 | |||
74 | case 'I': reg[getnum(s)] = lua_rawgettable(); break; | ||
75 | |||
76 | case 't': lua_settable(); break; | ||
77 | |||
78 | case 'T': lua_rawsettable(); break; | ||
79 | |||
80 | default: luaL_verror("unknown command in `testC': %c", *(s-1)); | ||
81 | |||
82 | } | ||
83 | if (*s == 0) return; | ||
84 | if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'"); | ||
85 | } | ||
86 | #else | 24 | #else |
87 | lua_error("`testC' not active"); | 25 | #define isatty(x) (x==0) /* assume stdin is a tty */ |
88 | #endif | 26 | #endif |
89 | } | ||
90 | 27 | ||
91 | 28 | ||
92 | static void manual_input (void) | 29 | static void manual_input (void) |
@@ -107,17 +44,17 @@ static void manual_input (void) | |||
107 | int main (int argc, char *argv[]) | 44 | int main (int argc, char *argv[]) |
108 | { | 45 | { |
109 | int i; | 46 | int i; |
110 | int result = 0; | ||
111 | setlocale(LC_ALL, ""); | 47 | setlocale(LC_ALL, ""); |
112 | iolib_open (); | 48 | lua_iolibopen (); |
113 | strlib_open (); | 49 | lua_strlibopen (); |
114 | mathlib_open (); | 50 | lua_mathlibopen (); |
115 | lua_register("testC", testC); | ||
116 | if (argc < 2) | 51 | if (argc < 2) |
117 | manual_input(); | 52 | manual_input(); |
118 | else for (i=1; i<argc; i++) { | 53 | else for (i=1; i<argc; i++) { |
119 | if (strcmp(argv[i], "-") == 0) | 54 | if (strcmp(argv[i], "-") == 0) |
120 | manual_input(); | 55 | manual_input(); |
56 | else if (strcmp(argv[i], "-d") == 0) | ||
57 | lua_debug = 1; | ||
121 | else if (strcmp(argv[i], "-v") == 0) | 58 | else if (strcmp(argv[i], "-v") == 0) |
122 | printf("%s %s\n(written by %s)\n\n", | 59 | printf("%s %s\n(written by %s)\n\n", |
123 | LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS); | 60 | LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS); |
@@ -128,7 +65,7 @@ int main (int argc, char *argv[]) | |||
128 | } | 65 | } |
129 | } | 66 | } |
130 | else { | 67 | else { |
131 | result = lua_dofile (argv[i]); | 68 | int result = lua_dofile (argv[i]); |
132 | if (result) { | 69 | if (result) { |
133 | if (result == 2) { | 70 | if (result == 2) { |
134 | fprintf(stderr, "lua: cannot execute file "); | 71 | fprintf(stderr, "lua: cannot execute file "); |
@@ -138,6 +75,6 @@ int main (int argc, char *argv[]) | |||
138 | } | 75 | } |
139 | } | 76 | } |
140 | } | 77 | } |
141 | return result; | 78 | return 0; |
142 | } | 79 | } |
143 | 80 | ||