1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/* Copyright (c) 2020 Jin Li, http://www.luvfight.me
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "MoonP/moon_compiler.h"
extern "C" {
#include "lua.h"
#include "lauxlib.h"
static const char moonplusCodes[] =
#include "MoonP/moonplus.h"
static void init_moonplus(lua_State* L) {
if (luaL_loadbuffer(L, moonplusCodes, sizeof(moonplusCodes) / sizeof(moonplusCodes[0]) - 1, "=(moonplus)") != 0) {
std::string err = std::string("fail to load moonplus module.\n") + lua_tostring(L, -1);
luaL_error(L, err.c_str());
} else if (lua_pcall(L, 0, 0, 0) != 0) {
std::string err = std::string("fail to init moonplus module.\n") + lua_tostring(L, -1);
luaL_error(L, err.c_str());
}
}
static const char stpCodes[] =
#include "MoonP/stacktraceplus.h"
static int init_stacktraceplus(lua_State* L) {
if (luaL_loadbuffer(L, stpCodes, sizeof(stpCodes) / sizeof(stpCodes[0]) - 1, "=(stacktraceplus)") != 0) {
std::string err = std::string("fail to load stacktraceplus module.\n") + lua_tostring(L, -1);
luaL_error(L, err.c_str());
} else if (lua_pcall(L, 0, 1, 0) != 0) {
std::string err = std::string("fail to init stacktraceplus module.\n") + lua_tostring(L, -1);
luaL_error(L, err.c_str());
}
return 1;
}
static int moontolua(lua_State* L) {
size_t size = 0;
const char* input = luaL_checklstring(L, 1, &size);
MoonP::MoonConfig config;
bool sameModule = false;
if (lua_gettop(L) == 2) {
luaL_checktype(L, 2, LUA_TTABLE);
lua_pushliteral(L, "lint_global");
lua_gettable(L, -2);
if (lua_isboolean(L, -1) != 0) {
config.lintGlobalVariable = lua_toboolean(L, -1) != 0;
}
lua_pop(L, 1);
lua_pushliteral(L, "implicit_return_root");
lua_gettable(L, -2);
if (lua_isboolean(L, -1) != 0) {
config.implicitReturnRoot = lua_toboolean(L, -1) != 0;
}
lua_pop(L, 1);
lua_pushliteral(L, "reserve_line_number");
lua_gettable(L, -2);
if (lua_isboolean(L, -1) != 0) {
config.reserveLineNumber = lua_toboolean(L, -1) != 0;
}
lua_pop(L, 1);
lua_pushliteral(L, "space_over_tab");
lua_gettable(L, -2);
if (lua_isboolean(L, -1) != 0) {
config.useSpaceOverTab = lua_toboolean(L, -1) != 0;
}
lua_pop(L, 1);
lua_pushliteral(L, "same_module");
lua_gettable(L, -2);
if (lua_isboolean(L, -1) != 0) {
sameModule = lua_toboolean(L, -1) != 0;
}
lua_pop(L, 1);
lua_pushliteral(L, "line_offset");
lua_gettable(L, -2);
if (lua_isnumber(L, -1) != 0) {
config.lineOffset = static_cast<int>(lua_tonumber(L, -1));
}
lua_pop(L, 1);
}
std::string s(input, size);
std::string codes, err;
MoonP::GlobalVars globals;
std::tie(codes, err, globals) = MoonP::MoonCompiler(L, nullptr, sameModule).compile(s, config);
if (codes.empty() && !err.empty()) {
lua_pushnil(L);
} else {
lua_pushlstring(L, codes.c_str(), codes.size());
}
if (err.empty()) {
lua_pushnil(L);
} else {
lua_pushlstring(L, err.c_str(), err.size());
}
if (globals) {
lua_createtable(L, static_cast<int>(globals->size()), 0);
int i = 1;
for (const auto& var : *globals) {
lua_createtable(L, 3, 0);
lua_pushlstring(L, var.name.c_str(), var.name.size());
lua_rawseti(L, -2, 1);
lua_pushinteger(L, var.line);
lua_rawseti(L, -2, 2);
lua_pushinteger(L, var.col);
lua_rawseti(L, -2, 3);
lua_rawseti(L, -2, i);
i++;
}
} else {
lua_pushnil(L);
}
return 3;
}
int luaopen_moonp(lua_State* L) {
lua_getglobal(L, "package"); // package
lua_getfield(L, -1, "loaded"); // package loaded
lua_createtable(L, 0, 0); // package loaded moonp
lua_pushcfunction(L, moontolua); // package loaded moonp func
lua_setfield(L, -2, "to_lua"); // moonp["to_lua"] = func, package loaded tb
lua_pushcfunction(L, init_stacktraceplus); // package loaded moonp func1
lua_setfield(L, -2, "load_stacktraceplus"); // moonp["load_stacktraceplus"] = func1, package loaded moonp
lua_setfield(L, -2, "moonp"); // loaded["moonp"] = moonp, package loaded
lua_pop(L, 2); // empty
init_moonplus(L);
return 0;
}
} // extern "C"
|