diff options
author | Li Jin <dragon-fly@qq.com> | 2021-04-16 17:25:39 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2021-04-16 17:25:39 +0800 |
commit | ce9f6632635222b38ef0b37b4b1273da4a1877b4 (patch) | |
tree | 04d4d3ea81749989015434333d3db1add7b0195a | |
parent | 3355b7c475bc4a3bc8c4f2e8a2f504bb3ccb9395 (diff) | |
download | yuescript-ce9f6632635222b38ef0b37b4b1273da4a1877b4.tar.gz yuescript-ce9f6632635222b38ef0b37b4b1273da4a1877b4.tar.bz2 yuescript-ce9f6632635222b38ef0b37b4b1273da4a1877b4.zip |
fix small issues, add web assembly support.
-rw-r--r-- | .gitignore | 5 | ||||
-rw-r--r-- | makefile | 15 | ||||
-rw-r--r-- | src/lua/ltable.c | 61 | ||||
-rw-r--r-- | src/lua/lua.c | 659 | ||||
-rw-r--r-- | src/lua/lua.hpp | 9 | ||||
-rw-r--r-- | src/lua/luac.c | 724 | ||||
-rw-r--r-- | src/lua/luaconf.h | 6 | ||||
-rw-r--r-- | src/lua/makefile | 222 | ||||
-rw-r--r-- | src/yue_wasm.cpp | 160 | ||||
-rw-r--r-- | src/yuescript/ast.hpp | 2 | ||||
-rw-r--r-- | src/yuescript/yue_ast.h | 5 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 16 | ||||
-rw-r--r-- | src/yuescript/yue_parser.cpp | 11 | ||||
-rw-r--r-- | src/yuescript/yue_parser.h | 1 |
14 files changed, 1764 insertions, 132 deletions
@@ -12,3 +12,8 @@ tup/*.lua | |||
12 | tup/t/*.lua | 12 | tup/t/*.lua |
13 | src/lua/*.o | 13 | src/lua/*.o |
14 | src/lua/*.a | 14 | src/lua/*.a |
15 | src/lua/*.wasm | ||
16 | src/lua/lua | ||
17 | src/lua/luac | ||
18 | doc/docs/.vuepress/public/js/yuescript.js | ||
19 | doc/docs/.vuepress/public/js/yuescript.wasm | ||
@@ -99,6 +99,7 @@ debug: export BIN_PATH := bin/debug | |||
99 | shared: export BUILD_PATH := build/shared | 99 | shared: export BUILD_PATH := build/shared |
100 | shared: export BIN_PATH := bin/shared | 100 | shared: export BIN_PATH := bin/shared |
101 | install: export BIN_PATH := bin/release | 101 | install: export BIN_PATH := bin/release |
102 | wasm: export BIN_PATH := bin/release | ||
102 | 103 | ||
103 | # Find all source files in the source directory, sorted by most | 104 | # Find all source files in the source directory, sorted by most |
104 | # recently modified | 105 | # recently modified |
@@ -116,8 +117,10 @@ ifeq ($(SOURCES),) | |||
116 | SOURCES := $(call rwildcard, $(SRC_PATH), *.$(SRC_EXT)) | 117 | SOURCES := $(call rwildcard, $(SRC_PATH), *.$(SRC_EXT)) |
117 | endif | 118 | endif |
118 | 119 | ||
120 | SOURCES := $(filter-out ./src/yue_wasm.cpp, $(SOURCES)) | ||
121 | |||
119 | ifeq ($(NO_LUA),true) | 122 | ifeq ($(NO_LUA),true) |
120 | SOURCES := $(filter-out ./src/yuescript/yue.cpp, $(SOURCES)) | 123 | SOURCES := $(filter-out ./src/yuescript/yuescript.cpp, $(SOURCES)) |
121 | endif | 124 | endif |
122 | 125 | ||
123 | # Set the object file names, with the source directory stripped | 126 | # Set the object file names, with the source directory stripped |
@@ -176,13 +179,19 @@ else | |||
176 | @echo "Beginning release build" | 179 | @echo "Beginning release build" |
177 | endif | 180 | endif |
178 | ifneq ($(NO_LUA),true) | 181 | ifneq ($(NO_LUA),true) |
179 | @$(MAKE) -C ./src/lua | 182 | @$(MAKE) generic -C ./src/lua |
180 | endif | 183 | endif |
181 | @$(START_TIME) | 184 | @$(START_TIME) |
182 | @$(MAKE) all --no-print-directory | 185 | @$(MAKE) all --no-print-directory |
183 | @echo -n "Total build time: " | 186 | @echo -n "Total build time: " |
184 | @$(END_TIME) | 187 | @$(END_TIME) |
185 | 188 | ||
189 | .PHONY: wasm | ||
190 | wasm: | ||
191 | @$(MAKE) generic CC='emcc -s WASM=1' AR='emar rcu' RANLIB='emranlib' -C ./src/lua | ||
192 | @mkdir -p doc/docs/.vuepress/public/js | ||
193 | @emcc src/yue_wasm.cpp src/yuescript/ast.cpp src/yuescript/parser.cpp src/yuescript/yue_compiler.cpp src/yuescript/yue_parser.cpp src/yuescript/yuescript.cpp src/lua/liblua.a -s WASM=1 -O2 -o doc/docs/.vuepress/public/js/yuescript.js -Isrc -Isrc/lua -std=c++17 --bind -fexceptions | ||
194 | |||
186 | # Debug build for gdb debugging | 195 | # Debug build for gdb debugging |
187 | .PHONY: debug | 196 | .PHONY: debug |
188 | debug: dirs | 197 | debug: dirs |
@@ -192,7 +201,7 @@ else | |||
192 | @echo "Beginning debug build" | 201 | @echo "Beginning debug build" |
193 | endif | 202 | endif |
194 | ifneq ($(NO_LUA),true) | 203 | ifneq ($(NO_LUA),true) |
195 | @$(MAKE) -C ./src/lua | 204 | @$(MAKE) generic -C ./src/lua |
196 | endif | 205 | endif |
197 | @$(START_TIME) | 206 | @$(START_TIME) |
198 | @$(MAKE) all --no-print-directory | 207 | @$(MAKE) all --no-print-directory |
diff --git a/src/lua/ltable.c b/src/lua/ltable.c index b520cdf..33c1ab3 100644 --- a/src/lua/ltable.c +++ b/src/lua/ltable.c | |||
@@ -68,20 +68,25 @@ | |||
68 | #define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) | 68 | #define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) |
69 | 69 | ||
70 | 70 | ||
71 | /* | ||
72 | ** When the original hash value is good, hashing by a power of 2 | ||
73 | ** avoids the cost of '%'. | ||
74 | */ | ||
71 | #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) | 75 | #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) |
72 | 76 | ||
73 | #define hashstr(t,str) hashpow2(t, (str)->hash) | ||
74 | #define hashboolean(t,p) hashpow2(t, p) | ||
75 | #define hashint(t,i) hashpow2(t, i) | ||
76 | |||
77 | |||
78 | /* | 77 | /* |
79 | ** for some types, it is better to avoid modulus by power of 2, as | 78 | ** for other types, it is better to avoid modulo by power of 2, as |
80 | ** they tend to have many 2 factors. | 79 | ** they can have many 2 factors. |
81 | */ | 80 | */ |
82 | #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) | 81 | #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) |
83 | 82 | ||
84 | 83 | ||
84 | #define hashstr(t,str) hashpow2(t, (str)->hash) | ||
85 | #define hashboolean(t,p) hashpow2(t, p) | ||
86 | |||
87 | #define hashint(t,i) hashpow2(t, i) | ||
88 | |||
89 | |||
85 | #define hashpointer(t,p) hashmod(t, point2uint(p)) | 90 | #define hashpointer(t,p) hashmod(t, point2uint(p)) |
86 | 91 | ||
87 | 92 | ||
@@ -135,24 +140,38 @@ static int l_hashfloat (lua_Number n) { | |||
135 | */ | 140 | */ |
136 | static Node *mainposition (const Table *t, int ktt, const Value *kvl) { | 141 | static Node *mainposition (const Table *t, int ktt, const Value *kvl) { |
137 | switch (withvariant(ktt)) { | 142 | switch (withvariant(ktt)) { |
138 | case LUA_VNUMINT: | 143 | case LUA_VNUMINT: { |
139 | return hashint(t, ivalueraw(*kvl)); | 144 | lua_Integer key = ivalueraw(*kvl); |
140 | case LUA_VNUMFLT: | 145 | return hashint(t, key); |
141 | return hashmod(t, l_hashfloat(fltvalueraw(*kvl))); | 146 | } |
142 | case LUA_VSHRSTR: | 147 | case LUA_VNUMFLT: { |
143 | return hashstr(t, tsvalueraw(*kvl)); | 148 | lua_Number n = fltvalueraw(*kvl); |
144 | case LUA_VLNGSTR: | 149 | return hashmod(t, l_hashfloat(n)); |
145 | return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl))); | 150 | } |
151 | case LUA_VSHRSTR: { | ||
152 | TString *ts = tsvalueraw(*kvl); | ||
153 | return hashstr(t, ts); | ||
154 | } | ||
155 | case LUA_VLNGSTR: { | ||
156 | TString *ts = tsvalueraw(*kvl); | ||
157 | return hashpow2(t, luaS_hashlongstr(ts)); | ||
158 | } | ||
146 | case LUA_VFALSE: | 159 | case LUA_VFALSE: |
147 | return hashboolean(t, 0); | 160 | return hashboolean(t, 0); |
148 | case LUA_VTRUE: | 161 | case LUA_VTRUE: |
149 | return hashboolean(t, 1); | 162 | return hashboolean(t, 1); |
150 | case LUA_VLIGHTUSERDATA: | 163 | case LUA_VLIGHTUSERDATA: { |
151 | return hashpointer(t, pvalueraw(*kvl)); | 164 | void *p = pvalueraw(*kvl); |
152 | case LUA_VLCF: | 165 | return hashpointer(t, p); |
153 | return hashpointer(t, fvalueraw(*kvl)); | 166 | } |
154 | default: | 167 | case LUA_VLCF: { |
155 | return hashpointer(t, gcvalueraw(*kvl)); | 168 | lua_CFunction f = fvalueraw(*kvl); |
169 | return hashpointer(t, f); | ||
170 | } | ||
171 | default: { | ||
172 | GCObject *o = gcvalueraw(*kvl); | ||
173 | return hashpointer(t, o); | ||
174 | } | ||
156 | } | 175 | } |
157 | } | 176 | } |
158 | 177 | ||
diff --git a/src/lua/lua.c b/src/lua/lua.c new file mode 100644 index 0000000..46b48db --- /dev/null +++ b/src/lua/lua.c | |||
@@ -0,0 +1,659 @@ | |||
1 | /* | ||
2 | ** $Id: lua.c $ | ||
3 | ** Lua stand-alone interpreter | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | #define lua_c | ||
8 | |||
9 | #include "lprefix.h" | ||
10 | |||
11 | |||
12 | #include <stdio.h> | ||
13 | #include <stdlib.h> | ||
14 | #include <string.h> | ||
15 | |||
16 | #include <signal.h> | ||
17 | |||
18 | #include "lua.h" | ||
19 | |||
20 | #include "lauxlib.h" | ||
21 | #include "lualib.h" | ||
22 | |||
23 | |||
24 | #if !defined(LUA_PROGNAME) | ||
25 | #define LUA_PROGNAME "lua" | ||
26 | #endif | ||
27 | |||
28 | #if !defined(LUA_INIT_VAR) | ||
29 | #define LUA_INIT_VAR "LUA_INIT" | ||
30 | #endif | ||
31 | |||
32 | #define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX | ||
33 | |||
34 | |||
35 | static lua_State *globalL = NULL; | ||
36 | |||
37 | static const char *progname = LUA_PROGNAME; | ||
38 | |||
39 | |||
40 | #if defined(LUA_USE_POSIX) /* { */ | ||
41 | |||
42 | /* | ||
43 | ** Use 'sigaction' when available. | ||
44 | */ | ||
45 | static void setsignal (int sig, void (*handler)(int)) { | ||
46 | struct sigaction sa; | ||
47 | sa.sa_handler = handler; | ||
48 | sa.sa_flags = 0; | ||
49 | sigemptyset(&sa.sa_mask); /* do not mask any signal */ | ||
50 | sigaction(sig, &sa, NULL); | ||
51 | } | ||
52 | |||
53 | #else /* }{ */ | ||
54 | |||
55 | #define setsignal signal | ||
56 | |||
57 | #endif /* } */ | ||
58 | |||
59 | |||
60 | /* | ||
61 | ** Hook set by signal function to stop the interpreter. | ||
62 | */ | ||
63 | static void lstop (lua_State *L, lua_Debug *ar) { | ||
64 | (void)ar; /* unused arg. */ | ||
65 | lua_sethook(L, NULL, 0, 0); /* reset hook */ | ||
66 | luaL_error(L, "interrupted!"); | ||
67 | } | ||
68 | |||
69 | |||
70 | /* | ||
71 | ** Function to be called at a C signal. Because a C signal cannot | ||
72 | ** just change a Lua state (as there is no proper synchronization), | ||
73 | ** this function only sets a hook that, when called, will stop the | ||
74 | ** interpreter. | ||
75 | */ | ||
76 | static void laction (int i) { | ||
77 | int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; | ||
78 | setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ | ||
79 | lua_sethook(globalL, lstop, flag, 1); | ||
80 | } | ||
81 | |||
82 | |||
83 | static void print_usage (const char *badoption) { | ||
84 | lua_writestringerror("%s: ", progname); | ||
85 | if (badoption[1] == 'e' || badoption[1] == 'l') | ||
86 | lua_writestringerror("'%s' needs argument\n", badoption); | ||
87 | else | ||
88 | lua_writestringerror("unrecognized option '%s'\n", badoption); | ||
89 | lua_writestringerror( | ||
90 | "usage: %s [options] [script [args]]\n" | ||
91 | "Available options are:\n" | ||
92 | " -e stat execute string 'stat'\n" | ||
93 | " -i enter interactive mode after executing 'script'\n" | ||
94 | " -l name require library 'name' into global 'name'\n" | ||
95 | " -v show version information\n" | ||
96 | " -E ignore environment variables\n" | ||
97 | " -W turn warnings on\n" | ||
98 | " -- stop handling options\n" | ||
99 | " - stop handling options and execute stdin\n" | ||
100 | , | ||
101 | progname); | ||
102 | } | ||
103 | |||
104 | |||
105 | /* | ||
106 | ** Prints an error message, adding the program name in front of it | ||
107 | ** (if present) | ||
108 | */ | ||
109 | static void l_message (const char *pname, const char *msg) { | ||
110 | if (pname) lua_writestringerror("%s: ", pname); | ||
111 | lua_writestringerror("%s\n", msg); | ||
112 | } | ||
113 | |||
114 | |||
115 | /* | ||
116 | ** Check whether 'status' is not OK and, if so, prints the error | ||
117 | ** message on the top of the stack. It assumes that the error object | ||
118 | ** is a string, as it was either generated by Lua or by 'msghandler'. | ||
119 | */ | ||
120 | static int report (lua_State *L, int status) { | ||
121 | if (status != LUA_OK) { | ||
122 | const char *msg = lua_tostring(L, -1); | ||
123 | l_message(progname, msg); | ||
124 | lua_pop(L, 1); /* remove message */ | ||
125 | } | ||
126 | return status; | ||
127 | } | ||
128 | |||
129 | |||
130 | /* | ||
131 | ** Message handler used to run all chunks | ||
132 | */ | ||
133 | static int msghandler (lua_State *L) { | ||
134 | const char *msg = lua_tostring(L, 1); | ||
135 | if (msg == NULL) { /* is error object not a string? */ | ||
136 | if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */ | ||
137 | lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */ | ||
138 | return 1; /* that is the message */ | ||
139 | else | ||
140 | msg = lua_pushfstring(L, "(error object is a %s value)", | ||
141 | luaL_typename(L, 1)); | ||
142 | } | ||
143 | luaL_traceback(L, L, msg, 1); /* append a standard traceback */ | ||
144 | return 1; /* return the traceback */ | ||
145 | } | ||
146 | |||
147 | |||
148 | /* | ||
149 | ** Interface to 'lua_pcall', which sets appropriate message function | ||
150 | ** and C-signal handler. Used to run all chunks. | ||
151 | */ | ||
152 | static int docall (lua_State *L, int narg, int nres) { | ||
153 | int status; | ||
154 | int base = lua_gettop(L) - narg; /* function index */ | ||
155 | lua_pushcfunction(L, msghandler); /* push message handler */ | ||
156 | lua_insert(L, base); /* put it under function and args */ | ||
157 | globalL = L; /* to be available to 'laction' */ | ||
158 | setsignal(SIGINT, laction); /* set C-signal handler */ | ||
159 | status = lua_pcall(L, narg, nres, base); | ||
160 | setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */ | ||
161 | lua_remove(L, base); /* remove message handler from the stack */ | ||
162 | return status; | ||
163 | } | ||
164 | |||
165 | |||
166 | static void print_version (void) { | ||
167 | lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); | ||
168 | lua_writeline(); | ||
169 | } | ||
170 | |||
171 | |||
172 | /* | ||
173 | ** Create the 'arg' table, which stores all arguments from the | ||
174 | ** command line ('argv'). It should be aligned so that, at index 0, | ||
175 | ** it has 'argv[script]', which is the script name. The arguments | ||
176 | ** to the script (everything after 'script') go to positive indices; | ||
177 | ** other arguments (before the script name) go to negative indices. | ||
178 | ** If there is no script name, assume interpreter's name as base. | ||
179 | */ | ||
180 | static void createargtable (lua_State *L, char **argv, int argc, int script) { | ||
181 | int i, narg; | ||
182 | if (script == argc) script = 0; /* no script name? */ | ||
183 | narg = argc - (script + 1); /* number of positive indices */ | ||
184 | lua_createtable(L, narg, script + 1); | ||
185 | for (i = 0; i < argc; i++) { | ||
186 | lua_pushstring(L, argv[i]); | ||
187 | lua_rawseti(L, -2, i - script); | ||
188 | } | ||
189 | lua_setglobal(L, "arg"); | ||
190 | } | ||
191 | |||
192 | |||
193 | static int dochunk (lua_State *L, int status) { | ||
194 | if (status == LUA_OK) status = docall(L, 0, 0); | ||
195 | return report(L, status); | ||
196 | } | ||
197 | |||
198 | |||
199 | static int dofile (lua_State *L, const char *name) { | ||
200 | return dochunk(L, luaL_loadfile(L, name)); | ||
201 | } | ||
202 | |||
203 | |||
204 | static int dostring (lua_State *L, const char *s, const char *name) { | ||
205 | return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); | ||
206 | } | ||
207 | |||
208 | |||
209 | /* | ||
210 | ** Calls 'require(name)' and stores the result in a global variable | ||
211 | ** with the given name. | ||
212 | */ | ||
213 | static int dolibrary (lua_State *L, const char *name) { | ||
214 | int status; | ||
215 | lua_getglobal(L, "require"); | ||
216 | lua_pushstring(L, name); | ||
217 | status = docall(L, 1, 1); /* call 'require(name)' */ | ||
218 | if (status == LUA_OK) | ||
219 | lua_setglobal(L, name); /* global[name] = require return */ | ||
220 | return report(L, status); | ||
221 | } | ||
222 | |||
223 | |||
224 | /* | ||
225 | ** Push on the stack the contents of table 'arg' from 1 to #arg | ||
226 | */ | ||
227 | static int pushargs (lua_State *L) { | ||
228 | int i, n; | ||
229 | if (lua_getglobal(L, "arg") != LUA_TTABLE) | ||
230 | luaL_error(L, "'arg' is not a table"); | ||
231 | n = (int)luaL_len(L, -1); | ||
232 | luaL_checkstack(L, n + 3, "too many arguments to script"); | ||
233 | for (i = 1; i <= n; i++) | ||
234 | lua_rawgeti(L, -i, i); | ||
235 | lua_remove(L, -i); /* remove table from the stack */ | ||
236 | return n; | ||
237 | } | ||
238 | |||
239 | |||
240 | static int handle_script (lua_State *L, char **argv) { | ||
241 | int status; | ||
242 | const char *fname = argv[0]; | ||
243 | if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) | ||
244 | fname = NULL; /* stdin */ | ||
245 | status = luaL_loadfile(L, fname); | ||
246 | if (status == LUA_OK) { | ||
247 | int n = pushargs(L); /* push arguments to script */ | ||
248 | status = docall(L, n, LUA_MULTRET); | ||
249 | } | ||
250 | return report(L, status); | ||
251 | } | ||
252 | |||
253 | |||
254 | /* bits of various argument indicators in 'args' */ | ||
255 | #define has_error 1 /* bad option */ | ||
256 | #define has_i 2 /* -i */ | ||
257 | #define has_v 4 /* -v */ | ||
258 | #define has_e 8 /* -e */ | ||
259 | #define has_E 16 /* -E */ | ||
260 | |||
261 | |||
262 | /* | ||
263 | ** Traverses all arguments from 'argv', returning a mask with those | ||
264 | ** needed before running any Lua code (or an error code if it finds | ||
265 | ** any invalid argument). 'first' returns the first not-handled argument | ||
266 | ** (either the script name or a bad argument in case of error). | ||
267 | */ | ||
268 | static int collectargs (char **argv, int *first) { | ||
269 | int args = 0; | ||
270 | int i; | ||
271 | for (i = 1; argv[i] != NULL; i++) { | ||
272 | *first = i; | ||
273 | if (argv[i][0] != '-') /* not an option? */ | ||
274 | return args; /* stop handling options */ | ||
275 | switch (argv[i][1]) { /* else check option */ | ||
276 | case '-': /* '--' */ | ||
277 | if (argv[i][2] != '\0') /* extra characters after '--'? */ | ||
278 | return has_error; /* invalid option */ | ||
279 | *first = i + 1; | ||
280 | return args; | ||
281 | case '\0': /* '-' */ | ||
282 | return args; /* script "name" is '-' */ | ||
283 | case 'E': | ||
284 | if (argv[i][2] != '\0') /* extra characters? */ | ||
285 | return has_error; /* invalid option */ | ||
286 | args |= has_E; | ||
287 | break; | ||
288 | case 'W': | ||
289 | if (argv[i][2] != '\0') /* extra characters? */ | ||
290 | return has_error; /* invalid option */ | ||
291 | break; | ||
292 | case 'i': | ||
293 | args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ | ||
294 | case 'v': | ||
295 | if (argv[i][2] != '\0') /* extra characters? */ | ||
296 | return has_error; /* invalid option */ | ||
297 | args |= has_v; | ||
298 | break; | ||
299 | case 'e': | ||
300 | args |= has_e; /* FALLTHROUGH */ | ||
301 | case 'l': /* both options need an argument */ | ||
302 | if (argv[i][2] == '\0') { /* no concatenated argument? */ | ||
303 | i++; /* try next 'argv' */ | ||
304 | if (argv[i] == NULL || argv[i][0] == '-') | ||
305 | return has_error; /* no next argument or it is another option */ | ||
306 | } | ||
307 | break; | ||
308 | default: /* invalid option */ | ||
309 | return has_error; | ||
310 | } | ||
311 | } | ||
312 | *first = i; /* no script name */ | ||
313 | return args; | ||
314 | } | ||
315 | |||
316 | |||
317 | /* | ||
318 | ** Processes options 'e' and 'l', which involve running Lua code, and | ||
319 | ** 'W', which also affects the state. | ||
320 | ** Returns 0 if some code raises an error. | ||
321 | */ | ||
322 | static int runargs (lua_State *L, char **argv, int n) { | ||
323 | int i; | ||
324 | for (i = 1; i < n; i++) { | ||
325 | int option = argv[i][1]; | ||
326 | lua_assert(argv[i][0] == '-'); /* already checked */ | ||
327 | switch (option) { | ||
328 | case 'e': case 'l': { | ||
329 | int status; | ||
330 | const char *extra = argv[i] + 2; /* both options need an argument */ | ||
331 | if (*extra == '\0') extra = argv[++i]; | ||
332 | lua_assert(extra != NULL); | ||
333 | status = (option == 'e') | ||
334 | ? dostring(L, extra, "=(command line)") | ||
335 | : dolibrary(L, extra); | ||
336 | if (status != LUA_OK) return 0; | ||
337 | break; | ||
338 | } | ||
339 | case 'W': | ||
340 | lua_warning(L, "@on", 0); /* warnings on */ | ||
341 | break; | ||
342 | } | ||
343 | } | ||
344 | return 1; | ||
345 | } | ||
346 | |||
347 | |||
348 | static int handle_luainit (lua_State *L) { | ||
349 | const char *name = "=" LUA_INITVARVERSION; | ||
350 | const char *init = getenv(name + 1); | ||
351 | if (init == NULL) { | ||
352 | name = "=" LUA_INIT_VAR; | ||
353 | init = getenv(name + 1); /* try alternative name */ | ||
354 | } | ||
355 | if (init == NULL) return LUA_OK; | ||
356 | else if (init[0] == '@') | ||
357 | return dofile(L, init+1); | ||
358 | else | ||
359 | return dostring(L, init, name); | ||
360 | } | ||
361 | |||
362 | |||
363 | /* | ||
364 | ** {================================================================== | ||
365 | ** Read-Eval-Print Loop (REPL) | ||
366 | ** =================================================================== | ||
367 | */ | ||
368 | |||
369 | #if !defined(LUA_PROMPT) | ||
370 | #define LUA_PROMPT "> " | ||
371 | #define LUA_PROMPT2 ">> " | ||
372 | #endif | ||
373 | |||
374 | #if !defined(LUA_MAXINPUT) | ||
375 | #define LUA_MAXINPUT 512 | ||
376 | #endif | ||
377 | |||
378 | |||
379 | /* | ||
380 | ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that | ||
381 | ** is, whether we're running lua interactively). | ||
382 | */ | ||
383 | #if !defined(lua_stdin_is_tty) /* { */ | ||
384 | |||
385 | #if defined(LUA_USE_POSIX) /* { */ | ||
386 | |||
387 | #include <unistd.h> | ||
388 | #define lua_stdin_is_tty() isatty(0) | ||
389 | |||
390 | #elif defined(LUA_USE_WINDOWS) /* }{ */ | ||
391 | |||
392 | #include <io.h> | ||
393 | #include <windows.h> | ||
394 | |||
395 | #define lua_stdin_is_tty() _isatty(_fileno(stdin)) | ||
396 | |||
397 | #else /* }{ */ | ||
398 | |||
399 | /* ISO C definition */ | ||
400 | #define lua_stdin_is_tty() 1 /* assume stdin is a tty */ | ||
401 | |||
402 | #endif /* } */ | ||
403 | |||
404 | #endif /* } */ | ||
405 | |||
406 | |||
407 | /* | ||
408 | ** lua_readline defines how to show a prompt and then read a line from | ||
409 | ** the standard input. | ||
410 | ** lua_saveline defines how to "save" a read line in a "history". | ||
411 | ** lua_freeline defines how to free a line read by lua_readline. | ||
412 | */ | ||
413 | #if !defined(lua_readline) /* { */ | ||
414 | |||
415 | #if defined(LUA_USE_READLINE) /* { */ | ||
416 | |||
417 | #include <readline/readline.h> | ||
418 | #include <readline/history.h> | ||
419 | #define lua_initreadline(L) ((void)L, rl_readline_name="lua") | ||
420 | #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) | ||
421 | #define lua_saveline(L,line) ((void)L, add_history(line)) | ||
422 | #define lua_freeline(L,b) ((void)L, free(b)) | ||
423 | |||
424 | #else /* }{ */ | ||
425 | |||
426 | #define lua_initreadline(L) ((void)L) | ||
427 | #define lua_readline(L,b,p) \ | ||
428 | ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ | ||
429 | fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ | ||
430 | #define lua_saveline(L,line) { (void)L; (void)line; } | ||
431 | #define lua_freeline(L,b) { (void)L; (void)b; } | ||
432 | |||
433 | #endif /* } */ | ||
434 | |||
435 | #endif /* } */ | ||
436 | |||
437 | |||
438 | /* | ||
439 | ** Return the string to be used as a prompt by the interpreter. Leave | ||
440 | ** the string (or nil, if using the default value) on the stack, to keep | ||
441 | ** it anchored. | ||
442 | */ | ||
443 | static const char *get_prompt (lua_State *L, int firstline) { | ||
444 | if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL) | ||
445 | return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */ | ||
446 | else { /* apply 'tostring' over the value */ | ||
447 | const char *p = luaL_tolstring(L, -1, NULL); | ||
448 | lua_remove(L, -2); /* remove original value */ | ||
449 | return p; | ||
450 | } | ||
451 | } | ||
452 | |||
453 | /* mark in error messages for incomplete statements */ | ||
454 | #define EOFMARK "<eof>" | ||
455 | #define marklen (sizeof(EOFMARK)/sizeof(char) - 1) | ||
456 | |||
457 | |||
458 | /* | ||
459 | ** Check whether 'status' signals a syntax error and the error | ||
460 | ** message at the top of the stack ends with the above mark for | ||
461 | ** incomplete statements. | ||
462 | */ | ||
463 | static int incomplete (lua_State *L, int status) { | ||
464 | if (status == LUA_ERRSYNTAX) { | ||
465 | size_t lmsg; | ||
466 | const char *msg = lua_tolstring(L, -1, &lmsg); | ||
467 | if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) { | ||
468 | lua_pop(L, 1); | ||
469 | return 1; | ||
470 | } | ||
471 | } | ||
472 | return 0; /* else... */ | ||
473 | } | ||
474 | |||
475 | |||
476 | /* | ||
477 | ** Prompt the user, read a line, and push it into the Lua stack. | ||
478 | */ | ||
479 | static int pushline (lua_State *L, int firstline) { | ||
480 | char buffer[LUA_MAXINPUT]; | ||
481 | char *b = buffer; | ||
482 | size_t l; | ||
483 | const char *prmt = get_prompt(L, firstline); | ||
484 | int readstatus = lua_readline(L, b, prmt); | ||
485 | if (readstatus == 0) | ||
486 | return 0; /* no input (prompt will be popped by caller) */ | ||
487 | lua_pop(L, 1); /* remove prompt */ | ||
488 | l = strlen(b); | ||
489 | if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ | ||
490 | b[--l] = '\0'; /* remove it */ | ||
491 | if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ | ||
492 | lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ | ||
493 | else | ||
494 | lua_pushlstring(L, b, l); | ||
495 | lua_freeline(L, b); | ||
496 | return 1; | ||
497 | } | ||
498 | |||
499 | |||
500 | /* | ||
501 | ** Try to compile line on the stack as 'return <line>;'; on return, stack | ||
502 | ** has either compiled chunk or original line (if compilation failed). | ||
503 | */ | ||
504 | static int addreturn (lua_State *L) { | ||
505 | const char *line = lua_tostring(L, -1); /* original line */ | ||
506 | const char *retline = lua_pushfstring(L, "return %s;", line); | ||
507 | int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); | ||
508 | if (status == LUA_OK) { | ||
509 | lua_remove(L, -2); /* remove modified line */ | ||
510 | if (line[0] != '\0') /* non empty? */ | ||
511 | lua_saveline(L, line); /* keep history */ | ||
512 | } | ||
513 | else | ||
514 | lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ | ||
515 | return status; | ||
516 | } | ||
517 | |||
518 | |||
519 | /* | ||
520 | ** Read multiple lines until a complete Lua statement | ||
521 | */ | ||
522 | static int multiline (lua_State *L) { | ||
523 | for (;;) { /* repeat until gets a complete statement */ | ||
524 | size_t len; | ||
525 | const char *line = lua_tolstring(L, 1, &len); /* get what it has */ | ||
526 | int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ | ||
527 | if (!incomplete(L, status) || !pushline(L, 0)) { | ||
528 | lua_saveline(L, line); /* keep history */ | ||
529 | return status; /* cannot or should not try to add continuation line */ | ||
530 | } | ||
531 | lua_pushliteral(L, "\n"); /* add newline... */ | ||
532 | lua_insert(L, -2); /* ...between the two lines */ | ||
533 | lua_concat(L, 3); /* join them */ | ||
534 | } | ||
535 | } | ||
536 | |||
537 | |||
538 | /* | ||
539 | ** Read a line and try to load (compile) it first as an expression (by | ||
540 | ** adding "return " in front of it) and second as a statement. Return | ||
541 | ** the final status of load/call with the resulting function (if any) | ||
542 | ** in the top of the stack. | ||
543 | */ | ||
544 | static int loadline (lua_State *L) { | ||
545 | int status; | ||
546 | lua_settop(L, 0); | ||
547 | if (!pushline(L, 1)) | ||
548 | return -1; /* no input */ | ||
549 | if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ | ||
550 | status = multiline(L); /* try as command, maybe with continuation lines */ | ||
551 | lua_remove(L, 1); /* remove line from the stack */ | ||
552 | lua_assert(lua_gettop(L) == 1); | ||
553 | return status; | ||
554 | } | ||
555 | |||
556 | |||
557 | /* | ||
558 | ** Prints (calling the Lua 'print' function) any values on the stack | ||
559 | */ | ||
560 | static void l_print (lua_State *L) { | ||
561 | int n = lua_gettop(L); | ||
562 | if (n > 0) { /* any result to be printed? */ | ||
563 | luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); | ||
564 | lua_getglobal(L, "print"); | ||
565 | lua_insert(L, 1); | ||
566 | if (lua_pcall(L, n, 0, 0) != LUA_OK) | ||
567 | l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)", | ||
568 | lua_tostring(L, -1))); | ||
569 | } | ||
570 | } | ||
571 | |||
572 | |||
573 | /* | ||
574 | ** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and | ||
575 | ** print any results. | ||
576 | */ | ||
577 | static void doREPL (lua_State *L) { | ||
578 | int status; | ||
579 | const char *oldprogname = progname; | ||
580 | progname = NULL; /* no 'progname' on errors in interactive mode */ | ||
581 | lua_initreadline(L); | ||
582 | while ((status = loadline(L)) != -1) { | ||
583 | if (status == LUA_OK) | ||
584 | status = docall(L, 0, LUA_MULTRET); | ||
585 | if (status == LUA_OK) l_print(L); | ||
586 | else report(L, status); | ||
587 | } | ||
588 | lua_settop(L, 0); /* clear stack */ | ||
589 | lua_writeline(); | ||
590 | progname = oldprogname; | ||
591 | } | ||
592 | |||
593 | /* }================================================================== */ | ||
594 | |||
595 | |||
596 | /* | ||
597 | ** Main body of stand-alone interpreter (to be called in protected mode). | ||
598 | ** Reads the options and handles them all. | ||
599 | */ | ||
600 | static int pmain (lua_State *L) { | ||
601 | int argc = (int)lua_tointeger(L, 1); | ||
602 | char **argv = (char **)lua_touserdata(L, 2); | ||
603 | int script; | ||
604 | int args = collectargs(argv, &script); | ||
605 | luaL_checkversion(L); /* check that interpreter has correct version */ | ||
606 | if (argv[0] && argv[0][0]) progname = argv[0]; | ||
607 | if (args == has_error) { /* bad arg? */ | ||
608 | print_usage(argv[script]); /* 'script' has index of bad arg. */ | ||
609 | return 0; | ||
610 | } | ||
611 | if (args & has_v) /* option '-v'? */ | ||
612 | print_version(); | ||
613 | if (args & has_E) { /* option '-E'? */ | ||
614 | lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ | ||
615 | lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); | ||
616 | } | ||
617 | luaL_openlibs(L); /* open standard libraries */ | ||
618 | createargtable(L, argv, argc, script); /* create table 'arg' */ | ||
619 | lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */ | ||
620 | if (!(args & has_E)) { /* no option '-E'? */ | ||
621 | if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ | ||
622 | return 0; /* error running LUA_INIT */ | ||
623 | } | ||
624 | if (!runargs(L, argv, script)) /* execute arguments -e and -l */ | ||
625 | return 0; /* something failed */ | ||
626 | if (script < argc && /* execute main script (if there is one) */ | ||
627 | handle_script(L, argv + script) != LUA_OK) | ||
628 | return 0; | ||
629 | if (args & has_i) /* -i option? */ | ||
630 | doREPL(L); /* do read-eval-print loop */ | ||
631 | else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ | ||
632 | if (lua_stdin_is_tty()) { /* running in interactive mode? */ | ||
633 | print_version(); | ||
634 | doREPL(L); /* do read-eval-print loop */ | ||
635 | } | ||
636 | else dofile(L, NULL); /* executes stdin as a file */ | ||
637 | } | ||
638 | lua_pushboolean(L, 1); /* signal no errors */ | ||
639 | return 1; | ||
640 | } | ||
641 | |||
642 | |||
643 | int main (int argc, char **argv) { | ||
644 | int status, result; | ||
645 | lua_State *L = luaL_newstate(); /* create state */ | ||
646 | if (L == NULL) { | ||
647 | l_message(argv[0], "cannot create state: not enough memory"); | ||
648 | return EXIT_FAILURE; | ||
649 | } | ||
650 | lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ | ||
651 | lua_pushinteger(L, argc); /* 1st argument */ | ||
652 | lua_pushlightuserdata(L, argv); /* 2nd argument */ | ||
653 | status = lua_pcall(L, 2, 1, 0); /* do the call */ | ||
654 | result = lua_toboolean(L, -1); /* get result */ | ||
655 | report(L, status); | ||
656 | lua_close(L); | ||
657 | return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; | ||
658 | } | ||
659 | |||
diff --git a/src/lua/lua.hpp b/src/lua/lua.hpp new file mode 100644 index 0000000..ec417f5 --- /dev/null +++ b/src/lua/lua.hpp | |||
@@ -0,0 +1,9 @@ | |||
1 | // lua.hpp | ||
2 | // Lua header files for C++ | ||
3 | // <<extern "C">> not supplied automatically because Lua also compiles as C++ | ||
4 | |||
5 | extern "C" { | ||
6 | #include "lua.h" | ||
7 | #include "lualib.h" | ||
8 | #include "lauxlib.h" | ||
9 | } | ||
diff --git a/src/lua/luac.c b/src/lua/luac.c new file mode 100644 index 0000000..56ddc41 --- /dev/null +++ b/src/lua/luac.c | |||
@@ -0,0 +1,724 @@ | |||
1 | /* | ||
2 | ** $Id: luac.c $ | ||
3 | ** Lua compiler (saves bytecodes to files; also lists bytecodes) | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | #define luac_c | ||
8 | #define LUA_CORE | ||
9 | |||
10 | #include "lprefix.h" | ||
11 | |||
12 | #include <ctype.h> | ||
13 | #include <errno.h> | ||
14 | #include <stdio.h> | ||
15 | #include <stdlib.h> | ||
16 | #include <string.h> | ||
17 | |||
18 | #include "lua.h" | ||
19 | #include "lauxlib.h" | ||
20 | |||
21 | #include "ldebug.h" | ||
22 | #include "lobject.h" | ||
23 | #include "lopcodes.h" | ||
24 | #include "lopnames.h" | ||
25 | #include "lstate.h" | ||
26 | #include "lundump.h" | ||
27 | |||
28 | static void PrintFunction(const Proto* f, int full); | ||
29 | #define luaU_print PrintFunction | ||
30 | |||
31 | #define PROGNAME "luac" /* default program name */ | ||
32 | #define OUTPUT PROGNAME ".out" /* default output file */ | ||
33 | |||
34 | static int listing=0; /* list bytecodes? */ | ||
35 | static int dumping=1; /* dump bytecodes? */ | ||
36 | static int stripping=0; /* strip debug information? */ | ||
37 | static char Output[]={ OUTPUT }; /* default output file name */ | ||
38 | static const char* output=Output; /* actual output file name */ | ||
39 | static const char* progname=PROGNAME; /* actual program name */ | ||
40 | static TString **tmname; | ||
41 | |||
42 | static void fatal(const char* message) | ||
43 | { | ||
44 | fprintf(stderr,"%s: %s\n",progname,message); | ||
45 | exit(EXIT_FAILURE); | ||
46 | } | ||
47 | |||
48 | static void cannot(const char* what) | ||
49 | { | ||
50 | fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); | ||
51 | exit(EXIT_FAILURE); | ||
52 | } | ||
53 | |||
54 | static void usage(const char* message) | ||
55 | { | ||
56 | if (*message=='-') | ||
57 | fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message); | ||
58 | else | ||
59 | fprintf(stderr,"%s: %s\n",progname,message); | ||
60 | fprintf(stderr, | ||
61 | "usage: %s [options] [filenames]\n" | ||
62 | "Available options are:\n" | ||
63 | " -l list (use -l -l for full listing)\n" | ||
64 | " -o name output to file 'name' (default is \"%s\")\n" | ||
65 | " -p parse only\n" | ||
66 | " -s strip debug information\n" | ||
67 | " -v show version information\n" | ||
68 | " -- stop handling options\n" | ||
69 | " - stop handling options and process stdin\n" | ||
70 | ,progname,Output); | ||
71 | exit(EXIT_FAILURE); | ||
72 | } | ||
73 | |||
74 | #define IS(s) (strcmp(argv[i],s)==0) | ||
75 | |||
76 | static int doargs(int argc, char* argv[]) | ||
77 | { | ||
78 | int i; | ||
79 | int version=0; | ||
80 | if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; | ||
81 | for (i=1; i<argc; i++) | ||
82 | { | ||
83 | if (*argv[i]!='-') /* end of options; keep it */ | ||
84 | break; | ||
85 | else if (IS("--")) /* end of options; skip it */ | ||
86 | { | ||
87 | ++i; | ||
88 | if (version) ++version; | ||
89 | break; | ||
90 | } | ||
91 | else if (IS("-")) /* end of options; use stdin */ | ||
92 | break; | ||
93 | else if (IS("-l")) /* list */ | ||
94 | ++listing; | ||
95 | else if (IS("-o")) /* output file */ | ||
96 | { | ||
97 | output=argv[++i]; | ||
98 | if (output==NULL || *output==0 || (*output=='-' && output[1]!=0)) | ||
99 | usage("'-o' needs argument"); | ||
100 | if (IS("-")) output=NULL; | ||
101 | } | ||
102 | else if (IS("-p")) /* parse only */ | ||
103 | dumping=0; | ||
104 | else if (IS("-s")) /* strip debug information */ | ||
105 | stripping=1; | ||
106 | else if (IS("-v")) /* show version */ | ||
107 | ++version; | ||
108 | else /* unknown option */ | ||
109 | usage(argv[i]); | ||
110 | } | ||
111 | if (i==argc && (listing || !dumping)) | ||
112 | { | ||
113 | dumping=0; | ||
114 | argv[--i]=Output; | ||
115 | } | ||
116 | if (version) | ||
117 | { | ||
118 | printf("%s\n",LUA_COPYRIGHT); | ||
119 | if (version==argc-1) exit(EXIT_SUCCESS); | ||
120 | } | ||
121 | return i; | ||
122 | } | ||
123 | |||
124 | #define FUNCTION "(function()end)();" | ||
125 | |||
126 | static const char* reader(lua_State* L, void* ud, size_t* size) | ||
127 | { | ||
128 | UNUSED(L); | ||
129 | if ((*(int*)ud)--) | ||
130 | { | ||
131 | *size=sizeof(FUNCTION)-1; | ||
132 | return FUNCTION; | ||
133 | } | ||
134 | else | ||
135 | { | ||
136 | *size=0; | ||
137 | return NULL; | ||
138 | } | ||
139 | } | ||
140 | |||
141 | #define toproto(L,i) getproto(s2v(L->top+(i))) | ||
142 | |||
143 | static const Proto* combine(lua_State* L, int n) | ||
144 | { | ||
145 | if (n==1) | ||
146 | return toproto(L,-1); | ||
147 | else | ||
148 | { | ||
149 | Proto* f; | ||
150 | int i=n; | ||
151 | if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1)); | ||
152 | f=toproto(L,-1); | ||
153 | for (i=0; i<n; i++) | ||
154 | { | ||
155 | f->p[i]=toproto(L,i-n-1); | ||
156 | if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; | ||
157 | } | ||
158 | f->sizelineinfo=0; | ||
159 | return f; | ||
160 | } | ||
161 | } | ||
162 | |||
163 | static int writer(lua_State* L, const void* p, size_t size, void* u) | ||
164 | { | ||
165 | UNUSED(L); | ||
166 | return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); | ||
167 | } | ||
168 | |||
169 | static int pmain(lua_State* L) | ||
170 | { | ||
171 | int argc=(int)lua_tointeger(L,1); | ||
172 | char** argv=(char**)lua_touserdata(L,2); | ||
173 | const Proto* f; | ||
174 | int i; | ||
175 | tmname=G(L)->tmname; | ||
176 | if (!lua_checkstack(L,argc)) fatal("too many input files"); | ||
177 | for (i=0; i<argc; i++) | ||
178 | { | ||
179 | const char* filename=IS("-") ? NULL : argv[i]; | ||
180 | if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1)); | ||
181 | } | ||
182 | f=combine(L,argc); | ||
183 | if (listing) luaU_print(f,listing>1); | ||
184 | if (dumping) | ||
185 | { | ||
186 | FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); | ||
187 | if (D==NULL) cannot("open"); | ||
188 | lua_lock(L); | ||
189 | luaU_dump(L,f,writer,D,stripping); | ||
190 | lua_unlock(L); | ||
191 | if (ferror(D)) cannot("write"); | ||
192 | if (fclose(D)) cannot("close"); | ||
193 | } | ||
194 | return 0; | ||
195 | } | ||
196 | |||
197 | int main(int argc, char* argv[]) | ||
198 | { | ||
199 | lua_State* L; | ||
200 | int i=doargs(argc,argv); | ||
201 | argc-=i; argv+=i; | ||
202 | if (argc<=0) usage("no input files given"); | ||
203 | L=luaL_newstate(); | ||
204 | if (L==NULL) fatal("cannot create state: not enough memory"); | ||
205 | lua_pushcfunction(L,&pmain); | ||
206 | lua_pushinteger(L,argc); | ||
207 | lua_pushlightuserdata(L,argv); | ||
208 | if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); | ||
209 | lua_close(L); | ||
210 | return EXIT_SUCCESS; | ||
211 | } | ||
212 | |||
213 | /* | ||
214 | ** print bytecodes | ||
215 | */ | ||
216 | |||
217 | #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") | ||
218 | #define VOID(p) ((const void*)(p)) | ||
219 | #define eventname(i) (getstr(tmname[i])) | ||
220 | |||
221 | static void PrintString(const TString* ts) | ||
222 | { | ||
223 | const char* s=getstr(ts); | ||
224 | size_t i,n=tsslen(ts); | ||
225 | printf("\""); | ||
226 | for (i=0; i<n; i++) | ||
227 | { | ||
228 | int c=(int)(unsigned char)s[i]; | ||
229 | switch (c) | ||
230 | { | ||
231 | case '"': | ||
232 | printf("\\\""); | ||
233 | break; | ||
234 | case '\\': | ||
235 | printf("\\\\"); | ||
236 | break; | ||
237 | case '\a': | ||
238 | printf("\\a"); | ||
239 | break; | ||
240 | case '\b': | ||
241 | printf("\\b"); | ||
242 | break; | ||
243 | case '\f': | ||
244 | printf("\\f"); | ||
245 | break; | ||
246 | case '\n': | ||
247 | printf("\\n"); | ||
248 | break; | ||
249 | case '\r': | ||
250 | printf("\\r"); | ||
251 | break; | ||
252 | case '\t': | ||
253 | printf("\\t"); | ||
254 | break; | ||
255 | case '\v': | ||
256 | printf("\\v"); | ||
257 | break; | ||
258 | default: | ||
259 | if (isprint(c)) printf("%c",c); else printf("\\%03d",c); | ||
260 | break; | ||
261 | } | ||
262 | } | ||
263 | printf("\""); | ||
264 | } | ||
265 | |||
266 | static void PrintType(const Proto* f, int i) | ||
267 | { | ||
268 | const TValue* o=&f->k[i]; | ||
269 | switch (ttypetag(o)) | ||
270 | { | ||
271 | case LUA_VNIL: | ||
272 | printf("N"); | ||
273 | break; | ||
274 | case LUA_VFALSE: | ||
275 | case LUA_VTRUE: | ||
276 | printf("B"); | ||
277 | break; | ||
278 | case LUA_VNUMFLT: | ||
279 | printf("F"); | ||
280 | break; | ||
281 | case LUA_VNUMINT: | ||
282 | printf("I"); | ||
283 | break; | ||
284 | case LUA_VSHRSTR: | ||
285 | case LUA_VLNGSTR: | ||
286 | printf("S"); | ||
287 | break; | ||
288 | default: /* cannot happen */ | ||
289 | printf("?%d",ttypetag(o)); | ||
290 | break; | ||
291 | } | ||
292 | printf("\t"); | ||
293 | } | ||
294 | |||
295 | static void PrintConstant(const Proto* f, int i) | ||
296 | { | ||
297 | const TValue* o=&f->k[i]; | ||
298 | switch (ttypetag(o)) | ||
299 | { | ||
300 | case LUA_VNIL: | ||
301 | printf("nil"); | ||
302 | break; | ||
303 | case LUA_VFALSE: | ||
304 | printf("false"); | ||
305 | break; | ||
306 | case LUA_VTRUE: | ||
307 | printf("true"); | ||
308 | break; | ||
309 | case LUA_VNUMFLT: | ||
310 | { | ||
311 | char buff[100]; | ||
312 | sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); | ||
313 | printf("%s",buff); | ||
314 | if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); | ||
315 | break; | ||
316 | } | ||
317 | case LUA_VNUMINT: | ||
318 | printf(LUA_INTEGER_FMT,ivalue(o)); | ||
319 | break; | ||
320 | case LUA_VSHRSTR: | ||
321 | case LUA_VLNGSTR: | ||
322 | PrintString(tsvalue(o)); | ||
323 | break; | ||
324 | default: /* cannot happen */ | ||
325 | printf("?%d",ttypetag(o)); | ||
326 | break; | ||
327 | } | ||
328 | } | ||
329 | |||
330 | #define COMMENT "\t; " | ||
331 | #define EXTRAARG GETARG_Ax(code[pc+1]) | ||
332 | #define EXTRAARGC (EXTRAARG*(MAXARG_C+1)) | ||
333 | #define ISK (isk ? "k" : "") | ||
334 | |||
335 | static void PrintCode(const Proto* f) | ||
336 | { | ||
337 | const Instruction* code=f->code; | ||
338 | int pc,n=f->sizecode; | ||
339 | for (pc=0; pc<n; pc++) | ||
340 | { | ||
341 | Instruction i=code[pc]; | ||
342 | OpCode o=GET_OPCODE(i); | ||
343 | int a=GETARG_A(i); | ||
344 | int b=GETARG_B(i); | ||
345 | int c=GETARG_C(i); | ||
346 | int ax=GETARG_Ax(i); | ||
347 | int bx=GETARG_Bx(i); | ||
348 | int sb=GETARG_sB(i); | ||
349 | int sc=GETARG_sC(i); | ||
350 | int sbx=GETARG_sBx(i); | ||
351 | int isk=GETARG_k(i); | ||
352 | int line=luaG_getfuncline(f,pc); | ||
353 | printf("\t%d\t",pc+1); | ||
354 | if (line>0) printf("[%d]\t",line); else printf("[-]\t"); | ||
355 | printf("%-9s\t",opnames[o]); | ||
356 | switch (o) | ||
357 | { | ||
358 | case OP_MOVE: | ||
359 | printf("%d %d",a,b); | ||
360 | break; | ||
361 | case OP_LOADI: | ||
362 | printf("%d %d",a,sbx); | ||
363 | break; | ||
364 | case OP_LOADF: | ||
365 | printf("%d %d",a,sbx); | ||
366 | break; | ||
367 | case OP_LOADK: | ||
368 | printf("%d %d",a,bx); | ||
369 | printf(COMMENT); PrintConstant(f,bx); | ||
370 | break; | ||
371 | case OP_LOADKX: | ||
372 | printf("%d",a); | ||
373 | printf(COMMENT); PrintConstant(f,EXTRAARG); | ||
374 | break; | ||
375 | case OP_LOADFALSE: | ||
376 | printf("%d",a); | ||
377 | break; | ||
378 | case OP_LFALSESKIP: | ||
379 | printf("%d",a); | ||
380 | break; | ||
381 | case OP_LOADTRUE: | ||
382 | printf("%d",a); | ||
383 | break; | ||
384 | case OP_LOADNIL: | ||
385 | printf("%d %d",a,b); | ||
386 | printf(COMMENT "%d out",b+1); | ||
387 | break; | ||
388 | case OP_GETUPVAL: | ||
389 | printf("%d %d",a,b); | ||
390 | printf(COMMENT "%s",UPVALNAME(b)); | ||
391 | break; | ||
392 | case OP_SETUPVAL: | ||
393 | printf("%d %d",a,b); | ||
394 | printf(COMMENT "%s",UPVALNAME(b)); | ||
395 | break; | ||
396 | case OP_GETTABUP: | ||
397 | printf("%d %d %d",a,b,c); | ||
398 | printf(COMMENT "%s",UPVALNAME(b)); | ||
399 | printf(" "); PrintConstant(f,c); | ||
400 | break; | ||
401 | case OP_GETTABLE: | ||
402 | printf("%d %d %d",a,b,c); | ||
403 | break; | ||
404 | case OP_GETI: | ||
405 | printf("%d %d %d",a,b,c); | ||
406 | break; | ||
407 | case OP_GETFIELD: | ||
408 | printf("%d %d %d",a,b,c); | ||
409 | printf(COMMENT); PrintConstant(f,c); | ||
410 | break; | ||
411 | case OP_SETTABUP: | ||
412 | printf("%d %d %d%s",a,b,c,ISK); | ||
413 | printf(COMMENT "%s",UPVALNAME(a)); | ||
414 | printf(" "); PrintConstant(f,b); | ||
415 | if (isk) { printf(" "); PrintConstant(f,c); } | ||
416 | break; | ||
417 | case OP_SETTABLE: | ||
418 | printf("%d %d %d%s",a,b,c,ISK); | ||
419 | if (isk) { printf(COMMENT); PrintConstant(f,c); } | ||
420 | break; | ||
421 | case OP_SETI: | ||
422 | printf("%d %d %d%s",a,b,c,ISK); | ||
423 | if (isk) { printf(COMMENT); PrintConstant(f,c); } | ||
424 | break; | ||
425 | case OP_SETFIELD: | ||
426 | printf("%d %d %d%s",a,b,c,ISK); | ||
427 | printf(COMMENT); PrintConstant(f,b); | ||
428 | if (isk) { printf(" "); PrintConstant(f,c); } | ||
429 | break; | ||
430 | case OP_NEWTABLE: | ||
431 | printf("%d %d %d",a,b,c); | ||
432 | printf(COMMENT "%d",c+EXTRAARGC); | ||
433 | break; | ||
434 | case OP_SELF: | ||
435 | printf("%d %d %d%s",a,b,c,ISK); | ||
436 | if (isk) { printf(COMMENT); PrintConstant(f,c); } | ||
437 | break; | ||
438 | case OP_ADDI: | ||
439 | printf("%d %d %d",a,b,sc); | ||
440 | break; | ||
441 | case OP_ADDK: | ||
442 | printf("%d %d %d",a,b,c); | ||
443 | printf(COMMENT); PrintConstant(f,c); | ||
444 | break; | ||
445 | case OP_SUBK: | ||
446 | printf("%d %d %d",a,b,c); | ||
447 | printf(COMMENT); PrintConstant(f,c); | ||
448 | break; | ||
449 | case OP_MULK: | ||
450 | printf("%d %d %d",a,b,c); | ||
451 | printf(COMMENT); PrintConstant(f,c); | ||
452 | break; | ||
453 | case OP_MODK: | ||
454 | printf("%d %d %d",a,b,c); | ||
455 | printf(COMMENT); PrintConstant(f,c); | ||
456 | break; | ||
457 | case OP_POWK: | ||
458 | printf("%d %d %d",a,b,c); | ||
459 | printf(COMMENT); PrintConstant(f,c); | ||
460 | break; | ||
461 | case OP_DIVK: | ||
462 | printf("%d %d %d",a,b,c); | ||
463 | printf(COMMENT); PrintConstant(f,c); | ||
464 | break; | ||
465 | case OP_IDIVK: | ||
466 | printf("%d %d %d",a,b,c); | ||
467 | printf(COMMENT); PrintConstant(f,c); | ||
468 | break; | ||
469 | case OP_BANDK: | ||
470 | printf("%d %d %d",a,b,c); | ||
471 | printf(COMMENT); PrintConstant(f,c); | ||
472 | break; | ||
473 | case OP_BORK: | ||
474 | printf("%d %d %d",a,b,c); | ||
475 | printf(COMMENT); PrintConstant(f,c); | ||
476 | break; | ||
477 | case OP_BXORK: | ||
478 | printf("%d %d %d",a,b,c); | ||
479 | printf(COMMENT); PrintConstant(f,c); | ||
480 | break; | ||
481 | case OP_SHRI: | ||
482 | printf("%d %d %d",a,b,sc); | ||
483 | break; | ||
484 | case OP_SHLI: | ||
485 | printf("%d %d %d",a,b,sc); | ||
486 | break; | ||
487 | case OP_ADD: | ||
488 | printf("%d %d %d",a,b,c); | ||
489 | break; | ||
490 | case OP_SUB: | ||
491 | printf("%d %d %d",a,b,c); | ||
492 | break; | ||
493 | case OP_MUL: | ||
494 | printf("%d %d %d",a,b,c); | ||
495 | break; | ||
496 | case OP_MOD: | ||
497 | printf("%d %d %d",a,b,c); | ||
498 | break; | ||
499 | case OP_POW: | ||
500 | printf("%d %d %d",a,b,c); | ||
501 | break; | ||
502 | case OP_DIV: | ||
503 | printf("%d %d %d",a,b,c); | ||
504 | break; | ||
505 | case OP_IDIV: | ||
506 | printf("%d %d %d",a,b,c); | ||
507 | break; | ||
508 | case OP_BAND: | ||
509 | printf("%d %d %d",a,b,c); | ||
510 | break; | ||
511 | case OP_BOR: | ||
512 | printf("%d %d %d",a,b,c); | ||
513 | break; | ||
514 | case OP_BXOR: | ||
515 | printf("%d %d %d",a,b,c); | ||
516 | break; | ||
517 | case OP_SHL: | ||
518 | printf("%d %d %d",a,b,c); | ||
519 | break; | ||
520 | case OP_SHR: | ||
521 | printf("%d %d %d",a,b,c); | ||
522 | break; | ||
523 | case OP_MMBIN: | ||
524 | printf("%d %d %d",a,b,c); | ||
525 | printf(COMMENT "%s",eventname(c)); | ||
526 | break; | ||
527 | case OP_MMBINI: | ||
528 | printf("%d %d %d %d",a,sb,c,isk); | ||
529 | printf(COMMENT "%s",eventname(c)); | ||
530 | if (isk) printf(" flip"); | ||
531 | break; | ||
532 | case OP_MMBINK: | ||
533 | printf("%d %d %d %d",a,b,c,isk); | ||
534 | printf(COMMENT "%s ",eventname(c)); PrintConstant(f,b); | ||
535 | if (isk) printf(" flip"); | ||
536 | break; | ||
537 | case OP_UNM: | ||
538 | printf("%d %d",a,b); | ||
539 | break; | ||
540 | case OP_BNOT: | ||
541 | printf("%d %d",a,b); | ||
542 | break; | ||
543 | case OP_NOT: | ||
544 | printf("%d %d",a,b); | ||
545 | break; | ||
546 | case OP_LEN: | ||
547 | printf("%d %d",a,b); | ||
548 | break; | ||
549 | case OP_CONCAT: | ||
550 | printf("%d %d",a,b); | ||
551 | break; | ||
552 | case OP_CLOSE: | ||
553 | printf("%d",a); | ||
554 | break; | ||
555 | case OP_TBC: | ||
556 | printf("%d",a); | ||
557 | break; | ||
558 | case OP_JMP: | ||
559 | printf("%d",GETARG_sJ(i)); | ||
560 | printf(COMMENT "to %d",GETARG_sJ(i)+pc+2); | ||
561 | break; | ||
562 | case OP_EQ: | ||
563 | printf("%d %d %d",a,b,isk); | ||
564 | break; | ||
565 | case OP_LT: | ||
566 | printf("%d %d %d",a,b,isk); | ||
567 | break; | ||
568 | case OP_LE: | ||
569 | printf("%d %d %d",a,b,isk); | ||
570 | break; | ||
571 | case OP_EQK: | ||
572 | printf("%d %d %d",a,b,isk); | ||
573 | printf(COMMENT); PrintConstant(f,b); | ||
574 | break; | ||
575 | case OP_EQI: | ||
576 | printf("%d %d %d",a,sb,isk); | ||
577 | break; | ||
578 | case OP_LTI: | ||
579 | printf("%d %d %d",a,sb,isk); | ||
580 | break; | ||
581 | case OP_LEI: | ||
582 | printf("%d %d %d",a,sb,isk); | ||
583 | break; | ||
584 | case OP_GTI: | ||
585 | printf("%d %d %d",a,sb,isk); | ||
586 | break; | ||
587 | case OP_GEI: | ||
588 | printf("%d %d %d",a,sb,isk); | ||
589 | break; | ||
590 | case OP_TEST: | ||
591 | printf("%d %d",a,isk); | ||
592 | break; | ||
593 | case OP_TESTSET: | ||
594 | printf("%d %d %d",a,b,isk); | ||
595 | break; | ||
596 | case OP_CALL: | ||
597 | printf("%d %d %d",a,b,c); | ||
598 | printf(COMMENT); | ||
599 | if (b==0) printf("all in "); else printf("%d in ",b-1); | ||
600 | if (c==0) printf("all out"); else printf("%d out",c-1); | ||
601 | break; | ||
602 | case OP_TAILCALL: | ||
603 | printf("%d %d %d",a,b,c); | ||
604 | printf(COMMENT "%d in",b-1); | ||
605 | break; | ||
606 | case OP_RETURN: | ||
607 | printf("%d %d %d",a,b,c); | ||
608 | printf(COMMENT); | ||
609 | if (b==0) printf("all out"); else printf("%d out",b-1); | ||
610 | break; | ||
611 | case OP_RETURN0: | ||
612 | break; | ||
613 | case OP_RETURN1: | ||
614 | printf("%d",a); | ||
615 | break; | ||
616 | case OP_FORLOOP: | ||
617 | printf("%d %d",a,bx); | ||
618 | printf(COMMENT "to %d",pc-bx+2); | ||
619 | break; | ||
620 | case OP_FORPREP: | ||
621 | printf("%d %d",a,bx); | ||
622 | printf(COMMENT "to %d",pc+bx+2); | ||
623 | break; | ||
624 | case OP_TFORPREP: | ||
625 | printf("%d %d",a,bx); | ||
626 | printf(COMMENT "to %d",pc+bx+2); | ||
627 | break; | ||
628 | case OP_TFORCALL: | ||
629 | printf("%d %d",a,c); | ||
630 | break; | ||
631 | case OP_TFORLOOP: | ||
632 | printf("%d %d",a,bx); | ||
633 | printf(COMMENT "to %d",pc-bx+2); | ||
634 | break; | ||
635 | case OP_SETLIST: | ||
636 | printf("%d %d %d",a,b,c); | ||
637 | if (isk) printf(COMMENT "%d",c+EXTRAARGC); | ||
638 | break; | ||
639 | case OP_CLOSURE: | ||
640 | printf("%d %d",a,bx); | ||
641 | printf(COMMENT "%p",VOID(f->p[bx])); | ||
642 | break; | ||
643 | case OP_VARARG: | ||
644 | printf("%d %d",a,c); | ||
645 | printf(COMMENT); | ||
646 | if (c==0) printf("all out"); else printf("%d out",c-1); | ||
647 | break; | ||
648 | case OP_VARARGPREP: | ||
649 | printf("%d",a); | ||
650 | break; | ||
651 | case OP_EXTRAARG: | ||
652 | printf("%d",ax); | ||
653 | break; | ||
654 | #if 0 | ||
655 | default: | ||
656 | printf("%d %d %d",a,b,c); | ||
657 | printf(COMMENT "not handled"); | ||
658 | break; | ||
659 | #endif | ||
660 | } | ||
661 | printf("\n"); | ||
662 | } | ||
663 | } | ||
664 | |||
665 | |||
666 | #define SS(x) ((x==1)?"":"s") | ||
667 | #define S(x) (int)(x),SS(x) | ||
668 | |||
669 | static void PrintHeader(const Proto* f) | ||
670 | { | ||
671 | const char* s=f->source ? getstr(f->source) : "=?"; | ||
672 | if (*s=='@' || *s=='=') | ||
673 | s++; | ||
674 | else if (*s==LUA_SIGNATURE[0]) | ||
675 | s="(bstring)"; | ||
676 | else | ||
677 | s="(string)"; | ||
678 | printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", | ||
679 | (f->linedefined==0)?"main":"function",s, | ||
680 | f->linedefined,f->lastlinedefined, | ||
681 | S(f->sizecode),VOID(f)); | ||
682 | printf("%d%s param%s, %d slot%s, %d upvalue%s, ", | ||
683 | (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams), | ||
684 | S(f->maxstacksize),S(f->sizeupvalues)); | ||
685 | printf("%d local%s, %d constant%s, %d function%s\n", | ||
686 | S(f->sizelocvars),S(f->sizek),S(f->sizep)); | ||
687 | } | ||
688 | |||
689 | static void PrintDebug(const Proto* f) | ||
690 | { | ||
691 | int i,n; | ||
692 | n=f->sizek; | ||
693 | printf("constants (%d) for %p:\n",n,VOID(f)); | ||
694 | for (i=0; i<n; i++) | ||
695 | { | ||
696 | printf("\t%d\t",i); | ||
697 | PrintType(f,i); | ||
698 | PrintConstant(f,i); | ||
699 | printf("\n"); | ||
700 | } | ||
701 | n=f->sizelocvars; | ||
702 | printf("locals (%d) for %p:\n",n,VOID(f)); | ||
703 | for (i=0; i<n; i++) | ||
704 | { | ||
705 | printf("\t%d\t%s\t%d\t%d\n", | ||
706 | i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); | ||
707 | } | ||
708 | n=f->sizeupvalues; | ||
709 | printf("upvalues (%d) for %p:\n",n,VOID(f)); | ||
710 | for (i=0; i<n; i++) | ||
711 | { | ||
712 | printf("\t%d\t%s\t%d\t%d\n", | ||
713 | i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx); | ||
714 | } | ||
715 | } | ||
716 | |||
717 | static void PrintFunction(const Proto* f, int full) | ||
718 | { | ||
719 | int i,n=f->sizep; | ||
720 | PrintHeader(f); | ||
721 | PrintCode(f); | ||
722 | if (full) PrintDebug(f); | ||
723 | for (i=0; i<n; i++) PrintFunction(f->p[i],full); | ||
724 | } | ||
diff --git a/src/lua/luaconf.h b/src/lua/luaconf.h index 38e14ed..e64d2ee 100644 --- a/src/lua/luaconf.h +++ b/src/lua/luaconf.h | |||
@@ -663,11 +663,13 @@ | |||
663 | 663 | ||
664 | /* | 664 | /* |
665 | ** macros to improve jump prediction, used mostly for error handling | 665 | ** macros to improve jump prediction, used mostly for error handling |
666 | ** and debug facilities. | 666 | ** and debug facilities. (Some macros in the Lua API use these macros. |
667 | ** Define LUA_NOBUILTIN if you do not want '__builtin_expect' in your | ||
668 | ** code.) | ||
667 | */ | 669 | */ |
668 | #if !defined(luai_likely) | 670 | #if !defined(luai_likely) |
669 | 671 | ||
670 | #if defined(__GNUC__) | 672 | #if defined(__GNUC__) && !defined(LUA_NOBUILTIN) |
671 | #define luai_likely(x) (__builtin_expect(((x) != 0), 1)) | 673 | #define luai_likely(x) (__builtin_expect(((x) != 0), 1)) |
672 | #define luai_unlikely(x) (__builtin_expect(((x) != 0), 0)) | 674 | #define luai_unlikely(x) (__builtin_expect(((x) != 0), 0)) |
673 | #else | 675 | #else |
diff --git a/src/lua/makefile b/src/lua/makefile index ccd5315..f78c0b8 100644 --- a/src/lua/makefile +++ b/src/lua/makefile | |||
@@ -1,89 +1,54 @@ | |||
1 | # makefile for building Lua | 1 | # Makefile for building Lua |
2 | # see INSTALL for installation instructions | 2 | # See ../doc/readme.html for installation and customization instructions. |
3 | # see ../Makefile and luaconf.h for further customization | ||
4 | 3 | ||
5 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= | 4 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= |
6 | 5 | ||
7 | # Warnings valid for both C and C++ | 6 | # Your platform. See PLATS for possible values. |
8 | CWARNSCPP= \ | 7 | PLAT= guess |
9 | -Wextra \ | 8 | |
10 | -Wshadow \ | 9 | CC= gcc -std=gnu99 |
11 | -Wsign-compare \ | 10 | CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_3 $(SYSCFLAGS) $(MYCFLAGS) |
12 | -Wundef \ | 11 | LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) |
13 | -Wwrite-strings \ | 12 | LIBS= -lm $(SYSLIBS) $(MYLIBS) |
14 | -Wredundant-decls \ | 13 | |
15 | -Wdisabled-optimization \ | 14 | AR= ar rcu |
16 | -Wdouble-promotion \ | ||
17 | # the next warnings might be useful sometimes, | ||
18 | # but usually they generate too much noise | ||
19 | # -Werror \ | ||
20 | # -pedantic # warns if we use jump tables \ | ||
21 | # -Wconversion \ | ||
22 | # -Wsign-conversion \ | ||
23 | # -Wstrict-overflow=2 \ | ||
24 | # -Wformat=2 \ | ||
25 | # -Wcast-qual \ | ||
26 | |||
27 | # The next warnings are neither valid nor needed for C++ | ||
28 | CWARNSC= -Wdeclaration-after-statement \ | ||
29 | -Wmissing-prototypes \ | ||
30 | -Wnested-externs \ | ||
31 | -Wstrict-prototypes \ | ||
32 | -Wc++-compat \ | ||
33 | -Wold-style-definition \ | ||
34 | |||
35 | |||
36 | CWARNS= $(CWARNSCPP) $(CWARNSC) | ||
37 | |||
38 | |||
39 | # -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"' | ||
40 | # -DMAXINDEXRK=1 -DLUA_COMPAT_5_3 | ||
41 | # -g -DLUA_USER_H='"ltests.h"' | ||
42 | # -pg -malign-double | ||
43 | # -DLUA_USE_CTYPE -DLUA_USE_APICHECK | ||
44 | # ('-ftrapv' for runtime checks of integer overflows) | ||
45 | # -fsanitize=undefined -ftrapv -fno-inline | ||
46 | |||
47 | |||
48 | LOCAL = $(CWARNS) | ||
49 | |||
50 | |||
51 | # enable Linux goodies | ||
52 | MYCFLAGS= $(LOCAL) -std=c99 | ||
53 | |||
54 | UNAME_S:=$(shell uname -s) | ||
55 | ifeq ($(UNAME_S),Darwin) | ||
56 | MYCFLAGS += -DLUA_USE_MACOSX | ||
57 | MYLDFLAGS= $(LOCAL) -Wl | ||
58 | else | ||
59 | MYCFLAGS += -DLUA_USE_LINUX | ||
60 | MYLDFLAGS= $(LOCAL) -Wl,-E | ||
61 | endif | ||
62 | |||
63 | CC= gcc | ||
64 | CFLAGS= -Wall -O2 $(MYCFLAGS) -fno-stack-protector -fno-common | ||
65 | AR= ar rc | ||
66 | RANLIB= ranlib | 15 | RANLIB= ranlib |
67 | RM= rm -f | 16 | RM= rm -f |
17 | UNAME= uname | ||
18 | |||
19 | SYSCFLAGS= | ||
20 | SYSLDFLAGS= | ||
21 | SYSLIBS= | ||
22 | |||
23 | MYCFLAGS= | ||
24 | MYLDFLAGS= | ||
25 | MYLIBS= | ||
26 | MYOBJS= | ||
68 | 27 | ||
28 | # Special flags for compiler modules; -Os reduces code size. | ||
29 | CMCFLAGS= | ||
69 | 30 | ||
31 | # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= | ||
70 | 32 | ||
71 | # == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= | 33 | PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix solaris |
72 | 34 | ||
35 | LUA_A= liblua.a | ||
36 | CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o | ||
37 | LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o linit.o | ||
38 | BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) | ||
73 | 39 | ||
74 | LIBS = -lm | 40 | LUA_T= lua |
41 | LUA_O= lua.o | ||
75 | 42 | ||
76 | CORE_T= liblua.a | 43 | LUAC_T= luac |
77 | CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \ | 44 | LUAC_O= luac.o |
78 | lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \ | ||
79 | ltm.o lundump.o lvm.o lzio.o | ||
80 | AUX_O= lauxlib.o | ||
81 | LIB_O= lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \ | ||
82 | lutf8lib.o loadlib.o lcorolib.o linit.o | ||
83 | 45 | ||
84 | ALL_T= $(CORE_T) | 46 | ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) |
85 | ALL_O= $(CORE_O) $(AUX_O) $(LIB_O) | 47 | ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) |
86 | ALL_A= $(CORE_T) | 48 | ALL_A= $(LUA_A) |
49 | |||
50 | # Targets start here. | ||
51 | default: $(PLAT) | ||
87 | 52 | ||
88 | all: $(ALL_T) | 53 | all: $(ALL_T) |
89 | 54 | ||
@@ -91,40 +56,104 @@ o: $(ALL_O) | |||
91 | 56 | ||
92 | a: $(ALL_A) | 57 | a: $(ALL_A) |
93 | 58 | ||
94 | $(CORE_T): $(CORE_O) $(AUX_O) $(LIB_O) | 59 | $(LUA_A): $(BASE_O) |
95 | $(AR) $@ $? | 60 | $(AR) $@ $(BASE_O) |
96 | $(RANLIB) $@ | 61 | $(RANLIB) $@ |
97 | 62 | ||
98 | llex.o: | 63 | $(LUA_T): $(LUA_O) $(LUA_A) |
99 | $(CC) $(CFLAGS) -Os -c llex.c | 64 | $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) |
100 | 65 | ||
101 | lparser.o: | 66 | $(LUAC_T): $(LUAC_O) $(LUA_A) |
102 | $(CC) $(CFLAGS) -Os -c lparser.c | 67 | $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) |
103 | |||
104 | lcode.o: | ||
105 | $(CC) $(CFLAGS) -Os -c lcode.c | ||
106 | 68 | ||
69 | test: | ||
70 | ./$(LUA_T) -v | ||
107 | 71 | ||
108 | clean: | 72 | clean: |
109 | $(RM) $(ALL_T) $(ALL_O) | 73 | $(RM) $(ALL_T) $(ALL_O) |
110 | 74 | ||
111 | depend: | 75 | depend: |
112 | @$(CC) $(CFLAGS) -MM *.c | 76 | @$(CC) $(CFLAGS) -MM l*.c |
113 | 77 | ||
114 | echo: | 78 | echo: |
115 | @echo "CC = $(CC)" | 79 | @echo "PLAT= $(PLAT)" |
116 | @echo "CFLAGS = $(CFLAGS)" | 80 | @echo "CC= $(CC)" |
117 | @echo "AR = $(AR)" | 81 | @echo "CFLAGS= $(CFLAGS)" |
118 | @echo "RANLIB = $(RANLIB)" | 82 | @echo "LDFLAGS= $(SYSLDFLAGS)" |
119 | @echo "RM = $(RM)" | 83 | @echo "LIBS= $(LIBS)" |
120 | @echo "MYCFLAGS = $(MYCFLAGS)" | 84 | @echo "AR= $(AR)" |
121 | @echo "MYLDFLAGS = $(MYLDFLAGS)" | 85 | @echo "RANLIB= $(RANLIB)" |
122 | @echo "DL = $(DL)" | 86 | @echo "RM= $(RM)" |
87 | @echo "UNAME= $(UNAME)" | ||
88 | |||
89 | # Convenience targets for popular platforms. | ||
90 | ALL= all | ||
91 | |||
92 | help: | ||
93 | @echo "Do 'make PLATFORM' where PLATFORM is one of these:" | ||
94 | @echo " $(PLATS)" | ||
95 | @echo "See doc/readme.html for complete instructions." | ||
96 | |||
97 | guess: | ||
98 | @echo Guessing `$(UNAME)` | ||
99 | @$(MAKE) `$(UNAME)` | ||
100 | |||
101 | AIX aix: | ||
102 | $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" | ||
103 | |||
104 | bsd: | ||
105 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E" | ||
106 | |||
107 | c89: | ||
108 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_C89" CC="gcc -std=c89" | ||
109 | @echo '' | ||
110 | @echo '*** C89 does not guarantee 64-bit integers for Lua.' | ||
111 | @echo '*** Make sure to compile all external Lua libraries' | ||
112 | @echo '*** with LUA_USE_C89 to ensure consistency' | ||
113 | @echo '' | ||
123 | 114 | ||
124 | $(ALL_O): makefile | 115 | FreeBSD NetBSD OpenBSD freebsd: |
116 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE -I/usr/include/edit" SYSLIBS="-Wl,-E -ledit" CC="cc" | ||
117 | |||
118 | generic: $(ALL) | ||
119 | |||
120 | Linux linux: linux-noreadline | ||
121 | |||
122 | linux-noreadline: | ||
123 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" | ||
124 | |||
125 | linux-readline: | ||
126 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE" SYSLIBS="-Wl,-E -ldl -lreadline" | ||
127 | |||
128 | Darwin macos macosx: | ||
129 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX -DLUA_USE_READLINE" SYSLIBS="-lreadline" | ||
130 | |||
131 | mingw: | ||
132 | $(MAKE) "LUA_A=lua54.dll" "LUA_T=lua.exe" \ | ||
133 | "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ | ||
134 | "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe | ||
135 | $(MAKE) "LUAC_T=luac.exe" luac.exe | ||
136 | |||
137 | posix: | ||
138 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" | ||
139 | |||
140 | SunOS solaris: | ||
141 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl" | ||
142 | |||
143 | # Targets that do not create files (not all makes understand .PHONY). | ||
144 | .PHONY: all $(PLATS) help test clean default o a depend echo | ||
145 | |||
146 | # Compiler modules may use special flags. | ||
147 | llex.o: | ||
148 | $(CC) $(CFLAGS) $(CMCFLAGS) -c llex.c | ||
149 | |||
150 | lparser.o: | ||
151 | $(CC) $(CFLAGS) $(CMCFLAGS) -c lparser.c | ||
152 | |||
153 | lcode.o: | ||
154 | $(CC) $(CFLAGS) $(CMCFLAGS) -c lcode.c | ||
125 | 155 | ||
126 | # DO NOT EDIT | 156 | # DO NOT DELETE |
127 | # automatically made with 'gcc -MM l*.c' | ||
128 | 157 | ||
129 | lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ | 158 | lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ |
130 | lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ | 159 | lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ |
@@ -177,6 +206,9 @@ ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ | |||
177 | ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h | 206 | ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h |
178 | ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ | 207 | ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ |
179 | llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h | 208 | llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h |
209 | lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h | ||
210 | luac.o: luac.c lprefix.h lua.h luaconf.h lauxlib.h ldebug.h lstate.h \ | ||
211 | lobject.h llimits.h ltm.h lzio.h lmem.h lopcodes.h lopnames.h lundump.h | ||
180 | lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ | 212 | lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ |
181 | lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ | 213 | lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ |
182 | lundump.h | 214 | lundump.h |
diff --git a/src/yue_wasm.cpp b/src/yue_wasm.cpp new file mode 100644 index 0000000..fd8b150 --- /dev/null +++ b/src/yue_wasm.cpp | |||
@@ -0,0 +1,160 @@ | |||
1 | #include "yuescript/yue_compiler.h" | ||
2 | |||
3 | extern "C" { | ||
4 | #include "lua.h" | ||
5 | #include "lauxlib.h" | ||
6 | #include "lualib.h" | ||
7 | int luaopen_yue(lua_State* L); | ||
8 | } // extern "C" | ||
9 | |||
10 | static void openlibs(void* state) { | ||
11 | lua_State* L = static_cast<lua_State*>(state); | ||
12 | luaL_openlibs(L); | ||
13 | luaopen_yue(L); | ||
14 | } | ||
15 | |||
16 | #define YUE_ARGS nullptr,openlibs | ||
17 | |||
18 | #include <string_view> | ||
19 | using namespace std::string_view_literals; | ||
20 | #include <emscripten/bind.h> | ||
21 | using namespace emscripten; | ||
22 | |||
23 | struct YueResult | ||
24 | { | ||
25 | std::string codes; | ||
26 | std::string err; | ||
27 | }; | ||
28 | |||
29 | YueResult tolua(const std::string& codes, bool reserveLineNumber = true, bool implicitReturnRoot = true, bool useSpaceOverTab = true) { | ||
30 | yue::YueConfig config; | ||
31 | config.reserveLineNumber = reserveLineNumber; | ||
32 | config.implicitReturnRoot = implicitReturnRoot; | ||
33 | config.useSpaceOverTab = useSpaceOverTab; | ||
34 | auto result = yue::YueCompiler{YUE_ARGS}.compile(codes, config); | ||
35 | return {result.codes, result.error}; | ||
36 | } | ||
37 | |||
38 | std::string version() { return std::string(yue::version); } | ||
39 | |||
40 | #define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;}) | ||
41 | #define DEFER(code) _DEFER(code,__LINE__) | ||
42 | |||
43 | void pushYue(lua_State* L, std::string_view name) { | ||
44 | lua_getglobal(L, "package"); // package | ||
45 | lua_getfield(L, -1, "loaded"); // package loaded | ||
46 | lua_getfield(L, -1, "yue"); // package loaded yue | ||
47 | lua_pushlstring(L, &name.front(), name.size()); // package loaded yue name | ||
48 | lua_gettable(L, -2); // loaded[name], package loaded yue item | ||
49 | lua_insert(L, -4); // item package loaded yue | ||
50 | lua_pop(L, 3); // item | ||
51 | } | ||
52 | |||
53 | void pushOptions(lua_State* L, int lineOffset) { | ||
54 | lua_newtable(L); | ||
55 | lua_pushliteral(L, "lint_global"); | ||
56 | lua_pushboolean(L, 0); | ||
57 | lua_rawset(L, -3); | ||
58 | lua_pushliteral(L, "implicit_return_root"); | ||
59 | lua_pushboolean(L, 1); | ||
60 | lua_rawset(L, -3); | ||
61 | lua_pushliteral(L, "reserve_line_number"); | ||
62 | lua_pushboolean(L, 1); | ||
63 | lua_rawset(L, -3); | ||
64 | lua_pushliteral(L, "space_over_tab"); | ||
65 | lua_pushboolean(L, 0); | ||
66 | lua_rawset(L, -3); | ||
67 | lua_pushliteral(L, "same_module"); | ||
68 | lua_pushboolean(L, 1); | ||
69 | lua_rawset(L, -3); | ||
70 | lua_pushliteral(L, "line_offset"); | ||
71 | lua_pushinteger(L, lineOffset); | ||
72 | lua_rawset(L, -3); | ||
73 | } | ||
74 | |||
75 | std::string exec(const std::string& codes) { | ||
76 | lua_State* L = luaL_newstate(); | ||
77 | openlibs(L); | ||
78 | DEFER(lua_close(L)); | ||
79 | auto execStr = [&](const std::string& s) { | ||
80 | std::string result; | ||
81 | pushYue(L, "insert_loader"sv); | ||
82 | if (lua_pcall(L, 0, 0, 0) != 0) { | ||
83 | result += lua_tostring(L, -1); | ||
84 | result += '\n'; | ||
85 | return result; | ||
86 | } | ||
87 | pushYue(L, "loadstring"sv); | ||
88 | lua_pushlstring(L, s.c_str(), s.size()); | ||
89 | lua_pushliteral(L, "=(eval str)"); | ||
90 | if (lua_pcall(L, 2, 2, 0) != 0) { | ||
91 | result += lua_tostring(L, -1); | ||
92 | result += '\n'; | ||
93 | return result; | ||
94 | } | ||
95 | if (lua_isnil(L, -2) != 0) { | ||
96 | result += lua_tostring(L, -1); | ||
97 | result += '\n'; | ||
98 | return result; | ||
99 | } | ||
100 | lua_pop(L, 1); | ||
101 | pushYue(L, "pcall"sv); | ||
102 | lua_insert(L, -2); | ||
103 | int last = lua_gettop(L) - 2; | ||
104 | if (lua_pcall(L, 1, LUA_MULTRET, 0) != 0) { | ||
105 | result = lua_tostring(L, -1); | ||
106 | result += '\n'; | ||
107 | return result; | ||
108 | } | ||
109 | int cur = lua_gettop(L); | ||
110 | int retCount = cur - last; | ||
111 | bool success = lua_toboolean(L, -retCount) != 0; | ||
112 | if (success) { | ||
113 | if (retCount > 1) { | ||
114 | for (int i = 1; i < retCount; ++i) { | ||
115 | result += luaL_tolstring(L, -retCount + i, nullptr); | ||
116 | result += '\n'; | ||
117 | lua_pop(L, 1); | ||
118 | } | ||
119 | } | ||
120 | } else { | ||
121 | result += lua_tostring(L, -1); | ||
122 | result += '\n'; | ||
123 | } | ||
124 | return result; | ||
125 | }; | ||
126 | |||
127 | execStr(R"yuescript( | ||
128 | _G.__output = {} | ||
129 | _G.print = (...)-> | ||
130 | len = select "#", ... | ||
131 | strs = {} | ||
132 | for i = 1, len | ||
133 | strs[#strs + 1] = tostring select i, ... | ||
134 | _G.__output[#_G.__output + 1] = table.concat strs, "\n" | ||
135 | )yuescript"); | ||
136 | std::string res2 = execStr(codes); | ||
137 | std::string res1 = execStr(R"yuescript( | ||
138 | res = table.concat _G.__output, "\n" | ||
139 | return res if res ~= "" | ||
140 | )yuescript"); | ||
141 | if (res1.empty()) { | ||
142 | return res2; | ||
143 | } else { | ||
144 | if (res2.empty()) { | ||
145 | return res1; | ||
146 | } else { | ||
147 | return res1 + res2; | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | |||
152 | EMSCRIPTEN_BINDINGS(yue) { | ||
153 | value_array<YueResult>("YueResult") | ||
154 | .element(&YueResult::codes) | ||
155 | .element(&YueResult::err); | ||
156 | function("tolua", &tolua); | ||
157 | function("exec", &exec); | ||
158 | function("version", &version); | ||
159 | } | ||
160 | |||
diff --git a/src/yuescript/ast.hpp b/src/yuescript/ast.hpp index 162a82e..0636a74 100644 --- a/src/yuescript/ast.hpp +++ b/src/yuescript/ast.hpp | |||
@@ -238,9 +238,9 @@ public: | |||
238 | m_ptr = nullptr; | 238 | m_ptr = nullptr; |
239 | } else { | 239 | } else { |
240 | assert(accept(node)); | 240 | assert(accept(node)); |
241 | node->retain(); | ||
241 | if (m_ptr) m_ptr->release(); | 242 | if (m_ptr) m_ptr->release(); |
242 | m_ptr = node; | 243 | m_ptr = node; |
243 | node->retain(); | ||
244 | } | 244 | } |
245 | } | 245 | } |
246 | 246 | ||
diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 96968c9..4c33759 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h | |||
@@ -763,6 +763,11 @@ AST_NODE(Block) | |||
763 | AST_MEMBER(Block, &sep, &statements) | 763 | AST_MEMBER(Block, &sep, &statements) |
764 | AST_END(Block) | 764 | AST_END(Block) |
765 | 765 | ||
766 | AST_NODE(BlockEnd) | ||
767 | ast_ptr<true, Block_t> block; | ||
768 | AST_MEMBER(BlockEnd, &block) | ||
769 | AST_END(BlockEnd) | ||
770 | |||
766 | AST_NODE(File) | 771 | AST_NODE(File) |
767 | ast_ptr<true, Block_t> block; | 772 | ast_ptr<true, Block_t> block; |
768 | AST_MEMBER(File, &block) | 773 | AST_MEMBER(File, &block) |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 6627189..caa5b9e 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -59,7 +59,7 @@ inline std::string s(std::string_view sv) { | |||
59 | return std::string(sv); | 59 | return std::string(sv); |
60 | } | 60 | } |
61 | 61 | ||
62 | const std::string_view version = "0.7.5"sv; | 62 | const std::string_view version = "0.7.6"sv; |
63 | const std::string_view extension = "yue"sv; | 63 | const std::string_view extension = "yue"sv; |
64 | 64 | ||
65 | class YueCompilerImpl { | 65 | class YueCompilerImpl { |
@@ -3217,7 +3217,11 @@ private: | |||
3217 | auto x = chainList.front(); | 3217 | auto x = chainList.front(); |
3218 | auto chain = x->new_ptr<ChainValue_t>(); | 3218 | auto chain = x->new_ptr<ChainValue_t>(); |
3219 | if (opIt == chainList.begin() && ast_is<ColonChainItem_t, DotChainItem_t>(x)) { | 3219 | if (opIt == chainList.begin() && ast_is<ColonChainItem_t, DotChainItem_t>(x)) { |
3220 | chain->items.push_back(toAst<Callable_t>(_withVars.top(), x)); | 3220 | if (_withVars.empty()) { |
3221 | throw std::logic_error(_info.errorMessage("short dot/colon syntax must be called within a with block"sv, x)); | ||
3222 | } else { | ||
3223 | chain->items.push_back(toAst<Callable_t>(_withVars.top(), x)); | ||
3224 | } | ||
3221 | } | 3225 | } |
3222 | for (auto it = chainList.begin(); it != opIt; ++it) { | 3226 | for (auto it = chainList.begin(); it != opIt; ++it) { |
3223 | chain->items.push_back(*it); | 3227 | chain->items.push_back(*it); |
@@ -3632,7 +3636,7 @@ private: | |||
3632 | } else { | 3636 | } else { |
3633 | if (!codes.empty()) { | 3637 | if (!codes.empty()) { |
3634 | if (isBlock) { | 3638 | if (isBlock) { |
3635 | info = _parser.parse<Block_t>(codes); | 3639 | info = _parser.parse<BlockEnd_t>(codes); |
3636 | if (!info.node) { | 3640 | if (!info.node) { |
3637 | info.error = info.error.substr(info.error.find(':') + 2); | 3641 | info.error = info.error.substr(info.error.find(':') + 2); |
3638 | throw std::logic_error(_info.errorMessage(s("failed to expanded macro as block: "sv) + info.error, x)); | 3642 | throw std::logic_error(_info.errorMessage(s("failed to expanded macro as block: "sv) + info.error, x)); |
@@ -3640,7 +3644,7 @@ private: | |||
3640 | } else { | 3644 | } else { |
3641 | info = _parser.parse<Exp_t>(codes); | 3645 | info = _parser.parse<Exp_t>(codes); |
3642 | if (!info.node && allowBlockMacroReturn) { | 3646 | if (!info.node && allowBlockMacroReturn) { |
3643 | info = _parser.parse<Block_t>(codes); | 3647 | info = _parser.parse<BlockEnd_t>(codes); |
3644 | if (!info.node) { | 3648 | if (!info.node) { |
3645 | info.error = info.error.substr(info.error.find(':') + 2); | 3649 | info.error = info.error.substr(info.error.find(':') + 2); |
3646 | throw std::logic_error(_info.errorMessage(s("failed to expanded macro as expr or block: "sv) + info.error, x)); | 3650 | throw std::logic_error(_info.errorMessage(s("failed to expanded macro as expr or block: "sv) + info.error, x)); |
@@ -3693,7 +3697,9 @@ private: | |||
3693 | info.node.set(exp); | 3697 | info.node.set(exp); |
3694 | } | 3698 | } |
3695 | } | 3699 | } |
3696 | if (auto block = info.node.as<Block_t>()) { | 3700 | if (auto blockEnd = info.node.as<BlockEnd_t>()) { |
3701 | auto block = blockEnd->block.get(); | ||
3702 | info.node.set(block); | ||
3697 | for (auto stmt_ : block->statements.objects()) { | 3703 | for (auto stmt_ : block->statements.objects()) { |
3698 | auto stmt = static_cast<Statement_t*>(stmt_); | 3704 | auto stmt = static_cast<Statement_t*>(stmt_); |
3699 | if (auto global = stmt->content.as<Global_t>()) { | 3705 | if (auto global = stmt->content.as<Global_t>()) { |
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 2234a59..c76b574 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
@@ -495,13 +495,13 @@ YueParser::YueParser() { | |||
495 | State* st = reinterpret_cast<State*>(item.user_data); | 495 | State* st = reinterpret_cast<State*>(item.user_data); |
496 | st->exportCount++; | 496 | st->exportCount++; |
497 | return true; | 497 | return true; |
498 | }) >> ((pl::user(export_default, [](const item_t& item) { | 498 | }) >> (pl::user(export_default >> Exp, [](const item_t& item) { |
499 | State* st = reinterpret_cast<State*>(item.user_data); | 499 | State* st = reinterpret_cast<State*>(item.user_data); |
500 | bool isValid = !st->exportDefault && st->exportCount == 1; | 500 | bool isValid = !st->exportDefault && st->exportCount == 1; |
501 | st->exportDefault = true; | 501 | st->exportDefault = true; |
502 | return isValid; | 502 | return isValid; |
503 | }) >> Exp) | 503 | }) |
504 | | (pl::user(true_(), [](const item_t& item) { | 504 | | (not_(export_default) >> pl::user(true_(), [](const item_t& item) { |
505 | State* st = reinterpret_cast<State*>(item.user_data); | 505 | State* st = reinterpret_cast<State*>(item.user_data); |
506 | if (st->exportDefault && st->exportCount > 1) { | 506 | if (st->exportDefault && st->exportCount > 1) { |
507 | return false; | 507 | return false; |
@@ -611,12 +611,13 @@ YueParser::YueParser() { | |||
611 | 611 | ||
612 | Body = InBlock | Statement; | 612 | Body = InBlock | Statement; |
613 | 613 | ||
614 | empty_line_stop = Space >> and_(Stop); | 614 | empty_line_stop = Space >> and_(Break); |
615 | Line = and_(check_indent >> Space >> not_(BackcallOperator)) >> Statement | Advance >> ensure(and_(Space >> BackcallOperator) >> Statement, PopIndent) | empty_line_stop; | 615 | Line = and_(check_indent >> Space >> not_(BackcallOperator)) >> Statement | Advance >> ensure(and_(Space >> BackcallOperator) >> Statement, PopIndent) | empty_line_stop; |
616 | Block = Seperator >> Line >> *(+Break >> Line); | 616 | Block = Seperator >> Line >> *(+Break >> Line); |
617 | 617 | ||
618 | Shebang = expr("#!") >> *(not_(Stop) >> Any); | 618 | Shebang = expr("#!") >> *(not_(Stop) >> Any); |
619 | File = White >> -Shebang >> Block >> eof(); | 619 | BlockEnd = Block >> -(+Break >> Space >> and_(Stop)) >> Stop; |
620 | File = White >> -Shebang >> Block >> -(+Break >> Space >> and_(eof())) >> eof(); | ||
620 | } | 621 | } |
621 | 622 | ||
622 | ParseInfo YueParser::parse(std::string_view codes, rule& r) { | 623 | ParseInfo YueParser::parse(std::string_view codes, rule& r) { |
diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index 729304a..a614d01 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h | |||
@@ -310,6 +310,7 @@ private: | |||
310 | AST_RULE(Statement) | 310 | AST_RULE(Statement) |
311 | AST_RULE(Body) | 311 | AST_RULE(Body) |
312 | AST_RULE(Block) | 312 | AST_RULE(Block) |
313 | AST_RULE(BlockEnd) | ||
313 | AST_RULE(File) | 314 | AST_RULE(File) |
314 | }; | 315 | }; |
315 | 316 | ||