aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/moonplus.cpp
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-02-17 11:22:07 +0800
committerLi Jin <dragon-fly@qq.com>2021-02-17 11:22:07 +0800
commit7066392d1c974065181d95d93274136dcd625d43 (patch)
treecf51eafc2c52cbc12246a306bca172d799193d30 /src/MoonP/moonplus.cpp
parent90cd12ad9ef465f3e435e1bd034dcfbe4e19d016 (diff)
downloadyuescript-7066392d1c974065181d95d93274136dcd625d43.tar.gz
yuescript-7066392d1c974065181d95d93274136dcd625d43.tar.bz2
yuescript-7066392d1c974065181d95d93274136dcd625d43.zip
stop reusing variables, rename project.
Diffstat (limited to 'src/MoonP/moonplus.cpp')
-rw-r--r--src/MoonP/moonplus.cpp139
1 files changed, 0 insertions, 139 deletions
diff --git a/src/MoonP/moonplus.cpp b/src/MoonP/moonplus.cpp
deleted file mode 100644
index 9b9689c..0000000
--- a/src/MoonP/moonplus.cpp
+++ /dev/null
@@ -1,139 +0,0 @@
1/* Copyright (c) 2021 Jin Li, http://www.luvfight.me
2
3Permission 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:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE 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. */
8#include "MoonP/moon_compiler.h"
9
10extern "C" {
11
12#include "lua.h"
13#include "lauxlib.h"
14
15static const char moonplusCodes[] =
16#include "MoonP/moonplus.h"
17
18static void init_moonplus(lua_State* L) {
19 if (luaL_loadbuffer(L, moonplusCodes, sizeof(moonplusCodes) / sizeof(moonplusCodes[0]) - 1, "=(moonplus)") != 0) {
20 std::string err = std::string("failed to load moonplus module.\n") + lua_tostring(L, -1);
21 luaL_error(L, err.c_str());
22 } else if (lua_pcall(L, 0, 0, 0) != 0) {
23 std::string err = std::string("failed to init moonplus module.\n") + lua_tostring(L, -1);
24 luaL_error(L, err.c_str());
25 }
26}
27
28static const char stpCodes[] =
29#include "MoonP/stacktraceplus.h"
30
31static int init_stacktraceplus(lua_State* L) {
32 if (luaL_loadbuffer(L, stpCodes, sizeof(stpCodes) / sizeof(stpCodes[0]) - 1, "=(stacktraceplus)") != 0) {
33 std::string err = std::string("failed to load stacktraceplus module.\n") + lua_tostring(L, -1);
34 luaL_error(L, err.c_str());
35 } else if (lua_pcall(L, 0, 1, 0) != 0) {
36 std::string err = std::string("failed to init stacktraceplus module.\n") + lua_tostring(L, -1);
37 luaL_error(L, err.c_str());
38 }
39 return 1;
40}
41
42static int moontolua(lua_State* L) {
43 size_t size = 0;
44 const char* input = luaL_checklstring(L, 1, &size);
45 MoonP::MoonConfig config;
46 bool sameModule = false;
47 if (lua_gettop(L) == 2) {
48 luaL_checktype(L, 2, LUA_TTABLE);
49 lua_pushliteral(L, "lint_global");
50 lua_gettable(L, -2);
51 if (lua_isboolean(L, -1) != 0) {
52 config.lintGlobalVariable = lua_toboolean(L, -1) != 0;
53 }
54 lua_pop(L, 1);
55 lua_pushliteral(L, "implicit_return_root");
56 lua_gettable(L, -2);
57 if (lua_isboolean(L, -1) != 0) {
58 config.implicitReturnRoot = lua_toboolean(L, -1) != 0;
59 }
60 lua_pop(L, 1);
61 lua_pushliteral(L, "reserve_line_number");
62 lua_gettable(L, -2);
63 if (lua_isboolean(L, -1) != 0) {
64 config.reserveLineNumber = lua_toboolean(L, -1) != 0;
65 }
66 lua_pop(L, 1);
67 lua_pushliteral(L, "space_over_tab");
68 lua_gettable(L, -2);
69 if (lua_isboolean(L, -1) != 0) {
70 config.useSpaceOverTab = lua_toboolean(L, -1) != 0;
71 }
72 lua_pop(L, 1);
73 lua_pushliteral(L, "same_module");
74 lua_gettable(L, -2);
75 if (lua_isboolean(L, -1) != 0) {
76 sameModule = lua_toboolean(L, -1) != 0;
77 }
78 lua_pop(L, 1);
79 lua_pushliteral(L, "line_offset");
80 lua_gettable(L, -2);
81 if (lua_isnumber(L, -1) != 0) {
82 config.lineOffset = static_cast<int>(lua_tonumber(L, -1));
83 }
84 lua_pop(L, 1);
85 }
86 std::string s(input, size);
87 auto result = MoonP::MoonCompiler(L, nullptr, sameModule).compile(s, config);
88 if (result.codes.empty() && !result.error.empty()) {
89 lua_pushnil(L);
90 } else {
91 lua_pushlstring(L, result.codes.c_str(), result.codes.size());
92 }
93 if (result.error.empty()) {
94 lua_pushnil(L);
95 } else {
96 lua_pushlstring(L, result.error.c_str(), result.error.size());
97 }
98 if (result.globals) {
99 lua_createtable(L, static_cast<int>(result.globals->size()), 0);
100 int i = 1;
101 for (const auto& var : *result.globals) {
102 lua_createtable(L, 3, 0);
103 lua_pushlstring(L, var.name.c_str(), var.name.size());
104 lua_rawseti(L, -2, 1);
105 lua_pushinteger(L, var.line);
106 lua_rawseti(L, -2, 2);
107 lua_pushinteger(L, var.col);
108 lua_rawseti(L, -2, 3);
109 lua_rawseti(L, -2, i);
110 i++;
111 }
112 } else {
113 lua_pushnil(L);
114 }
115 return 3;
116}
117
118int luaopen_moonp(lua_State* L) {
119 lua_getglobal(L, "package"); // package
120 lua_getfield(L, -1, "loaded"); // package loaded
121 lua_createtable(L, 0, 0); // package loaded moonp
122 lua_pushcfunction(L, moontolua); // package loaded moonp func
123 lua_setfield(L, -2, "to_lua"); // moonp["to_lua"] = func, package loaded moonp
124 lua_pushlstring(L, &MoonP::version.front(), MoonP::version.size()); // package loaded moonp version
125 lua_setfield(L, -2, "version"); // moonp["version"] = version, package loaded moonp
126 lua_createtable(L, 0, 0); // package loaded moonp options
127 lua_pushlstring(L, &MoonP::extension.front(), MoonP::extension.size()); // package loaded moonp options ext
128 lua_setfield(L, -2, "extension"); // options["extension"] = ext, package loaded moonp options
129 lua_setfield(L, -2, "options"); // moonp["options"] = options, package loaded moonp
130 lua_pushcfunction(L, init_stacktraceplus); // package loaded moonp func1
131 lua_setfield(L, -2, "load_stacktraceplus"); // moonp["load_stacktraceplus"] = func1, package loaded moonp
132 lua_setfield(L, -2, "moonp"); // loaded["moonp"] = moonp, package loaded
133 lua_pop(L, 2); // empty
134 init_moonplus(L);
135 return 0;
136}
137
138} // extern "C"
139