|
| 1 | +#include "Preprocessor.h" |
| 2 | +#include <cctype> |
| 3 | + |
| 4 | +using namespace std; |
| 5 | + |
| 6 | + |
| 7 | +const std::string& Preprocessor::genCode() { |
| 8 | + std::string script; |
| 9 | + |
| 10 | + std::string::const_iterator b = input.begin(); |
| 11 | + std::string::const_iterator e = input.end(); |
| 12 | + bool is_script = false; |
| 13 | + if (*b == '#') { ++b; is_script = true; } |
| 14 | + for (auto p = input.begin(); p != input.end(); ++p) { |
| 15 | + if (*p == '\n') { |
| 16 | + if (is_script) { e = p+1; script.append(b, e); } |
| 17 | + if (!is_script && *(p+1) == '#') { |
| 18 | + e = p+1; |
| 19 | + int k = add_const_str(b, e); |
| 20 | + script += "__loadStr("; |
| 21 | + script += to_string(k); |
| 22 | + script += ");\n"; |
| 23 | + } |
| 24 | + if (is_script) b=p+1; |
| 25 | + if (*(p+1) == '#') { b=p+1; ++b; is_script = true; } |
| 26 | + else { is_script = false; } |
| 27 | + } |
| 28 | + } |
| 29 | + code_string = script; |
| 30 | + return code_string; |
| 31 | +} |
| 32 | + |
| 33 | +const std::string& Preprocessor::runCode() { |
| 34 | + lua_pushglobaltable(L); |
| 35 | + lua_pushlightuserdata(L, this); |
| 36 | + lua_setfield(L, -2, "__preprocessor"); |
| 37 | + |
| 38 | + int error = luaL_loadbuffer(L, code_string.c_str(), code_string.size() ,"chunk") //加载当前script |
| 39 | + | lua_pcall(L, 0, 0, 0); // 巧妙的利用或运算符,前面若成功返回0,则执行后面的 |
| 40 | + |
| 41 | + if (error) { |
| 42 | + printf("====== ERROR! ======\n"); |
| 43 | + printf("%s\n", lua_tostring(L, -1)); |
| 44 | + lua_pop(L, 1);/* pop error message from the stack */ |
| 45 | + return output; |
| 46 | + } |
| 47 | + |
| 48 | + return output; |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +int Preprocessor::add_const_str( |
| 53 | + std::string::const_iterator p, std::string::const_iterator q) { |
| 54 | + string str(p, q); |
| 55 | + lua_pushstring(L, str.c_str()); |
| 56 | + return luaL_ref(L, LUA_REGISTRYINDEX); |
| 57 | +} |
| 58 | + |
| 59 | +static std::string add_lua_call(const char*& p) { |
| 60 | + auto b = p; |
| 61 | + while (isalpha(*p)||isdigit(*p)||(*p=='_')) ++p; |
| 62 | + if (*p == '(') { |
| 63 | + for (int k = 1; k != 0; ++p) { |
| 64 | + if (*p == '(') ++k; |
| 65 | + if (*p == ')') --k; |
| 66 | + } |
| 67 | + } |
| 68 | + return string(b, p-b); |
| 69 | +} |
| 70 | + |
| 71 | +static int lua_echo (lua_State *L) { |
| 72 | + int n = lua_gettop(L); /* number of arguments */ |
| 73 | + |
| 74 | + // get the pointer for Class Preprocess |
| 75 | + lua_pushglobaltable(L); |
| 76 | + lua_getfield(L, -1, "__preprocessor"); |
| 77 | + Preprocessor* p = (Preprocessor*)lua_topointer(L, -1); |
| 78 | + lua_pop(L, 1); |
| 79 | + |
| 80 | + lua_getglobal(L, "tostring"); |
| 81 | + for (int i=1; i<=n; i++) { |
| 82 | + const char *s; |
| 83 | + size_t l; |
| 84 | + lua_pushvalue(L, -1); /* function to be called */ |
| 85 | + lua_pushvalue(L, i); /* value to print */ |
| 86 | + lua_call(L, 1, 1); |
| 87 | + s = lua_tolstring(L, -1, &l); /* get result */ |
| 88 | + if (s == NULL) |
| 89 | + return luaL_error(L, "'tostring' must return a string to 'print'"); |
| 90 | + if (i>1) p->output += '\t'; |
| 91 | + string data(s, l); |
| 92 | + p->output += data; |
| 93 | + |
| 94 | + lua_pop(L, 1); /* pop result */ |
| 95 | + } |
| 96 | + p->output += '\n'; |
| 97 | + |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +static int lua_loadStr(lua_State* L) { |
| 104 | + lua_gettop(L); /* number of arguments */ |
| 105 | + |
| 106 | + int id = lua_tonumber(L, -1); |
| 107 | + lua_rawgeti(L, LUA_REGISTRYINDEX, id); |
| 108 | + const char* str = lua_tostring(L, -1); |
| 109 | + // get the pointer for Class Preprocess |
| 110 | + lua_pushglobaltable(L); |
| 111 | + lua_getfield(L, -1, "__preprocessor"); |
| 112 | + Preprocessor* pp = (Preprocessor*)lua_topointer(L, -1); |
| 113 | + lua_pushglobaltable(L); |
| 114 | + lua_getfield(L, -1, "Escape"); |
| 115 | + bool b = lua_toboolean(L, -1); |
| 116 | + lua_pop(L, 2); |
| 117 | + |
| 118 | + if (b == false) |
| 119 | + pp->output += str; |
| 120 | + else { |
| 121 | + for (const char* p = str; p != 0; ++p) { |
| 122 | + if (*p != '$') pp->output += *p; |
| 123 | + else { |
| 124 | + lua_pushcfunction(L, lua_echo); |
| 125 | + string script = add_lua_call(p); |
| 126 | + int error = luaL_loadbuffer(L, script.c_str(), script.size() ,"chunk") //加载当前script |
| 127 | + | lua_pcall(L, 0, 0, 0); // 巧妙的利用或运算符,前面若成功返回0,则执行后面的 |
| 128 | + |
| 129 | + if (error | lua_pcall(L, 0, 0, 0)) { |
| 130 | + printf("====== LUA ERROR! ======\n"); |
| 131 | + printf("%s\n", lua_tostring(L, -1)); |
| 132 | + lua_pop(L, 1);/* pop error message from the stack */ |
| 133 | + return 0; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + lua_pop(L, 3); |
| 140 | + return 0; |
| 141 | +} |
| 142 | + |
| 143 | +Preprocessor::Preprocessor (const std::string& input, lua_State* L) |
| 144 | + : input(input) { |
| 145 | + this->L = L; |
| 146 | + lua_register(L, "__loadStr", lua_loadStr); |
| 147 | + lua_register(L, "echo", lua_echo); |
| 148 | + |
| 149 | + lua_pushglobaltable(L); |
| 150 | + lua_pushboolean(L, 0); |
| 151 | + lua_setfield(L, -2, "Escape"); |
| 152 | +} |
| 153 | + |
| 154 | +Preprocessor::~Preprocessor () { |
| 155 | + |
| 156 | +} |
0 commit comments