diff options
author | Li Jin <dragon-fly@qq.com> | 2021-05-25 09:47:07 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2021-05-25 09:47:07 +0800 |
commit | 42a97ee390258515912a59537fdce1a5a2ac847c (patch) | |
tree | 1242556d4fe4fe3619ec88918ded92b12fc635a1 | |
parent | 4d61a00ebc5b956da72525de0e180de28a1e8ac6 (diff) | |
download | yuescript-42a97ee390258515912a59537fdce1a5a2ac847c.tar.gz yuescript-42a97ee390258515912a59537fdce1a5a2ac847c.tar.bz2 yuescript-42a97ee390258515912a59537fdce1a5a2ac847c.zip |
cleanup.
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 473 |
1 files changed, 235 insertions, 238 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 0c71806..4b3064e 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -38,6 +38,7 @@ extern "C" { | |||
38 | 38 | ||
39 | namespace yue { | 39 | namespace yue { |
40 | using namespace std::string_view_literals; | 40 | using namespace std::string_view_literals; |
41 | using namespace std::literals::string_literals; | ||
41 | using namespace parserlib; | 42 | using namespace parserlib; |
42 | 43 | ||
43 | #define BLOCK_START do { | 44 | #define BLOCK_START do { |
@@ -48,17 +49,13 @@ using namespace parserlib; | |||
48 | #define DEFER(code) _DEFER(code,__LINE__) | 49 | #define DEFER(code) _DEFER(code,__LINE__) |
49 | #define YUEE(msg,node) throw std::logic_error( \ | 50 | #define YUEE(msg,node) throw std::logic_error( \ |
50 | _info.errorMessage( \ | 51 | _info.errorMessage( \ |
51 | std::string("[File] ") + __FILE__ \ | 52 | "[File] "s + __FILE__ \ |
52 | + ",\n[Func] " + __FUNCTION__ \ | 53 | + ",\n[Func] "s + __FUNCTION__ \ |
53 | + ",\n[Line] " + std::to_string(__LINE__) \ | 54 | + ",\n[Line] "s + std::to_string(__LINE__) \ |
54 | + ",\n[Error] " + msg, node)) | 55 | + ",\n[Error] "s + msg, node)) |
55 | 56 | ||
56 | typedef std::list<std::string> str_list; | 57 | typedef std::list<std::string> str_list; |
57 | 58 | ||
58 | inline std::string s(std::string_view sv) { | ||
59 | return std::string(sv); | ||
60 | } | ||
61 | |||
62 | const std::string_view version = "0.7.14"sv; | 59 | const std::string_view version = "0.7.14"sv; |
63 | const std::string_view extension = "yue"sv; | 60 | const std::string_view extension = "yue"sv; |
64 | 61 | ||
@@ -340,7 +337,7 @@ private: | |||
340 | 337 | ||
341 | void checkConst(const std::string& name, ast_node* x) const { | 338 | void checkConst(const std::string& name, ast_node* x) const { |
342 | if (isConst(name)) { | 339 | if (isConst(name)) { |
343 | throw std::logic_error(_info.errorMessage(s("attempt to assign to const variable '"sv) + name + '\'', x)); | 340 | throw std::logic_error(_info.errorMessage("attempt to assign to const variable '"s + name + '\'', x)); |
344 | } | 341 | } |
345 | } | 342 | } |
346 | 343 | ||
@@ -395,7 +392,7 @@ private: | |||
395 | int index = 0; | 392 | int index = 0; |
396 | std::string newName; | 393 | std::string newName; |
397 | do { | 394 | do { |
398 | newName = s(name) + std::to_string(index); | 395 | newName = std::string(name) + std::to_string(index); |
399 | index++; | 396 | index++; |
400 | } while (isLocal(newName)); | 397 | } while (isLocal(newName)); |
401 | return newName; | 398 | return newName; |
@@ -408,9 +405,9 @@ private: | |||
408 | transformValue(value, tmp); | 405 | transformValue(value, tmp); |
409 | } else { | 406 | } else { |
410 | transformExp(cond, tmp, ExpUsage::Closure); | 407 | transformExp(cond, tmp, ExpUsage::Closure); |
411 | tmp.back() = s("("sv) + tmp.back() + s(")"sv); | 408 | tmp.back() = '(' + tmp.back() + ')'; |
412 | } | 409 | } |
413 | return s("not "sv) + tmp.back(); | 410 | return "not "s + tmp.back(); |
414 | } else { | 411 | } else { |
415 | transformExp(cond, tmp, ExpUsage::Closure); | 412 | transformExp(cond, tmp, ExpUsage::Closure); |
416 | return tmp.back(); | 413 | return tmp.back(); |
@@ -419,7 +416,7 @@ private: | |||
419 | 416 | ||
420 | const std::string nll(ast_node* node) const { | 417 | const std::string nll(ast_node* node) const { |
421 | if (_config.reserveLineNumber) { | 418 | if (_config.reserveLineNumber) { |
422 | return s(" -- "sv) + std::to_string(node->m_begin.m_line + _config.lineOffset) + _newLine; | 419 | return " -- "s + std::to_string(node->m_begin.m_line + _config.lineOffset) + _newLine; |
423 | } else { | 420 | } else { |
424 | return _newLine; | 421 | return _newLine; |
425 | } | 422 | } |
@@ -427,7 +424,7 @@ private: | |||
427 | 424 | ||
428 | const std::string nlr(ast_node* node) const { | 425 | const std::string nlr(ast_node* node) const { |
429 | if (_config.reserveLineNumber) { | 426 | if (_config.reserveLineNumber) { |
430 | return s(" -- "sv) + std::to_string(node->m_end.m_line + _config.lineOffset) + _newLine; | 427 | return " -- "s + std::to_string(node->m_end.m_line + _config.lineOffset) + _newLine; |
431 | } else { | 428 | } else { |
432 | return _newLine; | 429 | return _newLine; |
433 | } | 430 | } |
@@ -479,7 +476,7 @@ private: | |||
479 | std::string join(const str_list& items, std::string_view sep) { | 476 | std::string join(const str_list& items, std::string_view sep) { |
480 | if (items.empty()) return Empty; | 477 | if (items.empty()) return Empty; |
481 | else if (items.size() == 1) return items.front(); | 478 | else if (items.size() == 1) return items.front(); |
482 | std::string sepStr = s(sep); | 479 | std::string sepStr = std::string(sep); |
483 | auto begin = ++items.begin(); | 480 | auto begin = ++items.begin(); |
484 | _joinBuf << items.front(); | 481 | _joinBuf << items.front(); |
485 | for (auto it = begin; it != items.end(); ++it) { | 482 | for (auto it = begin; it != items.end(); ++it) { |
@@ -674,7 +671,7 @@ private: | |||
674 | 671 | ||
675 | template <class T> | 672 | template <class T> |
676 | ast_ptr<false, T> toAst(std::string_view codes, ast_node* parent) { | 673 | ast_ptr<false, T> toAst(std::string_view codes, ast_node* parent) { |
677 | auto res = _parser.parse<T>(s(codes)); | 674 | auto res = _parser.parse<T>(std::string(codes)); |
678 | res.node->traverse([&](ast_node* node) { | 675 | res.node->traverse([&](ast_node* node) { |
679 | node->m_begin.m_line = parent->m_begin.m_line; | 676 | node->m_begin.m_line = parent->m_begin.m_line; |
680 | node->m_begin.m_col = parent->m_begin.m_col; | 677 | node->m_begin.m_col = parent->m_begin.m_col; |
@@ -1122,7 +1119,7 @@ private: | |||
1122 | 1119 | ||
1123 | std::string getPredefine(const str_list& defs) { | 1120 | std::string getPredefine(const str_list& defs) { |
1124 | if (defs.empty()) return Empty; | 1121 | if (defs.empty()) return Empty; |
1125 | return indent() + s("local "sv) + join(defs, ", "sv); | 1122 | return indent() + "local "s + join(defs, ", "sv); |
1126 | } | 1123 | } |
1127 | 1124 | ||
1128 | std::string getDestrucureDefine(ExpListAssign_t* assignment) { | 1125 | std::string getDestrucureDefine(ExpListAssign_t* assignment) { |
@@ -1187,7 +1184,7 @@ private: | |||
1187 | chainValue->items.pop_back(); | 1184 | chainValue->items.pop_back(); |
1188 | transformExp(static_cast<Exp_t*>(*it), args, ExpUsage::Closure); | 1185 | transformExp(static_cast<Exp_t*>(*it), args, ExpUsage::Closure); |
1189 | if (vit != values.end()) transformAssignItem(*vit, args); | 1186 | if (vit != values.end()) transformAssignItem(*vit, args); |
1190 | else args.push_back(s("nil"sv)); | 1187 | else args.push_back("nil"s); |
1191 | _buf << indent() << globalVar("setmetatable"sv, x) << '(' << join(args, ", "sv) << ')' << nll(x); | 1188 | _buf << indent() << globalVar("setmetatable"sv, x) << '(' << join(args, ", "sv) << ')' << nll(x); |
1192 | str_list temp; | 1189 | str_list temp; |
1193 | temp.push_back(clearBuf()); | 1190 | temp.push_back(clearBuf()); |
@@ -1348,7 +1345,7 @@ private: | |||
1348 | _buf << indent(); | 1345 | _buf << indent(); |
1349 | if (pair.isVariable) { | 1346 | if (pair.isVariable) { |
1350 | checkConst(pair.name, assignment); | 1347 | checkConst(pair.name, assignment); |
1351 | if (!isDefined(pair.name)) _buf << s("local "sv); | 1348 | if (!isDefined(pair.name)) _buf << "local "s; |
1352 | } | 1349 | } |
1353 | _buf << pair.name << " = "sv << destruct.value << pair.structure << nll(assignment); | 1350 | _buf << pair.name << " = "sv << destruct.value << pair.structure << nll(assignment); |
1354 | addToScope(pair.name); | 1351 | addToScope(pair.name); |
@@ -1455,7 +1452,7 @@ private: | |||
1455 | auto subPairs = destructFromExp(pair); | 1452 | auto subPairs = destructFromExp(pair); |
1456 | for (auto& p : subPairs) { | 1453 | for (auto& p : subPairs) { |
1457 | pairs.push_back({p.isVariable, p.name, | 1454 | pairs.push_back({p.isVariable, p.name, |
1458 | s("["sv) + std::to_string(index) + s("]"sv) + p.structure}); | 1455 | '[' + std::to_string(index) + ']' + p.structure}); |
1459 | } | 1456 | } |
1460 | } else { | 1457 | } else { |
1461 | bool lintGlobal = _config.lintGlobalVariable; | 1458 | bool lintGlobal = _config.lintGlobalVariable; |
@@ -1481,9 +1478,9 @@ private: | |||
1481 | auto vp = static_cast<variable_pair_t*>(pair); | 1478 | auto vp = static_cast<variable_pair_t*>(pair); |
1482 | auto name = _parser.toString(vp->name); | 1479 | auto name = _parser.toString(vp->name); |
1483 | if (Keywords.find(name) != Keywords.end()) { | 1480 | if (Keywords.find(name) != Keywords.end()) { |
1484 | pairs.push_back({true, name, s("[\""sv) + name + s("\"]"sv)}); | 1481 | pairs.push_back({true, name, "[\""s + name + "\"]"s}); |
1485 | } else { | 1482 | } else { |
1486 | pairs.push_back({true, name, s("."sv) + name}); | 1483 | pairs.push_back({true, name, '.' + name}); |
1487 | } | 1484 | } |
1488 | break; | 1485 | break; |
1489 | } | 1486 | } |
@@ -1495,9 +1492,9 @@ private: | |||
1495 | if (!key) throw std::logic_error(_info.errorMessage("invalid key for destructure"sv, np)); | 1492 | if (!key) throw std::logic_error(_info.errorMessage("invalid key for destructure"sv, np)); |
1496 | keyName = _parser.toString(key); | 1493 | keyName = _parser.toString(key); |
1497 | if (Keywords.find(keyName) != Keywords.end()) { | 1494 | if (Keywords.find(keyName) != Keywords.end()) { |
1498 | keyName = s("[\""sv) + keyName + s("\"]"sv); | 1495 | keyName = "[\""s + keyName + "\"]"s; |
1499 | } else { | 1496 | } else { |
1500 | keyName = s("."sv) + keyName; | 1497 | keyName = "."s + keyName; |
1501 | } | 1498 | } |
1502 | } | 1499 | } |
1503 | if (auto exp = np->value.as<Exp_t>()) { | 1500 | if (auto exp = np->value.as<Exp_t>()) { |
@@ -1757,7 +1754,7 @@ private: | |||
1757 | auto right = std::move(temp.back()); | 1754 | auto right = std::move(temp.back()); |
1758 | temp.pop_back(); | 1755 | temp.pop_back(); |
1759 | if (!singleValueFrom(update->value)) { | 1756 | if (!singleValueFrom(update->value)) { |
1760 | right = s("("sv) + right + s(")"sv); | 1757 | right = '(' + right + ')'; |
1761 | } | 1758 | } |
1762 | _buf << join(temp) << indent() << left << " = "sv << left << | 1759 | _buf << join(temp) << indent() << left << " = "sv << left << |
1763 | ' ' << _parser.toString(update->op) << ' ' << right << nll(assignment); | 1760 | ' ' << _parser.toString(update->op) << ' ' << right << nll(assignment); |
@@ -1790,9 +1787,9 @@ private: | |||
1790 | transformExpList(expList, temp); | 1787 | transformExpList(expList, temp); |
1791 | std::string left = std::move(temp.back()); | 1788 | std::string left = std::move(temp.back()); |
1792 | temp.pop_back(); | 1789 | temp.pop_back(); |
1793 | out.push_back(indent() + left + s(" = "sv) + join(temp, ", "sv) + nll(assignment)); | 1790 | out.push_back(indent() + left + " = "s + join(temp, ", "sv) + nll(assignment)); |
1794 | } else { | 1791 | } else { |
1795 | out.push_back(preDefine + s(" = "sv) + join(temp, ", "sv) + nll(assignment)); | 1792 | out.push_back(preDefine + " = "s + join(temp, ", "sv) + nll(assignment)); |
1796 | } | 1793 | } |
1797 | } else { | 1794 | } else { |
1798 | std::string preDefine = getPredefine(defs); | 1795 | std::string preDefine = getPredefine(defs); |
@@ -1805,7 +1802,7 @@ private: | |||
1805 | for (auto value : assign->values.objects()) { | 1802 | for (auto value : assign->values.objects()) { |
1806 | transformAssignItem(value, temp); | 1803 | transformAssignItem(value, temp); |
1807 | } | 1804 | } |
1808 | out.push_back((preDefine.empty() ? Empty : preDefine + nll(assignment)) + indent() + left + s(" = "sv) + join(temp, ", "sv) + nll(assignment)); | 1805 | out.push_back((preDefine.empty() ? Empty : preDefine + nll(assignment)) + indent() + left + " = "s + join(temp, ", "sv) + nll(assignment)); |
1809 | } | 1806 | } |
1810 | break; | 1807 | break; |
1811 | } | 1808 | } |
@@ -1853,7 +1850,7 @@ private: | |||
1853 | } | 1850 | } |
1854 | str_list temp; | 1851 | str_list temp; |
1855 | if (usage == ExpUsage::Closure) { | 1852 | if (usage == ExpUsage::Closure) { |
1856 | temp.push_back(s("(function()"sv) + nll(nodes.front())); | 1853 | temp.push_back("(function()"s + nll(nodes.front())); |
1857 | pushScope(); | 1854 | pushScope(); |
1858 | _enableReturn.push(true); | 1855 | _enableReturn.push(true); |
1859 | } | 1856 | } |
@@ -1894,7 +1891,7 @@ private: | |||
1894 | } | 1891 | } |
1895 | if (storingValue) { | 1892 | if (storingValue) { |
1896 | if (usage != ExpUsage::Closure) { | 1893 | if (usage != ExpUsage::Closure) { |
1897 | temp.push_back(indent() + s("do"sv) + nll(assign)); | 1894 | temp.push_back(indent() + "do"s + nll(assign)); |
1898 | pushScope(); | 1895 | pushScope(); |
1899 | } | 1896 | } |
1900 | auto expList = toAst<ExpList_t>(desVar, x); | 1897 | auto expList = toAst<ExpList_t>(desVar, x); |
@@ -1919,7 +1916,7 @@ private: | |||
1919 | if (!isDefined(var)) { | 1916 | if (!isDefined(var)) { |
1920 | storingValue = true; | 1917 | storingValue = true; |
1921 | if (usage != ExpUsage::Closure) { | 1918 | if (usage != ExpUsage::Closure) { |
1922 | temp.push_back(indent() + s("do"sv) + nll(assign)); | 1919 | temp.push_back(indent() + "do"s + nll(assign)); |
1923 | pushScope(); | 1920 | pushScope(); |
1924 | } | 1921 | } |
1925 | } | 1922 | } |
@@ -1946,7 +1943,7 @@ private: | |||
1946 | } | 1943 | } |
1947 | if (pair.second) { | 1944 | if (pair.second) { |
1948 | if (!pair.first) { | 1945 | if (!pair.first) { |
1949 | temp.push_back(indent() + s("else"sv) + nll(pair.second)); | 1946 | temp.push_back(indent() + "else"s + nll(pair.second)); |
1950 | } | 1947 | } |
1951 | pushScope(); | 1948 | pushScope(); |
1952 | if (pair == ifCondPairs.front() && extraAssignment) { | 1949 | if (pair == ifCondPairs.front() && extraAssignment) { |
@@ -1956,18 +1953,18 @@ private: | |||
1956 | popScope(); | 1953 | popScope(); |
1957 | } | 1954 | } |
1958 | if (!pair.first) { | 1955 | if (!pair.first) { |
1959 | temp.push_back(indent() + s("end"sv) + nll(nodes.front())); | 1956 | temp.push_back(indent() + "end"s + nll(nodes.front())); |
1960 | break; | 1957 | break; |
1961 | } | 1958 | } |
1962 | } | 1959 | } |
1963 | if (storingValue && usage != ExpUsage::Closure) { | 1960 | if (storingValue && usage != ExpUsage::Closure) { |
1964 | popScope(); | 1961 | popScope(); |
1965 | temp.push_back(indent() + s("end"sv) + nlr(nodes.front())); | 1962 | temp.push_back(indent() + "end"s + nlr(nodes.front())); |
1966 | } | 1963 | } |
1967 | if (usage == ExpUsage::Closure) { | 1964 | if (usage == ExpUsage::Closure) { |
1968 | _enableReturn.pop(); | 1965 | _enableReturn.pop(); |
1969 | popScope(); | 1966 | popScope(); |
1970 | temp.push_back(indent() + s("end)()"sv)); | 1967 | temp.push_back(indent() + "end)()"s); |
1971 | } | 1968 | } |
1972 | out.push_back(join(temp)); | 1969 | out.push_back(join(temp)); |
1973 | } | 1970 | } |
@@ -2132,7 +2129,7 @@ private: | |||
2132 | if (_varArgs.empty() || !_varArgs.top()) { | 2129 | if (_varArgs.empty() || !_varArgs.top()) { |
2133 | throw std::logic_error(_info.errorMessage("cannot use '...' outside a vararg function near '...'"sv, item)); | 2130 | throw std::logic_error(_info.errorMessage("cannot use '...' outside a vararg function near '...'"sv, item)); |
2134 | } | 2131 | } |
2135 | out.push_back(s("..."sv)); | 2132 | out.push_back("..."s); |
2136 | break; | 2133 | break; |
2137 | case id<Parens_t>(): transformParens(static_cast<Parens_t*>(item), out); break; | 2134 | case id<Parens_t>(): transformParens(static_cast<Parens_t*>(item), out); break; |
2138 | default: YUEE("AST node mismatch", item); break; | 2135 | default: YUEE("AST node mismatch", item); break; |
@@ -2142,7 +2139,7 @@ private: | |||
2142 | void transformParens(Parens_t* parans, str_list& out) { | 2139 | void transformParens(Parens_t* parans, str_list& out) { |
2143 | str_list temp; | 2140 | str_list temp; |
2144 | transformExp(parans->expr, temp, ExpUsage::Closure); | 2141 | transformExp(parans->expr, temp, ExpUsage::Closure); |
2145 | out.push_back(s("("sv) + temp.front() + s(")"sv)); | 2142 | out.push_back('(' + temp.front() + ')'); |
2146 | } | 2143 | } |
2147 | 2144 | ||
2148 | void transformSimpleValue(SimpleValue_t* simpleValue, str_list& out) { | 2145 | void transformSimpleValue(SimpleValue_t* simpleValue, str_list& out) { |
@@ -2174,7 +2171,7 @@ private: | |||
2174 | bool isFatArrow = _parser.toString(funLit->arrow) == "=>"sv; | 2171 | bool isFatArrow = _parser.toString(funLit->arrow) == "=>"sv; |
2175 | pushScope(); | 2172 | pushScope(); |
2176 | if (isFatArrow) { | 2173 | if (isFatArrow) { |
2177 | forceAddToScope(s("self"sv)); | 2174 | forceAddToScope("self"s); |
2178 | } | 2175 | } |
2179 | if (auto argsDef = funLit->argsDef.get()) { | 2176 | if (auto argsDef = funLit->argsDef.get()) { |
2180 | transformFnArgsDef(argsDef, temp); | 2177 | transformFnArgsDef(argsDef, temp); |
@@ -2209,7 +2206,7 @@ private: | |||
2209 | } | 2206 | } |
2210 | auto& bodyCodes = temp.back(); | 2207 | auto& bodyCodes = temp.back(); |
2211 | _buf << "function("sv << | 2208 | _buf << "function("sv << |
2212 | (isFatArrow ? s("self"sv) : Empty) << | 2209 | (isFatArrow ? "self"s : Empty) << |
2213 | ')'; | 2210 | ')'; |
2214 | if (!bodyCodes.empty()) { | 2211 | if (!bodyCodes.empty()) { |
2215 | _buf << nll(funLit) << bodyCodes; | 2212 | _buf << nll(funLit) << bodyCodes; |
@@ -2448,7 +2445,7 @@ private: | |||
2448 | } | 2445 | } |
2449 | } | 2446 | } |
2450 | if (isRoot && !_info.moduleName.empty()) { | 2447 | if (isRoot && !_info.moduleName.empty()) { |
2451 | block->statements.push_front(toAst<Statement_t>(_info.moduleName + s(_info.exportDefault ? "=nil"sv : "={}"sv), block)); | 2448 | block->statements.push_front(toAst<Statement_t>(_info.moduleName + (_info.exportDefault ? "=nil"s : "={}"s), block)); |
2452 | } | 2449 | } |
2453 | switch (usage) { | 2450 | switch (usage) { |
2454 | case ExpUsage::Closure: | 2451 | case ExpUsage::Closure: |
@@ -2528,7 +2525,7 @@ private: | |||
2528 | out.push_back(Empty); | 2525 | out.push_back(Empty); |
2529 | } | 2526 | } |
2530 | if (isRoot && !_info.moduleName.empty()) { | 2527 | if (isRoot && !_info.moduleName.empty()) { |
2531 | out.back().append(indent() + s("return "sv) + _info.moduleName + nlr(block)); | 2528 | out.back().append(indent() + "return "s + _info.moduleName + nlr(block)); |
2532 | } | 2529 | } |
2533 | } | 2530 | } |
2534 | 2531 | ||
@@ -2676,22 +2673,22 @@ private: | |||
2676 | pushOptions(macro->m_begin.m_line - 1); // cur loadstring codes chunk options | 2673 | pushOptions(macro->m_begin.m_line - 1); // cur loadstring codes chunk options |
2677 | if (lua_pcall(L, 3, 2, 0) != 0) { // loadstring(codes,chunk,options), cur f err | 2674 | if (lua_pcall(L, 3, 2, 0) != 0) { // loadstring(codes,chunk,options), cur f err |
2678 | std::string err = lua_tostring(L, -1); | 2675 | std::string err = lua_tostring(L, -1); |
2679 | throw std::logic_error(_info.errorMessage(s("failed to load macro codes\n"sv) + err, macro->macroLit)); | 2676 | throw std::logic_error(_info.errorMessage("failed to load macro codes\n"s + err, macro->macroLit)); |
2680 | } // cur f err | 2677 | } // cur f err |
2681 | if (lua_isnil(L, -2) != 0) { // f == nil, cur f err | 2678 | if (lua_isnil(L, -2) != 0) { // f == nil, cur f err |
2682 | std::string err = lua_tostring(L, -1); | 2679 | std::string err = lua_tostring(L, -1); |
2683 | throw std::logic_error(_info.errorMessage(s("failed to load macro codes, at (macro "sv) + macroName + s("): "sv) + err, macro->macroLit)); | 2680 | throw std::logic_error(_info.errorMessage("failed to load macro codes, at (macro "s + macroName + "): "s + err, macro->macroLit)); |
2684 | } | 2681 | } |
2685 | lua_pop(L, 1); // cur f | 2682 | lua_pop(L, 1); // cur f |
2686 | pushYue("pcall"sv); // cur f pcall | 2683 | pushYue("pcall"sv); // cur f pcall |
2687 | lua_insert(L, -2); // cur pcall f | 2684 | lua_insert(L, -2); // cur pcall f |
2688 | if (lua_pcall(L, 1, 2, 0) != 0) { // f(), cur success macro | 2685 | if (lua_pcall(L, 1, 2, 0) != 0) { // f(), cur success macro |
2689 | std::string err = lua_tostring(L, -1); | 2686 | std::string err = lua_tostring(L, -1); |
2690 | throw std::logic_error(_info.errorMessage(s("failed to generate macro function\n"sv) + err, macro->macroLit)); | 2687 | throw std::logic_error(_info.errorMessage("failed to generate macro function\n"s + err, macro->macroLit)); |
2691 | } // cur success res | 2688 | } // cur success res |
2692 | if (lua_toboolean(L, -2) == 0) { | 2689 | if (lua_toboolean(L, -2) == 0) { |
2693 | std::string err = lua_tostring(L, -1); | 2690 | std::string err = lua_tostring(L, -1); |
2694 | throw std::logic_error(_info.errorMessage(s("failed to generate macro function\n"sv) + err, macro->macroLit)); | 2691 | throw std::logic_error(_info.errorMessage("failed to generate macro function\n"s + err, macro->macroLit)); |
2695 | } // cur true macro | 2692 | } // cur true macro |
2696 | lua_remove(L, -2); // cur macro | 2693 | lua_remove(L, -2); // cur macro |
2697 | if (exporting && !_moduleName.empty()) { | 2694 | if (exporting && !_moduleName.empty()) { |
@@ -2768,15 +2765,15 @@ private: | |||
2768 | } | 2765 | } |
2769 | } | 2766 | } |
2770 | transformValue(singleValue, out); | 2767 | transformValue(singleValue, out); |
2771 | out.back() = indent() + s("return "sv) + out.back() + nlr(returnNode); | 2768 | out.back() = indent() + "return "s + out.back() + nlr(returnNode); |
2772 | return; | 2769 | return; |
2773 | } else { | 2770 | } else { |
2774 | str_list temp; | 2771 | str_list temp; |
2775 | transformExpListLow(valueList, temp); | 2772 | transformExpListLow(valueList, temp); |
2776 | out.push_back(indent() + s("return "sv) + temp.back() + nlr(returnNode)); | 2773 | out.push_back(indent() + "return "s + temp.back() + nlr(returnNode)); |
2777 | } | 2774 | } |
2778 | } else { | 2775 | } else { |
2779 | out.push_back(indent() + s("return"sv) + nll(returnNode)); | 2776 | out.push_back(indent() + "return"s + nll(returnNode)); |
2780 | } | 2777 | } |
2781 | } | 2778 | } |
2782 | 2779 | ||
@@ -2823,7 +2820,7 @@ private: | |||
2823 | case id<self_class_name_t>(): { | 2820 | case id<self_class_name_t>(): { |
2824 | auto clsName = static_cast<self_class_name_t*>(selfName->name.get()); | 2821 | auto clsName = static_cast<self_class_name_t*>(selfName->name.get()); |
2825 | arg.name = _parser.toString(clsName->name); | 2822 | arg.name = _parser.toString(clsName->name); |
2826 | arg.assignSelf = s("self.__class."sv) + arg.name; | 2823 | arg.assignSelf = "self.__class."s + arg.name; |
2827 | break; | 2824 | break; |
2828 | } | 2825 | } |
2829 | case id<self_class_t>(): | 2826 | case id<self_class_t>(): |
@@ -2832,7 +2829,7 @@ private: | |||
2832 | case id<self_name_t>(): { | 2829 | case id<self_name_t>(): { |
2833 | auto sfName = static_cast<self_name_t*>(selfName->name.get()); | 2830 | auto sfName = static_cast<self_name_t*>(selfName->name.get()); |
2834 | arg.name = _parser.toString(sfName->name); | 2831 | arg.name = _parser.toString(sfName->name); |
2835 | arg.assignSelf = s("self."sv) + arg.name; | 2832 | arg.assignSelf = "self."s + arg.name; |
2836 | break; | 2833 | break; |
2837 | } | 2834 | } |
2838 | case id<self_t>(): | 2835 | case id<self_t>(): |
@@ -2861,13 +2858,13 @@ private: | |||
2861 | temp.back() = clearBuf(); | 2858 | temp.back() = clearBuf(); |
2862 | } | 2859 | } |
2863 | if (varNames.empty()) varNames = arg.name; | 2860 | if (varNames.empty()) varNames = arg.name; |
2864 | else varNames.append(s(", "sv) + arg.name); | 2861 | else varNames.append(", "s + arg.name); |
2865 | } | 2862 | } |
2866 | if (argDefList->varArg) { | 2863 | if (argDefList->varArg) { |
2867 | auto& arg = argItems.emplace_back(); | 2864 | auto& arg = argItems.emplace_back(); |
2868 | arg.name = "..."sv; | 2865 | arg.name = "..."sv; |
2869 | if (varNames.empty()) varNames = arg.name; | 2866 | if (varNames.empty()) varNames = arg.name; |
2870 | else varNames.append(s(", "sv) + arg.name); | 2867 | else varNames.append(", "s + arg.name); |
2871 | _varArgs.top() = true; | 2868 | _varArgs.top() = true; |
2872 | } | 2869 | } |
2873 | std::string initCodes = join(temp); | 2870 | std::string initCodes = join(temp); |
@@ -2877,13 +2874,13 @@ private: | |||
2877 | for (auto it = items.begin(); it != items.end(); ++it) { | 2874 | for (auto it = items.begin(); it != items.end(); ++it) { |
2878 | if (it->assignSelf.empty()) continue; | 2875 | if (it->assignSelf.empty()) continue; |
2879 | if (result.empty()) result = (&it->name)[index]; | 2876 | if (result.empty()) result = (&it->name)[index]; |
2880 | else result.append(s(", "sv) + (&it->name)[index]); | 2877 | else result.append(", "s + (&it->name)[index]); |
2881 | } | 2878 | } |
2882 | return result; | 2879 | return result; |
2883 | }; | 2880 | }; |
2884 | std::string sleft = sjoin(argItems, 1); | 2881 | std::string sleft = sjoin(argItems, 1); |
2885 | std::string sright = sjoin(argItems, 0); | 2882 | std::string sright = sjoin(argItems, 0); |
2886 | initCodes.append(indent() + sleft + s(" = "sv) + sright + nll(argDefList)); | 2883 | initCodes.append(indent() + sleft + " = "s + sright + nll(argDefList)); |
2887 | } | 2884 | } |
2888 | out.push_back(varNames); | 2885 | out.push_back(varNames); |
2889 | out.push_back(initCodes); | 2886 | out.push_back(initCodes); |
@@ -2897,7 +2894,7 @@ private: | |||
2897 | auto clsName = static_cast<self_class_name_t*>(name); | 2894 | auto clsName = static_cast<self_class_name_t*>(name); |
2898 | auto nameStr = _parser.toString(clsName->name); | 2895 | auto nameStr = _parser.toString(clsName->name); |
2899 | if (LuaKeywords.find(nameStr) != LuaKeywords.end()) { | 2896 | if (LuaKeywords.find(nameStr) != LuaKeywords.end()) { |
2900 | out.push_back(s("self.__class[\""sv) + nameStr + s("\"]"sv)); | 2897 | out.push_back("self.__class[\""s + nameStr + "\"]"s); |
2901 | if (invoke) { | 2898 | if (invoke) { |
2902 | if (auto invokePtr = invoke.as<Invoke_t>()) { | 2899 | if (auto invokePtr = invoke.as<Invoke_t>()) { |
2903 | invokePtr->args.push_front(toAst<Exp_t>("self.__class"sv, x)); | 2900 | invokePtr->args.push_front(toAst<Exp_t>("self.__class"sv, x)); |
@@ -2907,18 +2904,18 @@ private: | |||
2907 | } | 2904 | } |
2908 | } | 2905 | } |
2909 | } else { | 2906 | } else { |
2910 | out.push_back(s("self.__class"sv) + s(invoke ? ":"sv : "."sv) + nameStr); | 2907 | out.push_back("self.__class"s + (invoke ? ':' : '.') + nameStr); |
2911 | } | 2908 | } |
2912 | break; | 2909 | break; |
2913 | } | 2910 | } |
2914 | case id<self_class_t>(): | 2911 | case id<self_class_t>(): |
2915 | out.push_back(s("self.__class"sv)); | 2912 | out.push_back("self.__class"s); |
2916 | break; | 2913 | break; |
2917 | case id<self_name_t>(): { | 2914 | case id<self_name_t>(): { |
2918 | auto sfName = static_cast<self_class_name_t*>(name); | 2915 | auto sfName = static_cast<self_class_name_t*>(name); |
2919 | auto nameStr = _parser.toString(sfName->name); | 2916 | auto nameStr = _parser.toString(sfName->name); |
2920 | if (LuaKeywords.find(nameStr) != LuaKeywords.end()) { | 2917 | if (LuaKeywords.find(nameStr) != LuaKeywords.end()) { |
2921 | out.push_back(s("self[\""sv) + nameStr + s("\"]"sv)); | 2918 | out.push_back("self[\""s + nameStr + "\"]"s); |
2922 | if (invoke) { | 2919 | if (invoke) { |
2923 | if (auto invokePtr = invoke.as<Invoke_t>()) { | 2920 | if (auto invokePtr = invoke.as<Invoke_t>()) { |
2924 | invokePtr->args.push_front(toAst<Exp_t>("self"sv, x)); | 2921 | invokePtr->args.push_front(toAst<Exp_t>("self"sv, x)); |
@@ -2928,12 +2925,12 @@ private: | |||
2928 | } | 2925 | } |
2929 | } | 2926 | } |
2930 | } else { | 2927 | } else { |
2931 | out.push_back(s("self"sv) + s(invoke ? ":"sv : "."sv) + nameStr); | 2928 | out.push_back("self"s + (invoke ? ':' : '.') + nameStr); |
2932 | } | 2929 | } |
2933 | break; | 2930 | break; |
2934 | } | 2931 | } |
2935 | case id<self_t>(): | 2932 | case id<self_t>(): |
2936 | out.push_back(s("self"sv)); | 2933 | out.push_back("self"s); |
2937 | break; | 2934 | break; |
2938 | default: YUEE("AST node mismatch", name); break; | 2935 | default: YUEE("AST node mismatch", name); break; |
2939 | } | 2936 | } |
@@ -2973,7 +2970,7 @@ private: | |||
2973 | } | 2970 | } |
2974 | case ExpUsage::Return: | 2971 | case ExpUsage::Return: |
2975 | transformParens(parens, out); | 2972 | transformParens(parens, out); |
2976 | out.back().insert(0, indent() + s("return "sv)); | 2973 | out.back().insert(0, indent() + "return "s); |
2977 | out.back().append(nlr(x)); | 2974 | out.back().append(nlr(x)); |
2978 | break; | 2975 | break; |
2979 | default: | 2976 | default: |
@@ -2991,7 +2988,7 @@ private: | |||
2991 | auto x = chainList.front(); | 2988 | auto x = chainList.front(); |
2992 | str_list temp; | 2989 | str_list temp; |
2993 | if (usage == ExpUsage::Closure) { | 2990 | if (usage == ExpUsage::Closure) { |
2994 | temp.push_back(s("(function()"sv) + nll(x)); | 2991 | temp.push_back("(function()"s + nll(x)); |
2995 | pushScope(); | 2992 | pushScope(); |
2996 | _enableReturn.push(true); | 2993 | _enableReturn.push(true); |
2997 | } | 2994 | } |
@@ -3117,16 +3114,16 @@ private: | |||
3117 | } | 3114 | } |
3118 | } | 3115 | } |
3119 | popScope(); | 3116 | popScope(); |
3120 | temp.push_back(indent() + s("end"sv) + nlr(x)); | 3117 | temp.push_back(indent() + "end"s + nlr(x)); |
3121 | switch (usage) { | 3118 | switch (usage) { |
3122 | case ExpUsage::Return: | 3119 | case ExpUsage::Return: |
3123 | temp.push_back(indent() + s("return nil"sv) + nlr(x)); | 3120 | temp.push_back(indent() + "return nil"s + nlr(x)); |
3124 | break; | 3121 | break; |
3125 | case ExpUsage::Closure: | 3122 | case ExpUsage::Closure: |
3126 | temp.push_back(indent() + s("return nil"sv) + nlr(x)); | 3123 | temp.push_back(indent() + "return nil"s + nlr(x)); |
3127 | _enableReturn.pop(); | 3124 | _enableReturn.pop(); |
3128 | popScope(); | 3125 | popScope(); |
3129 | temp.push_back(indent() + s("end)()"sv)); | 3126 | temp.push_back(indent() + "end)()"s); |
3130 | break; | 3127 | break; |
3131 | default: | 3128 | default: |
3132 | break; | 3129 | break; |
@@ -3143,11 +3140,11 @@ private: | |||
3143 | str_list temp; | 3140 | str_list temp; |
3144 | switch (usage) { | 3141 | switch (usage) { |
3145 | case ExpUsage::Assignment: | 3142 | case ExpUsage::Assignment: |
3146 | temp.push_back(indent() + s("do"sv) + nll(x)); | 3143 | temp.push_back(indent() + "do"s + nll(x)); |
3147 | pushScope(); | 3144 | pushScope(); |
3148 | break; | 3145 | break; |
3149 | case ExpUsage::Closure: | 3146 | case ExpUsage::Closure: |
3150 | temp.push_back(s("(function()"sv) + nll(x)); | 3147 | temp.push_back("(function()"s + nll(x)); |
3151 | pushScope(); | 3148 | pushScope(); |
3152 | _enableReturn.push(true); | 3149 | _enableReturn.push(true); |
3153 | break; | 3150 | break; |
@@ -3192,7 +3189,7 @@ private: | |||
3192 | assignment->action.set(assign); | 3189 | assignment->action.set(assign); |
3193 | transformAssignment(assignment, temp); | 3190 | transformAssignment(assignment, temp); |
3194 | } | 3191 | } |
3195 | auto funLit = toAst<Exp_t>(fnVar + s(" and (...)-> "sv) + fnVar + s(" "sv) + baseVar + s(", ..."sv), x); | 3192 | auto funLit = toAst<Exp_t>(fnVar + " and (...)-> "s + fnVar + ' ' + baseVar + ", ..."s, x); |
3196 | switch (usage) { | 3193 | switch (usage) { |
3197 | case ExpUsage::Closure: | 3194 | case ExpUsage::Closure: |
3198 | case ExpUsage::Return: { | 3195 | case ExpUsage::Return: { |
@@ -3218,12 +3215,12 @@ private: | |||
3218 | switch (usage) { | 3215 | switch (usage) { |
3219 | case ExpUsage::Assignment: | 3216 | case ExpUsage::Assignment: |
3220 | popScope(); | 3217 | popScope(); |
3221 | temp.push_back(indent() + s("end"sv) + nlr(x)); | 3218 | temp.push_back(indent() + "end"s + nlr(x)); |
3222 | break; | 3219 | break; |
3223 | case ExpUsage::Closure: | 3220 | case ExpUsage::Closure: |
3224 | _enableReturn.pop(); | 3221 | _enableReturn.pop(); |
3225 | popScope(); | 3222 | popScope(); |
3226 | temp.push_back(indent() + s("end)()"sv)); | 3223 | temp.push_back(indent() + "end)()"s); |
3227 | break; | 3224 | break; |
3228 | default: | 3225 | default: |
3229 | break; | 3226 | break; |
@@ -3270,7 +3267,7 @@ private: | |||
3270 | case id<ColonChainItem_t>(): { | 3267 | case id<ColonChainItem_t>(): { |
3271 | auto colon = static_cast<ColonChainItem_t*>(*opIt); | 3268 | auto colon = static_cast<ColonChainItem_t*>(*opIt); |
3272 | auto meta = colon->name.to<Metamethod_t>(); | 3269 | auto meta = colon->name.to<Metamethod_t>(); |
3273 | auto newColon = toAst<ColonChainItem_t>(s("\\__"sv) + _parser.toString(meta->name), x); | 3270 | auto newColon = toAst<ColonChainItem_t>("\\__"s + _parser.toString(meta->name), x); |
3274 | chain->items.push_back(newColon); | 3271 | chain->items.push_back(newColon); |
3275 | break; | 3272 | break; |
3276 | } | 3273 | } |
@@ -3278,7 +3275,7 @@ private: | |||
3278 | auto dot = static_cast<DotChainItem_t*>(*opIt); | 3275 | auto dot = static_cast<DotChainItem_t*>(*opIt); |
3279 | if (dot->name.is<Metatable_t>()) break; | 3276 | if (dot->name.is<Metatable_t>()) break; |
3280 | auto meta = dot->name.to<Metamethod_t>(); | 3277 | auto meta = dot->name.to<Metamethod_t>(); |
3281 | auto newDot = toAst<DotChainItem_t>(s(".__"sv) + _parser.toString(meta->name), x); | 3278 | auto newDot = toAst<DotChainItem_t>(".__"s + _parser.toString(meta->name), x); |
3282 | chain->items.push_back(newDot); | 3279 | chain->items.push_back(newDot); |
3283 | break; | 3280 | break; |
3284 | } | 3281 | } |
@@ -3350,7 +3347,7 @@ private: | |||
3350 | auto exp = newExp(value, x); | 3347 | auto exp = newExp(value, x); |
3351 | callVar = singleVariableFrom(exp); | 3348 | callVar = singleVariableFrom(exp); |
3352 | if (callVar.empty()) { | 3349 | if (callVar.empty()) { |
3353 | callVar = getUnusedName(s("_call_"sv)); | 3350 | callVar = getUnusedName("_call_"s); |
3354 | auto assignment = x->new_ptr<ExpListAssign_t>(); | 3351 | auto assignment = x->new_ptr<ExpListAssign_t>(); |
3355 | assignment->expList.set(toAst<ExpList_t>(callVar, x)); | 3352 | assignment->expList.set(toAst<ExpList_t>(callVar, x)); |
3356 | auto assign = x->new_ptr<Assign_t>(); | 3353 | auto assign = x->new_ptr<Assign_t>(); |
@@ -3369,7 +3366,7 @@ private: | |||
3369 | if (ast_is<existential_op_t>(*current)) { | 3366 | if (ast_is<existential_op_t>(*current)) { |
3370 | chainValue->items.push_back(x->new_ptr<existential_op_t>()); | 3367 | chainValue->items.push_back(x->new_ptr<existential_op_t>()); |
3371 | } | 3368 | } |
3372 | chainValue->items.push_back(toAst<Exp_t>(s("\""sv) + name + s("\""sv), x)); | 3369 | chainValue->items.push_back(toAst<Exp_t>('\"' + name + '\"', x)); |
3373 | if (auto invoke = ast_cast<Invoke_t>(followItem)) { | 3370 | if (auto invoke = ast_cast<Invoke_t>(followItem)) { |
3374 | invoke->args.push_front(toAst<Exp_t>(callVar, x)); | 3371 | invoke->args.push_front(toAst<Exp_t>(callVar, x)); |
3375 | } else { | 3372 | } else { |
@@ -3444,11 +3441,11 @@ private: | |||
3444 | } | 3441 | } |
3445 | case id<String_t>(): | 3442 | case id<String_t>(): |
3446 | transformString(static_cast<String_t*>(item), temp); | 3443 | transformString(static_cast<String_t*>(item), temp); |
3447 | temp.back() = s("("sv) + temp.back() + s(")"sv); | 3444 | temp.back() = '(' + temp.back() + ')'; |
3448 | break; | 3445 | break; |
3449 | case id<Exp_t>(): | 3446 | case id<Exp_t>(): |
3450 | transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); | 3447 | transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); |
3451 | temp.back() = s(temp.back().front() == '[' ? "[ "sv : "["sv) + temp.back() + s("]"sv); | 3448 | temp.back() = (temp.back().front() == '[' ? "[ "s : "["s) + temp.back() + ']'; |
3452 | break; | 3449 | break; |
3453 | case id<InvokeArgs_t>(): transformInvokeArgs(static_cast<InvokeArgs_t*>(item), temp); break; | 3450 | case id<InvokeArgs_t>(): transformInvokeArgs(static_cast<InvokeArgs_t*>(item), temp); break; |
3454 | default: YUEE("AST node mismatch", item); break; | 3451 | default: YUEE("AST node mismatch", item); break; |
@@ -3459,7 +3456,7 @@ private: | |||
3459 | out.push_back(indent() + join(temp) + nll(x)); | 3456 | out.push_back(indent() + join(temp) + nll(x)); |
3460 | break; | 3457 | break; |
3461 | case ExpUsage::Return: | 3458 | case ExpUsage::Return: |
3462 | out.push_back(indent() + s("return "sv) + join(temp) + nll(x)); | 3459 | out.push_back(indent() + "return "s + join(temp) + nll(x)); |
3463 | break; | 3460 | break; |
3464 | case ExpUsage::Assignment: YUEE("invalid expression usage", x); break; | 3461 | case ExpUsage::Assignment: YUEE("invalid expression usage", x); break; |
3465 | default: | 3462 | default: |
@@ -3499,22 +3496,22 @@ private: | |||
3499 | pushOptions(args->back()->m_begin.m_line - 1); // loadstring codes chunk options | 3496 | pushOptions(args->back()->m_begin.m_line - 1); // loadstring codes chunk options |
3500 | if (lua_pcall(L, 3, 2, 0) != 0) { // loadstring(codes,chunk,options), f err | 3497 | if (lua_pcall(L, 3, 2, 0) != 0) { // loadstring(codes,chunk,options), f err |
3501 | std::string err = lua_tostring(L, -1); | 3498 | std::string err = lua_tostring(L, -1); |
3502 | throw std::logic_error(_info.errorMessage(s("failed to load macro codes\n"sv) + err, x)); | 3499 | throw std::logic_error(_info.errorMessage("failed to load macro codes\n"s + err, x)); |
3503 | } // f err | 3500 | } // f err |
3504 | if (lua_isnil(L, -2) != 0) { // f == nil, f err | 3501 | if (lua_isnil(L, -2) != 0) { // f == nil, f err |
3505 | std::string err = lua_tostring(L, -1); | 3502 | std::string err = lua_tostring(L, -1); |
3506 | throw std::logic_error(_info.errorMessage(s("failed to load macro codes, at (macro in-place): "sv) + err, x)); | 3503 | throw std::logic_error(_info.errorMessage("failed to load macro codes, at (macro in-place): "s + err, x)); |
3507 | } | 3504 | } |
3508 | lua_pop(L, 1); // f | 3505 | lua_pop(L, 1); // f |
3509 | pushYue("pcall"sv); // f pcall | 3506 | pushYue("pcall"sv); // f pcall |
3510 | lua_insert(L, -2); // pcall f | 3507 | lua_insert(L, -2); // pcall f |
3511 | if (lua_pcall(L, 1, 2, 0) != 0) { // f(), success macroFunc | 3508 | if (lua_pcall(L, 1, 2, 0) != 0) { // f(), success macroFunc |
3512 | std::string err = lua_tostring(L, -1); | 3509 | std::string err = lua_tostring(L, -1); |
3513 | throw std::logic_error(_info.errorMessage(s("failed to generate macro function\n"sv) + err, x)); | 3510 | throw std::logic_error(_info.errorMessage("failed to generate macro function\n"s + err, x)); |
3514 | } // success res | 3511 | } // success res |
3515 | if (lua_toboolean(L, -2) == 0) { | 3512 | if (lua_toboolean(L, -2) == 0) { |
3516 | std::string err = lua_tostring(L, -1); | 3513 | std::string err = lua_tostring(L, -1); |
3517 | throw std::logic_error(_info.errorMessage(s("failed to generate macro function\n"sv) + err, x)); | 3514 | throw std::logic_error(_info.errorMessage("failed to generate macro function\n"s + err, x)); |
3518 | } // true macroFunc | 3515 | } // true macroFunc |
3519 | lua_remove(L, -2); // macroFunc | 3516 | lua_remove(L, -2); // macroFunc |
3520 | pushYue("pcall"sv); // macroFunc pcall | 3517 | pushYue("pcall"sv); // macroFunc pcall |
@@ -3522,11 +3519,11 @@ private: | |||
3522 | bool success = lua_pcall(L, 1, 2, 0) == 0; | 3519 | bool success = lua_pcall(L, 1, 2, 0) == 0; |
3523 | if (!success) { // err | 3520 | if (!success) { // err |
3524 | std::string err = lua_tostring(L, -1); | 3521 | std::string err = lua_tostring(L, -1); |
3525 | throw std::logic_error(_info.errorMessage(s("failed to expand macro: "sv) + err, x)); | 3522 | throw std::logic_error(_info.errorMessage("failed to expand macro: "s + err, x)); |
3526 | } // success err | 3523 | } // success err |
3527 | if (lua_toboolean(L, -2) == 0) { | 3524 | if (lua_toboolean(L, -2) == 0) { |
3528 | std::string err = lua_tostring(L, -1); | 3525 | std::string err = lua_tostring(L, -1); |
3529 | throw std::logic_error(_info.errorMessage(s("failed to expand macro: "sv) + err, x)); | 3526 | throw std::logic_error(_info.errorMessage("failed to expand macro: "s + err, x)); |
3530 | } | 3527 | } |
3531 | return {Empty, Empty, {}}; | 3528 | return {Empty, Empty, {}}; |
3532 | } | 3529 | } |
@@ -3589,15 +3586,15 @@ private: | |||
3589 | bool success = lua_pcall(L, static_cast<int>(args->size()) + 1, 2, 0) == 0; | 3586 | bool success = lua_pcall(L, static_cast<int>(args->size()) + 1, 2, 0) == 0; |
3590 | if (!success) { // cur err | 3587 | if (!success) { // cur err |
3591 | std::string err = lua_tostring(L, -1); | 3588 | std::string err = lua_tostring(L, -1); |
3592 | throw std::logic_error(_info.errorMessage(s("failed to expand macro: "sv) + err, x)); | 3589 | throw std::logic_error(_info.errorMessage("failed to expand macro: "s + err, x)); |
3593 | } // cur success res | 3590 | } // cur success res |
3594 | if (lua_toboolean(L, -2) == 0) { | 3591 | if (lua_toboolean(L, -2) == 0) { |
3595 | std::string err = lua_tostring(L, -1); | 3592 | std::string err = lua_tostring(L, -1); |
3596 | throw std::logic_error(_info.errorMessage(s("failed to expand macro: "sv) + err, x)); | 3593 | throw std::logic_error(_info.errorMessage("failed to expand macro: "s + err, x)); |
3597 | } | 3594 | } |
3598 | lua_remove(L, -2); // cur res | 3595 | lua_remove(L, -2); // cur res |
3599 | if (lua_isstring(L, -1) == 0 && lua_istable(L, -1) == 0) { | 3596 | if (lua_isstring(L, -1) == 0 && lua_istable(L, -1) == 0) { |
3600 | throw std::logic_error(_info.errorMessage(s("macro function must return string or table"sv), x)); | 3597 | throw std::logic_error(_info.errorMessage("macro function must return string or table"sv, x)); |
3601 | } // cur res | 3598 | } // cur res |
3602 | std::string codes; | 3599 | std::string codes; |
3603 | std::string type; | 3600 | std::string type; |
@@ -3607,7 +3604,7 @@ private: | |||
3607 | if (lua_isstring(L, -1) != 0) { | 3604 | if (lua_isstring(L, -1) != 0) { |
3608 | codes = lua_tostring(L, -1); | 3605 | codes = lua_tostring(L, -1); |
3609 | } else { | 3606 | } else { |
3610 | throw std::logic_error(_info.errorMessage(s("macro table must contain field \"codes\" of string"sv), x)); | 3607 | throw std::logic_error(_info.errorMessage("macro table must contain field \"codes\" of string"sv, x)); |
3611 | } | 3608 | } |
3612 | lua_pop(L, 1); // cur res | 3609 | lua_pop(L, 1); // cur res |
3613 | lua_getfield(L, -1, "type"); // cur res type | 3610 | lua_getfield(L, -1, "type"); // cur res type |
@@ -3615,7 +3612,7 @@ private: | |||
3615 | type = lua_tostring(L, -1); | 3612 | type = lua_tostring(L, -1); |
3616 | } | 3613 | } |
3617 | if (type != "lua"sv && type != "text"sv) { | 3614 | if (type != "lua"sv && type != "text"sv) { |
3618 | throw std::logic_error(_info.errorMessage(s("macro table must contain field \"type\" of value \"lua\" or \"text\""sv), x)); | 3615 | throw std::logic_error(_info.errorMessage("macro table must contain field \"type\" of value \"lua\" or \"text\""sv, x)); |
3619 | } | 3616 | } |
3620 | lua_pop(L, 1); // cur res | 3617 | lua_pop(L, 1); // cur res |
3621 | lua_getfield(L, -1, "locals"); // cur res locals | 3618 | lua_getfield(L, -1, "locals"); // cur res locals |
@@ -3624,13 +3621,13 @@ private: | |||
3624 | lua_rawgeti(L, -1, i + 1); // cur res locals item | 3621 | lua_rawgeti(L, -1, i + 1); // cur res locals item |
3625 | size_t len = 0; | 3622 | size_t len = 0; |
3626 | if (lua_isstring(L, -1) == 0) { | 3623 | if (lua_isstring(L, -1) == 0) { |
3627 | throw std::logic_error(_info.errorMessage(s("macro table field \"locals\" must be a table of strings"sv), x)); | 3624 | throw std::logic_error(_info.errorMessage("macro table field \"locals\" must be a table of strings"sv, x)); |
3628 | } | 3625 | } |
3629 | auto name = lua_tolstring(L, -1, &len); | 3626 | auto name = lua_tolstring(L, -1, &len); |
3630 | if (_parser.match<Variable_t>({name, len})) { | 3627 | if (_parser.match<Variable_t>({name, len})) { |
3631 | localVars.push_back(std::string(name, len)); | 3628 | localVars.push_back(std::string(name, len)); |
3632 | } else { | 3629 | } else { |
3633 | throw std::logic_error(_info.errorMessage(s("macro table field \"locals\" must contain names for local variables, got \""sv) + std::string(name, len) + '"', x)); | 3630 | throw std::logic_error(_info.errorMessage("macro table field \"locals\" must contain names for local variables, got \""s + std::string(name, len) + '"', x)); |
3634 | } | 3631 | } |
3635 | lua_pop(L, 1); | 3632 | lua_pop(L, 1); |
3636 | } | 3633 | } |
@@ -3654,7 +3651,7 @@ private: | |||
3654 | if (!isBlock) { | 3651 | if (!isBlock) { |
3655 | throw std::logic_error(_info.errorMessage("lua macro can only be placed where block macro is allowed"sv, x)); | 3652 | throw std::logic_error(_info.errorMessage("lua macro can only be placed where block macro is allowed"sv, x)); |
3656 | } | 3653 | } |
3657 | auto macroChunk = s("=(macro "sv) + _parser.toString(x->name) + ')'; | 3654 | auto macroChunk = "=(macro "s + _parser.toString(x->name) + ')'; |
3658 | int top = lua_gettop(L); | 3655 | int top = lua_gettop(L); |
3659 | DEFER(lua_settop(L, top)); | 3656 | DEFER(lua_settop(L, top)); |
3660 | if (luaL_loadbuffer(L, codes.c_str(), codes.size(), macroChunk.c_str()) != 0) { | 3657 | if (luaL_loadbuffer(L, codes.c_str(), codes.size(), macroChunk.c_str()) != 0) { |
@@ -3673,7 +3670,7 @@ private: | |||
3673 | info = _parser.parse<BlockEnd_t>(codes); | 3670 | info = _parser.parse<BlockEnd_t>(codes); |
3674 | if (!info.node) { | 3671 | if (!info.node) { |
3675 | info.error = info.error.substr(info.error.find(':') + 2); | 3672 | info.error = info.error.substr(info.error.find(':') + 2); |
3676 | throw std::logic_error(_info.errorMessage(s("failed to expand macro as block: "sv) + info.error, x)); | 3673 | throw std::logic_error(_info.errorMessage("failed to expand macro as block: "s + info.error, x)); |
3677 | } | 3674 | } |
3678 | } else { | 3675 | } else { |
3679 | info = _parser.parse<Exp_t>(codes); | 3676 | info = _parser.parse<Exp_t>(codes); |
@@ -3681,12 +3678,12 @@ private: | |||
3681 | info = _parser.parse<BlockEnd_t>(codes); | 3678 | info = _parser.parse<BlockEnd_t>(codes); |
3682 | if (!info.node) { | 3679 | if (!info.node) { |
3683 | info.error = info.error.substr(info.error.find(':') + 2); | 3680 | info.error = info.error.substr(info.error.find(':') + 2); |
3684 | throw std::logic_error(_info.errorMessage(s("failed to expand macro as expr or block: "sv) + info.error, x)); | 3681 | throw std::logic_error(_info.errorMessage("failed to expand macro as expr or block: "s + info.error, x)); |
3685 | } | 3682 | } |
3686 | isBlock = true; | 3683 | isBlock = true; |
3687 | } else if (!info.node) { | 3684 | } else if (!info.node) { |
3688 | info.error = info.error.substr(info.error.find(':') + 2); | 3685 | info.error = info.error.substr(info.error.find(':') + 2); |
3689 | throw std::logic_error(_info.errorMessage(s("failed to expand macro as expr: "sv) + info.error, x)); | 3686 | throw std::logic_error(_info.errorMessage("failed to expand macro as expr: "s + info.error, x)); |
3690 | } | 3687 | } |
3691 | } | 3688 | } |
3692 | int line = x->m_begin.m_line; | 3689 | int line = x->m_begin.m_line; |
@@ -3738,18 +3735,18 @@ private: | |||
3738 | auto stmt = static_cast<Statement_t*>(stmt_); | 3735 | auto stmt = static_cast<Statement_t*>(stmt_); |
3739 | if (auto global = stmt->content.as<Global_t>()) { | 3736 | if (auto global = stmt->content.as<Global_t>()) { |
3740 | if (global->item.is<global_op_t>()) { | 3737 | if (global->item.is<global_op_t>()) { |
3741 | throw std::logic_error(_info.errorMessage(s("can not insert global statement with wildcard operator from macro"sv), x)); | 3738 | throw std::logic_error(_info.errorMessage("can not insert global statement with wildcard operator from macro"sv, x)); |
3742 | } | 3739 | } |
3743 | } else if (auto local = stmt->content.as<Local_t>()) { | 3740 | } else if (auto local = stmt->content.as<Local_t>()) { |
3744 | if (local->item.is<local_flag_t>()) { | 3741 | if (local->item.is<local_flag_t>()) { |
3745 | throw std::logic_error(_info.errorMessage(s("can not insert local statement with wildcard operator from macro"sv), x)); | 3742 | throw std::logic_error(_info.errorMessage("can not insert local statement with wildcard operator from macro"sv, x)); |
3746 | } | 3743 | } |
3747 | } | 3744 | } |
3748 | } | 3745 | } |
3749 | } | 3746 | } |
3750 | return {info.node, std::move(info.codes), Empty, std::move(localVars)}; | 3747 | return {info.node, std::move(info.codes), Empty, std::move(localVars)}; |
3751 | } else { | 3748 | } else { |
3752 | if (!isBlock) throw std::logic_error(_info.errorMessage(s("failed to expand empty macro as expr"sv), x)); | 3749 | if (!isBlock) throw std::logic_error(_info.errorMessage("failed to expand empty macro as expr"sv, x)); |
3753 | return {x->new_ptr<Block_t>().get(), std::move(info.codes), Empty, std::move(localVars)}; | 3750 | return {x->new_ptr<Block_t>().get(), std::move(info.codes), Empty, std::move(localVars)}; |
3754 | } | 3751 | } |
3755 | } | 3752 | } |
@@ -3837,15 +3834,15 @@ private: | |||
3837 | void transformDotChainItem(DotChainItem_t* dotChainItem, str_list& out) { | 3834 | void transformDotChainItem(DotChainItem_t* dotChainItem, str_list& out) { |
3838 | auto name = _parser.toString(dotChainItem->name); | 3835 | auto name = _parser.toString(dotChainItem->name); |
3839 | if (Keywords.find(name) != Keywords.end()) { | 3836 | if (Keywords.find(name) != Keywords.end()) { |
3840 | out.push_back(s("[\""sv) + name + s("\"]"sv)); | 3837 | out.push_back("[\""s + name + "\"]"s); |
3841 | } else { | 3838 | } else { |
3842 | out.push_back(s("."sv) + name); | 3839 | out.push_back('.' + name); |
3843 | } | 3840 | } |
3844 | } | 3841 | } |
3845 | 3842 | ||
3846 | void transformColonChainItem(ColonChainItem_t* colonChainItem, str_list& out) { | 3843 | void transformColonChainItem(ColonChainItem_t* colonChainItem, str_list& out) { |
3847 | auto name = _parser.toString(colonChainItem->name); | 3844 | auto name = _parser.toString(colonChainItem->name); |
3848 | out.push_back(s(colonChainItem->switchToDot ? "."sv : ":"sv) + name); | 3845 | out.push_back((colonChainItem->switchToDot ? '.' : ':') + name); |
3849 | } | 3846 | } |
3850 | 3847 | ||
3851 | void transformSlice(Slice_t* slice, str_list&) { | 3848 | void transformSlice(Slice_t* slice, str_list&) { |
@@ -3864,14 +3861,14 @@ private: | |||
3864 | default: YUEE("AST node mismatch", arg); break; | 3861 | default: YUEE("AST node mismatch", arg); break; |
3865 | } | 3862 | } |
3866 | } | 3863 | } |
3867 | out.push_back(s("("sv) + join(temp, ", "sv) + s(")"sv)); | 3864 | out.push_back('(' + join(temp, ", "sv) + ')'); |
3868 | } | 3865 | } |
3869 | 3866 | ||
3870 | void transform_unary_value(unary_value_t* unary_value, str_list& out) { | 3867 | void transform_unary_value(unary_value_t* unary_value, str_list& out) { |
3871 | str_list temp; | 3868 | str_list temp; |
3872 | for (auto _op : unary_value->ops.objects()) { | 3869 | for (auto _op : unary_value->ops.objects()) { |
3873 | std::string op = _parser.toString(_op); | 3870 | std::string op = _parser.toString(_op); |
3874 | temp.push_back(op + (op == "not"sv ? s(" "sv) : Empty)); | 3871 | temp.push_back(op == "not"sv ? op + ' ' : op); |
3875 | } | 3872 | } |
3876 | transformValue(unary_value->value, temp); | 3873 | transformValue(unary_value->value, temp); |
3877 | out.push_back(join(temp)); | 3874 | out.push_back(join(temp)); |
@@ -3885,7 +3882,7 @@ private: | |||
3885 | std::string unary_op; | 3882 | std::string unary_op; |
3886 | for (auto _op : unary_exp->ops.objects()) { | 3883 | for (auto _op : unary_exp->ops.objects()) { |
3887 | std::string op = _parser.toString(_op); | 3884 | std::string op = _parser.toString(_op); |
3888 | unary_op.append(op + (op == "not"sv ? s(" "sv) : Empty)); | 3885 | unary_op.append(op == "not"sv ? op + ' ' : op); |
3889 | } | 3886 | } |
3890 | str_list temp; | 3887 | str_list temp; |
3891 | for (auto _value : unary_exp->expos.objects()) { | 3888 | for (auto _value : unary_exp->expos.objects()) { |
@@ -3921,7 +3918,7 @@ private: | |||
3921 | break; | 3918 | break; |
3922 | case id<Exp_t>(): | 3919 | case id<Exp_t>(): |
3923 | transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); | 3920 | transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); |
3924 | temp.back() = indent() + s("if "sv) + temp.back() + s(" then"sv) + nll(item); | 3921 | temp.back() = indent() + "if "s + temp.back() + " then"s + nll(item); |
3925 | pushScope(); | 3922 | pushScope(); |
3926 | break; | 3923 | break; |
3927 | default: YUEE("AST node mismatch", item); break; | 3924 | default: YUEE("AST node mismatch", item); break; |
@@ -3977,14 +3974,14 @@ private: | |||
3977 | break; | 3974 | break; |
3978 | case id<Exp_t>(): | 3975 | case id<Exp_t>(): |
3979 | transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); | 3976 | transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); |
3980 | temp.back() = indent() + s("if "sv) + temp.back() + s(" then"sv) + nll(item); | 3977 | temp.back() = indent() + "if "s + temp.back() + " then"s + nll(item); |
3981 | pushScope(); | 3978 | pushScope(); |
3982 | break; | 3979 | break; |
3983 | default: YUEE("AST node mismatch", item); break; | 3980 | default: YUEE("AST node mismatch", item); break; |
3984 | } | 3981 | } |
3985 | } | 3982 | } |
3986 | { | 3983 | { |
3987 | auto assignLeft = toAst<ExpList_t>(accumVar + s("["sv) + lenVar + s("]"sv), x); | 3984 | auto assignLeft = toAst<ExpList_t>(accumVar + '[' + lenVar + ']', x); |
3988 | auto assign = x->new_ptr<Assign_t>(); | 3985 | auto assign = x->new_ptr<Assign_t>(); |
3989 | assign->values.push_back(comp->value); | 3986 | assign->values.push_back(comp->value); |
3990 | auto assignment = x->new_ptr<ExpListAssign_t>(); | 3987 | auto assignment = x->new_ptr<ExpListAssign_t>(); |
@@ -4011,10 +4008,10 @@ private: | |||
4011 | case ExpUsage::Closure: { | 4008 | case ExpUsage::Closure: { |
4012 | _enableReturn.pop(); | 4009 | _enableReturn.pop(); |
4013 | out.push_back(clearBuf()); | 4010 | out.push_back(clearBuf()); |
4014 | out.back().append(indent() + s("return "sv) + accumVar + nlr(comp)); | 4011 | out.back().append(indent() + "return "s + accumVar + nlr(comp)); |
4015 | popScope(); | 4012 | popScope(); |
4016 | out.back().insert(0, s("(function()"sv) + nll(comp)); | 4013 | out.back().insert(0, "(function()"s + nll(comp)); |
4017 | out.back().append(indent() + s("end)()"sv)); | 4014 | out.back().append(indent() + "end)()"s); |
4018 | break; | 4015 | break; |
4019 | } | 4016 | } |
4020 | case ExpUsage::Assignment: { | 4017 | case ExpUsage::Assignment: { |
@@ -4026,14 +4023,14 @@ private: | |||
4026 | assignment->action.set(assign); | 4023 | assignment->action.set(assign); |
4027 | transformAssignment(assignment, temp); | 4024 | transformAssignment(assignment, temp); |
4028 | popScope(); | 4025 | popScope(); |
4029 | out.back() = indent() + s("do"sv) + nll(comp) + | 4026 | out.back() = indent() + "do"s + nll(comp) + |
4030 | out.back() + temp.back() + | 4027 | out.back() + temp.back() + |
4031 | indent() + s("end"sv) + nlr(comp); | 4028 | indent() + "end"s + nlr(comp); |
4032 | break; | 4029 | break; |
4033 | } | 4030 | } |
4034 | case ExpUsage::Return: | 4031 | case ExpUsage::Return: |
4035 | out.push_back(clearBuf()); | 4032 | out.push_back(clearBuf()); |
4036 | out.back().append(indent() + s("return "sv) + accumVar + nlr(comp)); | 4033 | out.back().append(indent() + "return "s + accumVar + nlr(comp)); |
4037 | break; | 4034 | break; |
4038 | default: | 4035 | default: |
4039 | break; | 4036 | break; |
@@ -4231,7 +4228,7 @@ private: | |||
4231 | default: YUEE("AST node mismatch", arg); break; | 4228 | default: YUEE("AST node mismatch", arg); break; |
4232 | } | 4229 | } |
4233 | } | 4230 | } |
4234 | out.push_back(s("("sv) + join(temp, ", "sv) + s(")"sv)); | 4231 | out.push_back('(' + join(temp, ", "sv) + ')'); |
4235 | } | 4232 | } |
4236 | 4233 | ||
4237 | void transformForHead(For_t* forNode, str_list& out) { | 4234 | void transformForHead(For_t* forNode, str_list& out) { |
@@ -4248,7 +4245,7 @@ private: | |||
4248 | const auto& start = *it; | 4245 | const auto& start = *it; |
4249 | const auto& stop = *(++it); | 4246 | const auto& stop = *(++it); |
4250 | const auto& step = *(++it); | 4247 | const auto& step = *(++it); |
4251 | _buf << indent() << "for "sv << varName << " = "sv << start << ", "sv << stop << (step.empty() ? Empty : s(", "sv) + step) << " do"sv << nll(forNode); | 4248 | _buf << indent() << "for "sv << varName << " = "sv << start << ", "sv << stop << (step.empty() ? Empty : ", "s + step) << " do"sv << nll(forNode); |
4252 | pushScope(); | 4249 | pushScope(); |
4253 | forceAddToScope(varName); | 4250 | forceAddToScope(varName); |
4254 | out.push_back(clearBuf()); | 4251 | out.push_back(clearBuf()); |
@@ -4316,7 +4313,7 @@ private: | |||
4316 | transformForHead(forNode, temp); | 4313 | transformForHead(forNode, temp); |
4317 | transformLoopBody(forNode->body, temp, Empty, ExpUsage::Common); | 4314 | transformLoopBody(forNode->body, temp, Empty, ExpUsage::Common); |
4318 | popScope(); | 4315 | popScope(); |
4319 | out.push_back(join(temp) + indent() + s("end"sv) + nlr(forNode)); | 4316 | out.push_back(join(temp) + indent() + "end"s + nlr(forNode)); |
4320 | } | 4317 | } |
4321 | 4318 | ||
4322 | std::string transformForInner(For_t* forNode, str_list& out) { | 4319 | std::string transformForInner(For_t* forNode, str_list& out) { |
@@ -4329,11 +4326,11 @@ private: | |||
4329 | _buf << indent() << "local "sv << len << " = 1"sv << nll(forNode); | 4326 | _buf << indent() << "local "sv << len << " = 1"sv << nll(forNode); |
4330 | out.push_back(clearBuf()); | 4327 | out.push_back(clearBuf()); |
4331 | transformForHead(forNode, out); | 4328 | transformForHead(forNode, out); |
4332 | auto expList = toAst<ExpList_t>(accum + s("["sv) + len + s("]"sv), x); | 4329 | auto expList = toAst<ExpList_t>(accum + '[' + len + ']', x); |
4333 | auto lenLine = len + s(" = "sv) + len + s(" + 1"sv) + nlr(forNode->body); | 4330 | auto lenLine = len + " = "s + len + " + 1"s + nlr(forNode->body); |
4334 | transformLoopBody(forNode->body, out, lenLine, ExpUsage::Assignment, expList); | 4331 | transformLoopBody(forNode->body, out, lenLine, ExpUsage::Assignment, expList); |
4335 | popScope(); | 4332 | popScope(); |
4336 | out.push_back(indent() + s("end"sv) + nlr(forNode)); | 4333 | out.push_back(indent() + "end"s + nlr(forNode)); |
4337 | return accum; | 4334 | return accum; |
4338 | } | 4335 | } |
4339 | 4336 | ||
@@ -4343,10 +4340,10 @@ private: | |||
4343 | pushScope(); | 4340 | pushScope(); |
4344 | _enableReturn.push(true); | 4341 | _enableReturn.push(true); |
4345 | auto accum = transformForInner(forNode, temp); | 4342 | auto accum = transformForInner(forNode, temp); |
4346 | temp.push_back(indent() + s("return "sv) + accum + nlr(forNode)); | 4343 | temp.push_back(indent() + "return "s + accum + nlr(forNode)); |
4347 | _enableReturn.pop(); | 4344 | _enableReturn.pop(); |
4348 | popScope(); | 4345 | popScope(); |
4349 | temp.push_back(indent() + s("end)()"sv)); | 4346 | temp.push_back(indent() + "end)()"s); |
4350 | out.push_back(join(temp)); | 4347 | out.push_back(join(temp)); |
4351 | } | 4348 | } |
4352 | 4349 | ||
@@ -4364,7 +4361,7 @@ private: | |||
4364 | assignment->action.set(assign); | 4361 | assignment->action.set(assign); |
4365 | transformAssignment(assignment, temp); | 4362 | transformAssignment(assignment, temp); |
4366 | popScope(); | 4363 | popScope(); |
4367 | temp.push_back(indent() + s("end"sv) + nlr(forNode)); | 4364 | temp.push_back(indent() + "end"s + nlr(forNode)); |
4368 | } else { | 4365 | } else { |
4369 | auto accum = transformForInner(forNode, temp); | 4366 | auto accum = transformForInner(forNode, temp); |
4370 | auto returnNode = x->new_ptr<Return_t>(); | 4367 | auto returnNode = x->new_ptr<Return_t>(); |
@@ -4377,7 +4374,7 @@ private: | |||
4377 | 4374 | ||
4378 | void transformBinaryOperator(BinaryOperator_t* node, str_list& out) { | 4375 | void transformBinaryOperator(BinaryOperator_t* node, str_list& out) { |
4379 | auto op = _parser.toString(node); | 4376 | auto op = _parser.toString(node); |
4380 | out.push_back(op == "!="sv ? s("~="sv) : op); | 4377 | out.push_back(op == "!="sv ? "~="s : op); |
4381 | } | 4378 | } |
4382 | 4379 | ||
4383 | void transformForEach(ForEach_t* forEach, str_list& out) { | 4380 | void transformForEach(ForEach_t* forEach, str_list& out) { |
@@ -4385,7 +4382,7 @@ private: | |||
4385 | transformForEachHead(forEach->nameList, forEach->loopValue, temp); | 4382 | transformForEachHead(forEach->nameList, forEach->loopValue, temp); |
4386 | transformLoopBody(forEach->body, temp, Empty, ExpUsage::Common); | 4383 | transformLoopBody(forEach->body, temp, Empty, ExpUsage::Common); |
4387 | popScope(); | 4384 | popScope(); |
4388 | out.push_back(temp.front() + temp.back() + indent() + s("end"sv) + nlr(forEach)); | 4385 | out.push_back(temp.front() + temp.back() + indent() + "end"s + nlr(forEach)); |
4389 | } | 4386 | } |
4390 | 4387 | ||
4391 | std::string transformForEachInner(ForEach_t* forEach, str_list& out) { | 4388 | std::string transformForEachInner(ForEach_t* forEach, str_list& out) { |
@@ -4398,11 +4395,11 @@ private: | |||
4398 | _buf << indent() << "local "sv << len << " = 1"sv << nll(forEach); | 4395 | _buf << indent() << "local "sv << len << " = 1"sv << nll(forEach); |
4399 | out.push_back(clearBuf()); | 4396 | out.push_back(clearBuf()); |
4400 | transformForEachHead(forEach->nameList, forEach->loopValue, out); | 4397 | transformForEachHead(forEach->nameList, forEach->loopValue, out); |
4401 | auto expList = toAst<ExpList_t>(accum + s("["sv) + len + s("]"sv), x); | 4398 | auto expList = toAst<ExpList_t>(accum + '[' + len + ']', x); |
4402 | auto lenLine = len + s(" = "sv) + len + s(" + 1"sv) + nlr(forEach->body); | 4399 | auto lenLine = len + " = "s + len + " + 1"s + nlr(forEach->body); |
4403 | transformLoopBody(forEach->body, out, lenLine, ExpUsage::Assignment, expList); | 4400 | transformLoopBody(forEach->body, out, lenLine, ExpUsage::Assignment, expList); |
4404 | popScope(); | 4401 | popScope(); |
4405 | out.push_back(indent() + s("end"sv) + nlr(forEach)); | 4402 | out.push_back(indent() + "end"s + nlr(forEach)); |
4406 | return accum; | 4403 | return accum; |
4407 | } | 4404 | } |
4408 | 4405 | ||
@@ -4412,10 +4409,10 @@ private: | |||
4412 | pushScope(); | 4409 | pushScope(); |
4413 | _enableReturn.push(true); | 4410 | _enableReturn.push(true); |
4414 | auto accum = transformForEachInner(forEach, temp); | 4411 | auto accum = transformForEachInner(forEach, temp); |
4415 | temp.push_back(indent() + s("return "sv) + accum + nlr(forEach)); | 4412 | temp.push_back(indent() + "return "s + accum + nlr(forEach)); |
4416 | _enableReturn.pop(); | 4413 | _enableReturn.pop(); |
4417 | popScope(); | 4414 | popScope(); |
4418 | temp.push_back(indent() + s("end)()"sv)); | 4415 | temp.push_back(indent() + "end)()"s); |
4419 | out.push_back(join(temp)); | 4416 | out.push_back(join(temp)); |
4420 | } | 4417 | } |
4421 | 4418 | ||
@@ -4433,7 +4430,7 @@ private: | |||
4433 | assignment->action.set(assign); | 4430 | assignment->action.set(assign); |
4434 | transformAssignment(assignment, temp); | 4431 | transformAssignment(assignment, temp); |
4435 | popScope(); | 4432 | popScope(); |
4436 | temp.push_back(indent() + s("end"sv) + nlr(forEach)); | 4433 | temp.push_back(indent() + "end"s + nlr(forEach)); |
4437 | } else { | 4434 | } else { |
4438 | auto accum = transformForEachInner(forEach, temp); | 4435 | auto accum = transformForEachInner(forEach, temp); |
4439 | auto returnNode = x->new_ptr<Return_t>(); | 4436 | auto returnNode = x->new_ptr<Return_t>(); |
@@ -4451,7 +4448,7 @@ private: | |||
4451 | _globals[name] = {pair->name->m_begin.m_line, pair->name->m_begin.m_col}; | 4448 | _globals[name] = {pair->name->m_begin.m_line, pair->name->m_begin.m_col}; |
4452 | } | 4449 | } |
4453 | } | 4450 | } |
4454 | out.push_back(name + s(" = "sv) + name); | 4451 | out.push_back(name + " = "s + name); |
4455 | } | 4452 | } |
4456 | 4453 | ||
4457 | void transform_normal_pair(normal_pair_t* pair, str_list& out) { | 4454 | void transform_normal_pair(normal_pair_t* pair, str_list& out) { |
@@ -4461,23 +4458,23 @@ private: | |||
4461 | case id<KeyName_t>(): { | 4458 | case id<KeyName_t>(): { |
4462 | transformKeyName(static_cast<KeyName_t*>(key), temp); | 4459 | transformKeyName(static_cast<KeyName_t*>(key), temp); |
4463 | if (LuaKeywords.find(temp.back()) != LuaKeywords.end()) { | 4460 | if (LuaKeywords.find(temp.back()) != LuaKeywords.end()) { |
4464 | temp.back() = s("[\""sv) + temp.back() + s("\"]"); | 4461 | temp.back() = "[\""s + temp.back() + "\"]"s; |
4465 | } | 4462 | } |
4466 | break; | 4463 | break; |
4467 | } | 4464 | } |
4468 | case id<Exp_t>(): | 4465 | case id<Exp_t>(): |
4469 | transformExp(static_cast<Exp_t*>(key), temp, ExpUsage::Closure); | 4466 | transformExp(static_cast<Exp_t*>(key), temp, ExpUsage::Closure); |
4470 | temp.back() = s(temp.back().front() == '[' ? "[ "sv : "["sv) + temp.back() + s("]"sv); | 4467 | temp.back() = (temp.back().front() == '[' ? "[ "s : "["s) + temp.back() + ']'; |
4471 | break; | 4468 | break; |
4472 | case id<DoubleString_t>(): | 4469 | case id<DoubleString_t>(): |
4473 | transformDoubleString(static_cast<DoubleString_t*>(key), temp); | 4470 | transformDoubleString(static_cast<DoubleString_t*>(key), temp); |
4474 | temp.back() = s("["sv) + temp.back() + s("]"sv); | 4471 | temp.back() = '[' + temp.back() + ']'; |
4475 | break; | 4472 | break; |
4476 | case id<SingleString_t>(): transformSingleString(static_cast<SingleString_t*>(key), temp); | 4473 | case id<SingleString_t>(): transformSingleString(static_cast<SingleString_t*>(key), temp); |
4477 | temp.back() = s("["sv) + temp.back() + s("]"sv); | 4474 | temp.back() = '[' + temp.back() + ']'; |
4478 | break; | 4475 | break; |
4479 | case id<LuaString_t>(): transformLuaString(static_cast<LuaString_t*>(key), temp); | 4476 | case id<LuaString_t>(): transformLuaString(static_cast<LuaString_t*>(key), temp); |
4480 | temp.back() = s("[ "sv) + temp.back() + s("]"sv); | 4477 | temp.back() = "[ "s + temp.back() + ']'; |
4481 | break; | 4478 | break; |
4482 | default: YUEE("AST node mismatch", key); break; | 4479 | default: YUEE("AST node mismatch", key); break; |
4483 | } | 4480 | } |
@@ -4487,7 +4484,7 @@ private: | |||
4487 | case id<TableBlock_t>(): transformTableBlock(static_cast<TableBlock_t*>(value), temp); break; | 4484 | case id<TableBlock_t>(): transformTableBlock(static_cast<TableBlock_t*>(value), temp); break; |
4488 | default: YUEE("AST node mismatch", value); break; | 4485 | default: YUEE("AST node mismatch", value); break; |
4489 | } | 4486 | } |
4490 | out.push_back(temp.front() + s(" = "sv) + temp.back()); | 4487 | out.push_back(temp.front() + " = "s + temp.back()); |
4491 | } | 4488 | } |
4492 | 4489 | ||
4493 | void transformKeyName(KeyName_t* keyName, str_list& out) { | 4490 | void transformKeyName(KeyName_t* keyName, str_list& out) { |
@@ -4522,18 +4519,18 @@ private: | |||
4522 | auto str = _parser.toString(content); | 4519 | auto str = _parser.toString(content); |
4523 | Utils::replace(str, "\r\n"sv, "\n"); | 4520 | Utils::replace(str, "\r\n"sv, "\n"); |
4524 | Utils::replace(str, "\n"sv, "\\n"sv); | 4521 | Utils::replace(str, "\n"sv, "\\n"sv); |
4525 | temp.push_back(s("\""sv) + str + s("\""sv)); | 4522 | temp.push_back('\"' + str + '\"'); |
4526 | break; | 4523 | break; |
4527 | } | 4524 | } |
4528 | case id<Exp_t>(): { | 4525 | case id<Exp_t>(): { |
4529 | transformExp(static_cast<Exp_t*>(content), temp, ExpUsage::Closure); | 4526 | transformExp(static_cast<Exp_t*>(content), temp, ExpUsage::Closure); |
4530 | temp.back() = globalVar("tostring"sv, content) + '(' + temp.back() + s(")"sv); | 4527 | temp.back() = globalVar("tostring"sv, content) + '(' + temp.back() + ')'; |
4531 | break; | 4528 | break; |
4532 | } | 4529 | } |
4533 | default: YUEE("AST node mismatch", content); break; | 4530 | default: YUEE("AST node mismatch", content); break; |
4534 | } | 4531 | } |
4535 | } | 4532 | } |
4536 | out.push_back(temp.empty() ? s("\"\""sv) : join(temp, " .. "sv)); | 4533 | out.push_back(temp.empty() ? "\"\""s : join(temp, " .. "sv)); |
4537 | } | 4534 | } |
4538 | 4535 | ||
4539 | void transformString(String_t* string, str_list& out) { | 4536 | void transformString(String_t* string, str_list& out) { |
@@ -4560,13 +4557,13 @@ private: | |||
4560 | 4557 | ||
4561 | void transformClassDeclClosure(ClassDecl_t* classDecl, str_list& out) { | 4558 | void transformClassDeclClosure(ClassDecl_t* classDecl, str_list& out) { |
4562 | str_list temp; | 4559 | str_list temp; |
4563 | temp.push_back(s("(function()"sv) + nll(classDecl)); | 4560 | temp.push_back("(function()"s + nll(classDecl)); |
4564 | pushScope(); | 4561 | pushScope(); |
4565 | _enableReturn.push(true); | 4562 | _enableReturn.push(true); |
4566 | transformClassDecl(classDecl, temp, ExpUsage::Return); | 4563 | transformClassDecl(classDecl, temp, ExpUsage::Return); |
4567 | _enableReturn.pop(); | 4564 | _enableReturn.pop(); |
4568 | popScope(); | 4565 | popScope(); |
4569 | temp.push_back(s("end)()"sv)); | 4566 | temp.push_back("end)()"s); |
4570 | out.push_back(join(temp)); | 4567 | out.push_back(join(temp)); |
4571 | } | 4568 | } |
4572 | 4569 | ||
@@ -4585,12 +4582,12 @@ private: | |||
4585 | bool newDefined = false; | 4582 | bool newDefined = false; |
4586 | std::tie(className, newDefined) = defineClassVariable(assignable); | 4583 | std::tie(className, newDefined) = defineClassVariable(assignable); |
4587 | if (newDefined) { | 4584 | if (newDefined) { |
4588 | temp.push_back(indent() + s("local "sv) + className + nll(classDecl)); | 4585 | temp.push_back(indent() + "local "s + className + nll(classDecl)); |
4589 | } | 4586 | } |
4590 | if (className.empty()) { | 4587 | if (className.empty()) { |
4591 | if (auto chain = ast_cast<AssignableChain_t>(assignable->item)) { | 4588 | if (auto chain = ast_cast<AssignableChain_t>(assignable->item)) { |
4592 | if (auto dotChain = ast_cast<DotChainItem_t>(chain->items.back())) { | 4589 | if (auto dotChain = ast_cast<DotChainItem_t>(chain->items.back())) { |
4593 | className = s("\""sv) + _parser.toString(dotChain->name) + s("\""sv); | 4590 | className = '\"' + _parser.toString(dotChain->name) + '\"'; |
4594 | } else if (auto index = ast_cast<Exp_t>(chain->items.back())) { | 4591 | } else if (auto index = ast_cast<Exp_t>(chain->items.back())) { |
4595 | if (auto name = index->getByPath<unary_exp_t, Value_t, String_t>()) { | 4592 | if (auto name = index->getByPath<unary_exp_t, Value_t, String_t>()) { |
4596 | transformString(name, temp); | 4593 | transformString(name, temp); |
@@ -4600,7 +4597,7 @@ private: | |||
4600 | } | 4597 | } |
4601 | } | 4598 | } |
4602 | } else { | 4599 | } else { |
4603 | className = s("\""sv) + className + s("\""sv); | 4600 | className = '\"' + className + '\"'; |
4604 | } | 4601 | } |
4605 | pushScope(); | 4602 | pushScope(); |
4606 | transformAssignable(assignable, temp); | 4603 | transformAssignable(assignable, temp); |
@@ -4610,14 +4607,14 @@ private: | |||
4610 | } else if (expList) { | 4607 | } else if (expList) { |
4611 | auto name = singleVariableFrom(expList); | 4608 | auto name = singleVariableFrom(expList); |
4612 | if (!name.empty()) { | 4609 | if (!name.empty()) { |
4613 | className = s("\""sv) + name + s("\""sv); | 4610 | className = '\"' + name + '\"'; |
4614 | } | 4611 | } |
4615 | } | 4612 | } |
4616 | temp.push_back(indent() + s("do"sv) + nll(classDecl)); | 4613 | temp.push_back(indent() + "do"s + nll(classDecl)); |
4617 | pushScope(); | 4614 | pushScope(); |
4618 | auto classVar = getUnusedName("_class_"sv); | 4615 | auto classVar = getUnusedName("_class_"sv); |
4619 | addToScope(classVar); | 4616 | addToScope(classVar); |
4620 | temp.push_back(indent() + s("local "sv) + classVar + nll(classDecl)); | 4617 | temp.push_back(indent() + "local "s + classVar + nll(classDecl)); |
4621 | if (body) { | 4618 | if (body) { |
4622 | str_list varDefs; | 4619 | str_list varDefs; |
4623 | for (auto item : body->contents.objects()) { | 4620 | for (auto item : body->contents.objects()) { |
@@ -4655,7 +4652,7 @@ private: | |||
4655 | } | 4652 | } |
4656 | } | 4653 | } |
4657 | if (!varDefs.empty()) { | 4654 | if (!varDefs.empty()) { |
4658 | temp.push_back(indent() + s("local "sv) + join(varDefs, ", "sv) + nll(body)); | 4655 | temp.push_back(indent() + "local "s + join(varDefs, ", "sv) + nll(body)); |
4659 | } | 4656 | } |
4660 | } | 4657 | } |
4661 | std::string parent, parentVar; | 4658 | std::string parent, parentVar; |
@@ -4665,11 +4662,11 @@ private: | |||
4665 | transformExp(extend, temp, ExpUsage::Closure); | 4662 | transformExp(extend, temp, ExpUsage::Closure); |
4666 | parent = temp.back(); | 4663 | parent = temp.back(); |
4667 | temp.pop_back(); | 4664 | temp.pop_back(); |
4668 | temp.push_back(indent() + s("local "sv) + parentVar + s(" = "sv) + parent + nll(classDecl)); | 4665 | temp.push_back(indent() + "local "s + parentVar + " = "s + parent + nll(classDecl)); |
4669 | } | 4666 | } |
4670 | auto baseVar = getUnusedName("_base_"sv); | 4667 | auto baseVar = getUnusedName("_base_"sv); |
4671 | addToScope(baseVar); | 4668 | addToScope(baseVar); |
4672 | temp.push_back(indent() + s("local "sv) + baseVar + s(" = "sv)); | 4669 | temp.push_back(indent() + "local "s + baseVar + " = "s); |
4673 | str_list builtins; | 4670 | str_list builtins; |
4674 | str_list commons; | 4671 | str_list commons; |
4675 | str_list statements; | 4672 | str_list statements; |
@@ -4700,25 +4697,25 @@ private: | |||
4700 | for (auto& member : members) { | 4697 | for (auto& member : members) { |
4701 | switch (member.type) { | 4698 | switch (member.type) { |
4702 | case MemType::Common: | 4699 | case MemType::Common: |
4703 | commons.push_back((commons.empty() ? Empty : s(","sv) + nll(member.node)) + member.item); | 4700 | commons.push_back((commons.empty() ? Empty : ',' + nll(member.node)) + member.item); |
4704 | break; | 4701 | break; |
4705 | case MemType::Builtin: | 4702 | case MemType::Builtin: |
4706 | builtins.push_back((builtins.empty() ? Empty : s(","sv) + nll(member.node)) + member.item); | 4703 | builtins.push_back((builtins.empty() ? Empty : ',' + nll(member.node)) + member.item); |
4707 | break; | 4704 | break; |
4708 | default: break; | 4705 | default: break; |
4709 | } | 4706 | } |
4710 | } | 4707 | } |
4711 | if (!commons.empty()) { | 4708 | if (!commons.empty()) { |
4712 | temp.back() += s("{"sv) + nll(body); | 4709 | temp.back() += '{' + nll(body); |
4713 | temp.push_back(join(commons) + nll(body)); | 4710 | temp.push_back(join(commons) + nll(body)); |
4714 | temp.push_back(indent() + s("}"sv) + nll(body)); | 4711 | temp.push_back(indent() + '}' + nll(body)); |
4715 | } else { | 4712 | } else { |
4716 | temp.back() += s("{ }"sv) + nll(body); | 4713 | temp.back() += "{ }"s + nll(body); |
4717 | } | 4714 | } |
4718 | } else { | 4715 | } else { |
4719 | temp.back() += s("{ }"sv) + nll(classDecl); | 4716 | temp.back() += "{ }"s + nll(classDecl); |
4720 | } | 4717 | } |
4721 | temp.push_back(indent() + baseVar + s(".__index = "sv) + baseVar + nll(classDecl)); | 4718 | temp.push_back(indent() + baseVar + ".__index = "s + baseVar + nll(classDecl)); |
4722 | str_list tmp; | 4719 | str_list tmp; |
4723 | if (usage == ExpUsage::Assignment) { | 4720 | if (usage == ExpUsage::Assignment) { |
4724 | auto assign = x->new_ptr<Assign_t>(); | 4721 | auto assign = x->new_ptr<Assign_t>(); |
@@ -4805,7 +4802,7 @@ private: | |||
4805 | } | 4802 | } |
4806 | temp.push_back(clearBuf()); | 4803 | temp.push_back(clearBuf()); |
4807 | popScope(); | 4804 | popScope(); |
4808 | temp.push_back(indent() + s("end"sv) + nlr(classDecl)); | 4805 | temp.push_back(indent() + "end"s + nlr(classDecl)); |
4809 | out.push_back(join(temp)); | 4806 | out.push_back(join(temp)); |
4810 | } | 4807 | } |
4811 | 4808 | ||
@@ -4825,7 +4822,7 @@ private: | |||
4825 | type = MemType::Property; | 4822 | type = MemType::Property; |
4826 | auto name = ast_cast<self_name_t>(selfName->name); | 4823 | auto name = ast_cast<self_name_t>(selfName->name); |
4827 | if (!name) throw std::logic_error(_info.errorMessage("invalid class poperty name"sv, selfName->name)); | 4824 | if (!name) throw std::logic_error(_info.errorMessage("invalid class poperty name"sv, selfName->name)); |
4828 | newSuperCall = classVar + s(".__parent."sv) + _parser.toString(name->name); | 4825 | newSuperCall = classVar + ".__parent."s + _parser.toString(name->name); |
4829 | } else { | 4826 | } else { |
4830 | auto x = keyName; | 4827 | auto x = keyName; |
4831 | auto nameNode = keyName->name.as<Name_t>(); | 4828 | auto nameNode = keyName->name.as<Name_t>(); |
@@ -4834,9 +4831,9 @@ private: | |||
4834 | if (name == "new"sv) { | 4831 | if (name == "new"sv) { |
4835 | type = MemType::Builtin; | 4832 | type = MemType::Builtin; |
4836 | keyName->name.set(toAst<Name_t>("__init"sv, x)); | 4833 | keyName->name.set(toAst<Name_t>("__init"sv, x)); |
4837 | newSuperCall = classVar + s(".__parent.__init"sv); | 4834 | newSuperCall = classVar + ".__parent.__init"s; |
4838 | } else { | 4835 | } else { |
4839 | newSuperCall = classVar + s(".__parent.__base."sv) + name; | 4836 | newSuperCall = classVar + ".__parent.__base."s + name; |
4840 | } | 4837 | } |
4841 | } | 4838 | } |
4842 | normal_pair->value->traverse([&](ast_node* node) { | 4839 | normal_pair->value->traverse([&](ast_node* node) { |
@@ -4872,10 +4869,10 @@ private: | |||
4872 | colonChainItem->switchToDot = true; | 4869 | colonChainItem->switchToDot = true; |
4873 | } | 4870 | } |
4874 | } | 4871 | } |
4875 | newSuperCall = classVar + s(".__parent"sv); | 4872 | newSuperCall = classVar + ".__parent"s; |
4876 | } | 4873 | } |
4877 | } else { | 4874 | } else { |
4878 | newSuperCall = classVar + s(".__parent"sv); | 4875 | newSuperCall = classVar + ".__parent"s; |
4879 | } | 4876 | } |
4880 | auto newChain = toAst<ChainValue_t>(newSuperCall, chainValue); | 4877 | auto newChain = toAst<ChainValue_t>(newSuperCall, chainValue); |
4881 | chainValue->items.pop_front(); | 4878 | chainValue->items.pop_front(); |
@@ -4925,13 +4922,13 @@ private: | |||
4925 | 4922 | ||
4926 | void transformWithClosure(With_t* with, str_list& out) { | 4923 | void transformWithClosure(With_t* with, str_list& out) { |
4927 | str_list temp; | 4924 | str_list temp; |
4928 | temp.push_back(s("(function()"sv) + nll(with)); | 4925 | temp.push_back("(function()"s + nll(with)); |
4929 | pushScope(); | 4926 | pushScope(); |
4930 | _enableReturn.push(true); | 4927 | _enableReturn.push(true); |
4931 | transformWith(with, temp, nullptr, true); | 4928 | transformWith(with, temp, nullptr, true); |
4932 | _enableReturn.pop(); | 4929 | _enableReturn.pop(); |
4933 | popScope(); | 4930 | popScope(); |
4934 | temp.push_back(indent() + s("end)()"sv)); | 4931 | temp.push_back(indent() + "end)()"s); |
4935 | out.push_back(join(temp)); | 4932 | out.push_back(join(temp)); |
4936 | } | 4933 | } |
4937 | 4934 | ||
@@ -4958,7 +4955,7 @@ private: | |||
4958 | assignment->action.set(assign); | 4955 | assignment->action.set(assign); |
4959 | if (!returnValue) { | 4956 | if (!returnValue) { |
4960 | scoped = true; | 4957 | scoped = true; |
4961 | temp.push_back(indent() + s("do"sv) + nll(with)); | 4958 | temp.push_back(indent() + "do"s + nll(with)); |
4962 | pushScope(); | 4959 | pushScope(); |
4963 | } | 4960 | } |
4964 | transformAssignment(assignment, temp); | 4961 | transformAssignment(assignment, temp); |
@@ -4984,7 +4981,7 @@ private: | |||
4984 | assignment->action.set(with->assigns); | 4981 | assignment->action.set(with->assigns); |
4985 | if (!returnValue) { | 4982 | if (!returnValue) { |
4986 | scoped = true; | 4983 | scoped = true; |
4987 | temp.push_back(indent() + s("do"sv) + nll(with)); | 4984 | temp.push_back(indent() + "do"s + nll(with)); |
4988 | pushScope(); | 4985 | pushScope(); |
4989 | } | 4986 | } |
4990 | transformAssignment(assignment, temp); | 4987 | transformAssignment(assignment, temp); |
@@ -5000,7 +4997,7 @@ private: | |||
5000 | assignment->action.set(assign); | 4997 | assignment->action.set(assign); |
5001 | if (!returnValue) { | 4998 | if (!returnValue) { |
5002 | scoped = true; | 4999 | scoped = true; |
5003 | temp.push_back(indent() + s("do"sv) + nll(with)); | 5000 | temp.push_back(indent() + "do"s + nll(with)); |
5004 | pushScope(); | 5001 | pushScope(); |
5005 | } | 5002 | } |
5006 | transformAssignment(assignment, temp); | 5003 | transformAssignment(assignment, temp); |
@@ -5047,7 +5044,7 @@ private: | |||
5047 | }); | 5044 | }); |
5048 | popScope(); | 5045 | popScope(); |
5049 | if (scoped) { | 5046 | if (scoped) { |
5050 | temp.push_back(indent() + s("do"sv) + nll(with)); | 5047 | temp.push_back(indent() + "do"s + nll(with)); |
5051 | pushScope(); | 5048 | pushScope(); |
5052 | } | 5049 | } |
5053 | } | 5050 | } |
@@ -5055,7 +5052,7 @@ private: | |||
5055 | if (with->eop) { | 5052 | if (with->eop) { |
5056 | auto ifNode = x->new_ptr<If_t>(); | 5053 | auto ifNode = x->new_ptr<If_t>(); |
5057 | ifNode->type.set(toAst<IfType_t>("if"sv, x)); | 5054 | ifNode->type.set(toAst<IfType_t>("if"sv, x)); |
5058 | ifNode->nodes.push_back(toAst<IfCond_t>(withVar + s("~=nil"sv), x)); | 5055 | ifNode->nodes.push_back(toAst<IfCond_t>(withVar + "~=nil"s, x)); |
5059 | ifNode->nodes.push_back(with->body); | 5056 | ifNode->nodes.push_back(with->body); |
5060 | transformIf(ifNode, temp, ExpUsage::Common); | 5057 | transformIf(ifNode, temp, ExpUsage::Common); |
5061 | } else { | 5058 | } else { |
@@ -5073,12 +5070,12 @@ private: | |||
5073 | if (returnValue) { | 5070 | if (returnValue) { |
5074 | auto last = lastStatementFrom(with->body); | 5071 | auto last = lastStatementFrom(with->body); |
5075 | if (last && !last->content.is<Return_t>()) { | 5072 | if (last && !last->content.is<Return_t>()) { |
5076 | temp.push_back(indent() + s("return "sv) + withVar + nll(with)); | 5073 | temp.push_back(indent() + "return "s + withVar + nll(with)); |
5077 | } | 5074 | } |
5078 | } | 5075 | } |
5079 | if (scoped) { | 5076 | if (scoped) { |
5080 | popScope(); | 5077 | popScope(); |
5081 | temp.push_back(indent() + s("end"sv) + nll(with)); | 5078 | temp.push_back(indent() + "end"s + nll(with)); |
5082 | } | 5079 | } |
5083 | out.push_back(join(temp)); | 5080 | out.push_back(join(temp)); |
5084 | } | 5081 | } |
@@ -5176,14 +5173,14 @@ private: | |||
5176 | names.push_back(item.name); | 5173 | names.push_back(item.name); |
5177 | } | 5174 | } |
5178 | if (_info.exportDefault) { | 5175 | if (_info.exportDefault) { |
5179 | out.back().append(indent() + _info.moduleName + s(" = "sv) + names.back() + nlr(exportNode)); | 5176 | out.back().append(indent() + _info.moduleName + " = "s + names.back() + nlr(exportNode)); |
5180 | } else { | 5177 | } else { |
5181 | str_list lefts, rights; | 5178 | str_list lefts, rights; |
5182 | for (const auto& name : names) { | 5179 | for (const auto& name : names) { |
5183 | lefts.push_back(_info.moduleName + s("[\""sv) + name + s("\"]"sv)); | 5180 | lefts.push_back(_info.moduleName + "[\""s + name + "\"]"s); |
5184 | rights.push_back(name); | 5181 | rights.push_back(name); |
5185 | } | 5182 | } |
5186 | out.back().append(indent() + join(lefts,", "sv) + s(" = "sv) + join(rights, ", "sv) + nlr(exportNode)); | 5183 | out.back().append(indent() + join(lefts, ", "sv) + " = "s + join(rights, ", "sv) + nlr(exportNode)); |
5187 | } | 5184 | } |
5188 | } else { | 5185 | } else { |
5189 | if (auto macro = exportNode->target.as<Macro_t>()) { | 5186 | if (auto macro = exportNode->target.as<Macro_t>()) { |
@@ -5200,14 +5197,14 @@ private: | |||
5200 | str_list temp; | 5197 | str_list temp; |
5201 | auto expList = exportNode->target.to<ExpList_t>(); | 5198 | auto expList = exportNode->target.to<ExpList_t>(); |
5202 | auto assignment = x->new_ptr<ExpListAssign_t>(); | 5199 | auto assignment = x->new_ptr<ExpListAssign_t>(); |
5203 | auto assignList = toAst<ExpList_t>(_info.moduleName + s("[#"sv) + _info.moduleName + s("+1]"sv), x); | 5200 | auto assignList = toAst<ExpList_t>(_info.moduleName + "[#"s + _info.moduleName + "+1]"s, x); |
5204 | assignment->expList.set(assignList); | 5201 | assignment->expList.set(assignList); |
5205 | for (auto exp : expList->exprs.objects()) { | 5202 | for (auto exp : expList->exprs.objects()) { |
5206 | if (auto classDecl = exp->getByPath<unary_exp_t, Value_t, SimpleValue_t, ClassDecl_t>()) { | 5203 | if (auto classDecl = exp->getByPath<unary_exp_t, Value_t, SimpleValue_t, ClassDecl_t>()) { |
5207 | if (classDecl->name && classDecl->name->item->getId() == id<Variable_t>()) { | 5204 | if (classDecl->name && classDecl->name->item->getId() == id<Variable_t>()) { |
5208 | transformClassDecl(classDecl, temp, ExpUsage::Common); | 5205 | transformClassDecl(classDecl, temp, ExpUsage::Common); |
5209 | auto name = _parser.toString(classDecl->name->item); | 5206 | auto name = _parser.toString(classDecl->name->item); |
5210 | assignment->expList.set(toAst<ExpList_t>(_info.moduleName + s("[\""sv) + name + s("\"]"sv), x)); | 5207 | assignment->expList.set(toAst<ExpList_t>(_info.moduleName + "[\""s + name + "\"]"s, x)); |
5211 | auto assign = x->new_ptr<Assign_t>(); | 5208 | auto assign = x->new_ptr<Assign_t>(); |
5212 | assign->values.push_back(toAst<Exp_t>(name, x)); | 5209 | assign->values.push_back(toAst<Exp_t>(name, x)); |
5213 | assignment->action.set(assign); | 5210 | assignment->action.set(assign); |
@@ -5228,7 +5225,7 @@ private: | |||
5228 | 5225 | ||
5229 | void transformTable(ast_node* table, const node_container& pairs, str_list& out) { | 5226 | void transformTable(ast_node* table, const node_container& pairs, str_list& out) { |
5230 | if (pairs.empty()) { | 5227 | if (pairs.empty()) { |
5231 | out.push_back(s("{ }"sv)); | 5228 | out.push_back("{ }"s); |
5232 | return; | 5229 | return; |
5233 | } | 5230 | } |
5234 | str_list temp; | 5231 | str_list temp; |
@@ -5289,13 +5286,13 @@ private: | |||
5289 | default: YUEE("AST node mismatch", pair); break; | 5286 | default: YUEE("AST node mismatch", pair); break; |
5290 | } | 5287 | } |
5291 | if (!isMetamethod) { | 5288 | if (!isMetamethod) { |
5292 | temp.back() = indent() + temp.back() + (pair == pairs.back() ? Empty : s(","sv)) + nll(pair); | 5289 | temp.back() = indent() + (pair == pairs.back() ? temp.back() : temp.back() + ',') + nll(pair); |
5293 | } | 5290 | } |
5294 | } | 5291 | } |
5295 | if (metatable->pairs.empty() && !metatableItem) { | 5292 | if (metatable->pairs.empty() && !metatableItem) { |
5296 | out.push_back(s("{"sv) + nll(table) + join(temp)); | 5293 | out.push_back('{' + nll(table) + join(temp)); |
5297 | decIndentOffset(); | 5294 | decIndentOffset(); |
5298 | out.back() += (indent() + s("}"sv)); | 5295 | out.back() += (indent() + '}'); |
5299 | } else { | 5296 | } else { |
5300 | auto tabStr = globalVar("setmetatable"sv, table); | 5297 | auto tabStr = globalVar("setmetatable"sv, table); |
5301 | tabStr += '('; | 5298 | tabStr += '('; |
@@ -5303,9 +5300,9 @@ private: | |||
5303 | decIndentOffset(); | 5300 | decIndentOffset(); |
5304 | tabStr += "{ }"sv; | 5301 | tabStr += "{ }"sv; |
5305 | } else { | 5302 | } else { |
5306 | tabStr += (s("{"sv) + nll(table) + join(temp)); | 5303 | tabStr += ('{' + nll(table) + join(temp)); |
5307 | decIndentOffset(); | 5304 | decIndentOffset(); |
5308 | tabStr += (indent() + s("}"sv)); | 5305 | tabStr += (indent() + '}'); |
5309 | } | 5306 | } |
5310 | tabStr += ", "sv; | 5307 | tabStr += ", "sv; |
5311 | str_list tmp; | 5308 | str_list tmp; |
@@ -5320,7 +5317,7 @@ private: | |||
5320 | break; | 5317 | break; |
5321 | } | 5318 | } |
5322 | tabStr += tmp.back(); | 5319 | tabStr += tmp.back(); |
5323 | tabStr += s(")"sv); | 5320 | tabStr += ')'; |
5324 | out.push_back(tabStr); | 5321 | out.push_back(tabStr); |
5325 | } | 5322 | } |
5326 | } | 5323 | } |
@@ -5357,7 +5354,7 @@ private: | |||
5357 | break; | 5354 | break; |
5358 | case id<Exp_t>(): | 5355 | case id<Exp_t>(): |
5359 | transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); | 5356 | transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); |
5360 | temp.back() = indent() + s("if "sv) + temp.back() + s(" then"sv) + nll(item); | 5357 | temp.back() = indent() + "if "s + temp.back() + " then"s + nll(item); |
5361 | pushScope(); | 5358 | pushScope(); |
5362 | break; | 5359 | break; |
5363 | default: YUEE("AST node mismatch", item); break; | 5360 | default: YUEE("AST node mismatch", item); break; |
@@ -5388,11 +5385,11 @@ private: | |||
5388 | _buf << indent() << "end"sv << nll(comp); | 5385 | _buf << indent() << "end"sv << nll(comp); |
5389 | switch (usage) { | 5386 | switch (usage) { |
5390 | case ExpUsage::Closure: | 5387 | case ExpUsage::Closure: |
5391 | out.push_back(clearBuf() + indent() + s("return "sv) + tbl + nlr(comp)); | 5388 | out.push_back(clearBuf() + indent() + "return "s + tbl + nlr(comp)); |
5392 | popScope(); | 5389 | popScope(); |
5393 | _enableReturn.pop(); | 5390 | _enableReturn.pop(); |
5394 | out.back().insert(0, s("(function()"sv) + nll(comp)); | 5391 | out.back().insert(0, "(function()"s + nll(comp)); |
5395 | out.back().append(indent() + s("end)()"sv)); | 5392 | out.back().append(indent() + "end)()"s); |
5396 | break; | 5393 | break; |
5397 | case ExpUsage::Assignment: { | 5394 | case ExpUsage::Assignment: { |
5398 | out.push_back(clearBuf()); | 5395 | out.push_back(clearBuf()); |
@@ -5404,12 +5401,12 @@ private: | |||
5404 | transformAssignment(assignment, temp); | 5401 | transformAssignment(assignment, temp); |
5405 | out.back().append(temp.back()); | 5402 | out.back().append(temp.back()); |
5406 | popScope(); | 5403 | popScope(); |
5407 | out.back().insert(0, indent() + s("do"sv) + nll(comp)); | 5404 | out.back().insert(0, indent() + "do"s + nll(comp)); |
5408 | out.back().append(indent() + s("end"sv) + nlr(comp)); | 5405 | out.back().append(indent() + "end"s + nlr(comp)); |
5409 | break; | 5406 | break; |
5410 | } | 5407 | } |
5411 | case ExpUsage::Return: | 5408 | case ExpUsage::Return: |
5412 | out.push_back(clearBuf() + indent() + s("return "sv) + tbl + nlr(comp)); | 5409 | out.push_back(clearBuf() + indent() + "return "s + tbl + nlr(comp)); |
5413 | break; | 5410 | break; |
5414 | default: | 5411 | default: |
5415 | break; | 5412 | break; |
@@ -5430,7 +5427,7 @@ private: | |||
5430 | const auto& start = *it; | 5427 | const auto& start = *it; |
5431 | const auto& stop = *(++it); | 5428 | const auto& stop = *(++it); |
5432 | const auto& step = *(++it); | 5429 | const auto& step = *(++it); |
5433 | _buf << indent() << "for "sv << varName << " = "sv << start << ", "sv << stop << (step.empty() ? Empty : s(", "sv) + step) << " do"sv << nll(comp); | 5430 | _buf << indent() << "for "sv << varName << " = "sv << start << ", "sv << stop << (step.empty() ? Empty : ", "s + step) << " do"sv << nll(comp); |
5434 | out.push_back(clearBuf()); | 5431 | out.push_back(clearBuf()); |
5435 | pushScope(); | 5432 | pushScope(); |
5436 | forceAddToScope(varName); | 5433 | forceAddToScope(varName); |
@@ -5447,19 +5444,19 @@ private: | |||
5447 | void transformDo(Do_t* doNode, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { | 5444 | void transformDo(Do_t* doNode, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { |
5448 | str_list temp; | 5445 | str_list temp; |
5449 | if (usage == ExpUsage::Closure) { | 5446 | if (usage == ExpUsage::Closure) { |
5450 | temp.push_back(s("(function()"sv) + nll(doNode)); | 5447 | temp.push_back("(function()"s + nll(doNode)); |
5451 | _enableReturn.push(true); | 5448 | _enableReturn.push(true); |
5452 | } else { | 5449 | } else { |
5453 | temp.push_back(indent() + s("do"sv) + nll(doNode)); | 5450 | temp.push_back(indent() + "do"s + nll(doNode)); |
5454 | } | 5451 | } |
5455 | pushScope(); | 5452 | pushScope(); |
5456 | transformBody(doNode->body, temp, usage, assignList); | 5453 | transformBody(doNode->body, temp, usage, assignList); |
5457 | popScope(); | 5454 | popScope(); |
5458 | if (usage == ExpUsage::Closure) { | 5455 | if (usage == ExpUsage::Closure) { |
5459 | _enableReturn.pop(); | 5456 | _enableReturn.pop(); |
5460 | temp.push_back(indent() + s("end)()"sv)); | 5457 | temp.push_back(indent() + "end)()"s); |
5461 | } else { | 5458 | } else { |
5462 | temp.push_back(indent() + s("end"sv) + nlr(doNode)); | 5459 | temp.push_back(indent() + "end"s + nlr(doNode)); |
5463 | } | 5460 | } |
5464 | out.push_back(join(temp)); | 5461 | out.push_back(join(temp)); |
5465 | } | 5462 | } |
@@ -5540,7 +5537,7 @@ private: | |||
5540 | if (!preDef.empty()) { | 5537 | if (!preDef.empty()) { |
5541 | temp.push_back(preDef + nll(import)); | 5538 | temp.push_back(preDef + nll(import)); |
5542 | } | 5539 | } |
5543 | temp.push_back(indent() + s("do"sv) + nll(import)); | 5540 | temp.push_back(indent() + "do"s + nll(import)); |
5544 | pushScope(); | 5541 | pushScope(); |
5545 | transformAssignment(objAssign, temp); | 5542 | transformAssignment(objAssign, temp); |
5546 | } | 5543 | } |
@@ -5550,7 +5547,7 @@ private: | |||
5550 | transformAssignment(assignment, temp); | 5547 | transformAssignment(assignment, temp); |
5551 | if (objAssign) { | 5548 | if (objAssign) { |
5552 | popScope(); | 5549 | popScope(); |
5553 | temp.push_back(indent() + s("end"sv) + nlr(import)); | 5550 | temp.push_back(indent() + "end"s + nlr(import)); |
5554 | } | 5551 | } |
5555 | out.push_back(join(temp)); | 5552 | out.push_back(join(temp)); |
5556 | } | 5553 | } |
@@ -5587,7 +5584,7 @@ private: | |||
5587 | break; | 5584 | break; |
5588 | } | 5585 | } |
5589 | case id<import_all_macro_t>(): | 5586 | case id<import_all_macro_t>(): |
5590 | if (importAllMacro) throw std::logic_error(_info.errorMessage(s("import all macro symbol duplicated"sv), item)); | 5587 | if (importAllMacro) throw std::logic_error(_info.errorMessage("import all macro symbol duplicated"sv, item)); |
5591 | importAllMacro = true; | 5588 | importAllMacro = true; |
5592 | break; | 5589 | break; |
5593 | case id<variable_pair_t>(): | 5590 | case id<variable_pair_t>(): |
@@ -5612,10 +5609,10 @@ private: | |||
5612 | lua_pushlstring(L, moduleName.c_str(), moduleName.size()); // cur find_modulepath moduleName | 5609 | lua_pushlstring(L, moduleName.c_str(), moduleName.size()); // cur find_modulepath moduleName |
5613 | if (lua_pcall(L, 1, 1, 0) != 0) { | 5610 | if (lua_pcall(L, 1, 1, 0) != 0) { |
5614 | std::string err = lua_tostring(L, -1); | 5611 | std::string err = lua_tostring(L, -1); |
5615 | throw std::logic_error(_info.errorMessage(s("failed to resolve module path\n"sv) + err, x)); | 5612 | throw std::logic_error(_info.errorMessage("failed to resolve module path\n"s + err, x)); |
5616 | } | 5613 | } |
5617 | if (lua_isnil(L, -1) != 0) { | 5614 | if (lua_isnil(L, -1) != 0) { |
5618 | throw std::logic_error(_info.errorMessage(s("failed to find module '"sv) + moduleName + '\'', x)); | 5615 | throw std::logic_error(_info.errorMessage("failed to find module '"s + moduleName + '\'', x)); |
5619 | } | 5616 | } |
5620 | std::string moduleFullName = lua_tostring(L, -1); | 5617 | std::string moduleFullName = lua_tostring(L, -1); |
5621 | lua_pop(L, 1); // cur | 5618 | lua_pop(L, 1); // cur |
@@ -5624,7 +5621,7 @@ private: | |||
5624 | lua_pushlstring(L, moduleFullName.c_str(), moduleFullName.size()); // cur load_text moduleFullName | 5621 | lua_pushlstring(L, moduleFullName.c_str(), moduleFullName.size()); // cur load_text moduleFullName |
5625 | if (lua_pcall(L, 1, 1, 0) != 0) { | 5622 | if (lua_pcall(L, 1, 1, 0) != 0) { |
5626 | std::string err = lua_tostring(L, -1); | 5623 | std::string err = lua_tostring(L, -1); |
5627 | throw std::logic_error(_info.errorMessage(s("failed to read module file\n"sv) + err, x)); | 5624 | throw std::logic_error(_info.errorMessage("failed to read module file\n"s + err, x)); |
5628 | } // cur text | 5625 | } // cur text |
5629 | if (lua_isnil(L, -1) != 0) { | 5626 | if (lua_isnil(L, -1) != 0) { |
5630 | throw std::logic_error(_info.errorMessage("failed to get module text"sv, x)); | 5627 | throw std::logic_error(_info.errorMessage("failed to get module text"sv, x)); |
@@ -5638,7 +5635,7 @@ private: | |||
5638 | config.implicitReturnRoot = _config.implicitReturnRoot; | 5635 | config.implicitReturnRoot = _config.implicitReturnRoot; |
5639 | auto result = compiler.compile(text, config); | 5636 | auto result = compiler.compile(text, config); |
5640 | if (result.codes.empty() && !result.error.empty()) { | 5637 | if (result.codes.empty() && !result.error.empty()) { |
5641 | throw std::logic_error(_info.errorMessage(s("failed to compile module '"sv) + moduleName + s("\': "sv) + result.error, x)); | 5638 | throw std::logic_error(_info.errorMessage("failed to compile module '"s + moduleName + "\': "s + result.error, x)); |
5642 | } | 5639 | } |
5643 | lua_pop(L, 1); // cur | 5640 | lua_pop(L, 1); // cur |
5644 | } | 5641 | } |
@@ -5701,7 +5698,7 @@ private: | |||
5701 | auto assignList = x->new_ptr<ExpList_t>(); | 5698 | auto assignList = x->new_ptr<ExpList_t>(); |
5702 | assignList->exprs.push_back(exp); | 5699 | assignList->exprs.push_back(exp); |
5703 | auto assign = x->new_ptr<Assign_t>(); | 5700 | auto assign = x->new_ptr<Assign_t>(); |
5704 | assign->values.push_back(toAst<Exp_t>(s("require "sv) + _parser.toString(import->literal), x)); | 5701 | assign->values.push_back(toAst<Exp_t>("require "s + _parser.toString(import->literal), x)); |
5705 | auto assignment = x->new_ptr<ExpListAssign_t>(); | 5702 | auto assignment = x->new_ptr<ExpListAssign_t>(); |
5706 | assignment->expList.set(assignList); | 5703 | assignment->expList.set(assignList); |
5707 | assignment->action.set(assign); | 5704 | assignment->action.set(assign); |
@@ -5725,24 +5722,24 @@ private: | |||
5725 | auto x = whileNode; | 5722 | auto x = whileNode; |
5726 | str_list temp; | 5723 | str_list temp; |
5727 | if (expList) { | 5724 | if (expList) { |
5728 | temp.push_back(indent() + s("do"sv) + nll(whileNode)); | 5725 | temp.push_back(indent() + "do"s + nll(whileNode)); |
5729 | } | 5726 | } |
5730 | pushScope(); | 5727 | pushScope(); |
5731 | auto accumVar = getUnusedName("_accum_"sv); | 5728 | auto accumVar = getUnusedName("_accum_"sv); |
5732 | addToScope(accumVar); | 5729 | addToScope(accumVar); |
5733 | auto lenVar = getUnusedName("_len_"sv); | 5730 | auto lenVar = getUnusedName("_len_"sv); |
5734 | addToScope(lenVar); | 5731 | addToScope(lenVar); |
5735 | temp.push_back(indent() + s("local "sv) + accumVar + s(" = { }"sv) + nll(whileNode)); | 5732 | temp.push_back(indent() + "local "s + accumVar + " = { }"s + nll(whileNode)); |
5736 | temp.push_back(indent() + s("local "sv) + lenVar + s(" = 1"sv) + nll(whileNode)); | 5733 | temp.push_back(indent() + "local "s + lenVar + " = 1"s + nll(whileNode)); |
5737 | bool isUntil = _parser.toString(whileNode->type) == "until"sv; | 5734 | bool isUntil = _parser.toString(whileNode->type) == "until"sv; |
5738 | auto condStr = transformCondExp(whileNode->condition, isUntil); | 5735 | auto condStr = transformCondExp(whileNode->condition, isUntil); |
5739 | temp.push_back(indent() + s("while "sv) + condStr + s(" do"sv) + nll(whileNode)); | 5736 | temp.push_back(indent() + "while "s + condStr + " do"s + nll(whileNode)); |
5740 | pushScope(); | 5737 | pushScope(); |
5741 | auto assignLeft = toAst<ExpList_t>(accumVar + s("["sv) + lenVar + s("]"sv), x); | 5738 | auto assignLeft = toAst<ExpList_t>(accumVar + '[' + lenVar + ']', x); |
5742 | auto lenLine = lenVar + s(" = "sv) + lenVar + s(" + 1"sv) + nlr(whileNode); | 5739 | auto lenLine = lenVar + " = "s + lenVar + " + 1"s + nlr(whileNode); |
5743 | transformLoopBody(whileNode->body, temp, lenLine, ExpUsage::Assignment, assignLeft); | 5740 | transformLoopBody(whileNode->body, temp, lenLine, ExpUsage::Assignment, assignLeft); |
5744 | popScope(); | 5741 | popScope(); |
5745 | temp.push_back(indent() + s("end"sv) + nlr(whileNode)); | 5742 | temp.push_back(indent() + "end"s + nlr(whileNode)); |
5746 | if (expList) { | 5743 | if (expList) { |
5747 | auto assign = x->new_ptr<Assign_t>(); | 5744 | auto assign = x->new_ptr<Assign_t>(); |
5748 | assign->values.push_back(toAst<Exp_t>(accumVar, x)); | 5745 | assign->values.push_back(toAst<Exp_t>(accumVar, x)); |
@@ -5751,11 +5748,11 @@ private: | |||
5751 | assignment->action.set(assign); | 5748 | assignment->action.set(assign); |
5752 | transformAssignment(assignment, temp); | 5749 | transformAssignment(assignment, temp); |
5753 | } else { | 5750 | } else { |
5754 | temp.push_back(indent() + s("return "sv) + accumVar + nlr(whileNode)); | 5751 | temp.push_back(indent() + "return "s + accumVar + nlr(whileNode)); |
5755 | } | 5752 | } |
5756 | popScope(); | 5753 | popScope(); |
5757 | if (expList) { | 5754 | if (expList) { |
5758 | temp.push_back(indent() + s("end"sv) + nlr(whileNode)); | 5755 | temp.push_back(indent() + "end"s + nlr(whileNode)); |
5759 | } | 5756 | } |
5760 | out.push_back(join(temp)); | 5757 | out.push_back(join(temp)); |
5761 | } | 5758 | } |
@@ -5763,28 +5760,28 @@ private: | |||
5763 | void transformWhileClosure(While_t* whileNode, str_list& out) { | 5760 | void transformWhileClosure(While_t* whileNode, str_list& out) { |
5764 | auto x = whileNode; | 5761 | auto x = whileNode; |
5765 | str_list temp; | 5762 | str_list temp; |
5766 | temp.push_back(s("(function() "sv) + nll(whileNode)); | 5763 | temp.push_back("(function() "s + nll(whileNode)); |
5767 | pushScope(); | 5764 | pushScope(); |
5768 | _enableReturn.push(true); | 5765 | _enableReturn.push(true); |
5769 | auto accumVar = getUnusedName("_accum_"sv); | 5766 | auto accumVar = getUnusedName("_accum_"sv); |
5770 | addToScope(accumVar); | 5767 | addToScope(accumVar); |
5771 | auto lenVar = getUnusedName("_len_"sv); | 5768 | auto lenVar = getUnusedName("_len_"sv); |
5772 | addToScope(lenVar); | 5769 | addToScope(lenVar); |
5773 | temp.push_back(indent() + s("local "sv) + accumVar + s(" = { }"sv) + nll(whileNode)); | 5770 | temp.push_back(indent() + "local "s + accumVar + " = { }"s + nll(whileNode)); |
5774 | temp.push_back(indent() + s("local "sv) + lenVar + s(" = 1"sv) + nll(whileNode)); | 5771 | temp.push_back(indent() + "local "s + lenVar + " = 1"s + nll(whileNode)); |
5775 | bool isUntil = _parser.toString(whileNode->type) == "until"sv; | 5772 | bool isUntil = _parser.toString(whileNode->type) == "until"sv; |
5776 | auto condStr = transformCondExp(whileNode->condition, isUntil); | 5773 | auto condStr = transformCondExp(whileNode->condition, isUntil); |
5777 | temp.push_back(indent() + s("while "sv) + condStr + s(" do"sv) + nll(whileNode)); | 5774 | temp.push_back(indent() + "while "s + condStr + " do"s + nll(whileNode)); |
5778 | pushScope(); | 5775 | pushScope(); |
5779 | auto assignLeft = toAst<ExpList_t>(accumVar + s("["sv) + lenVar + s("]"sv), x); | 5776 | auto assignLeft = toAst<ExpList_t>(accumVar + '[' + lenVar + ']', x); |
5780 | auto lenLine = lenVar + s(" = "sv) + lenVar + s(" + 1"sv) + nlr(whileNode); | 5777 | auto lenLine = lenVar + " = "s + lenVar + " + 1"s + nlr(whileNode); |
5781 | transformLoopBody(whileNode->body, temp, lenLine, ExpUsage::Assignment, assignLeft); | 5778 | transformLoopBody(whileNode->body, temp, lenLine, ExpUsage::Assignment, assignLeft); |
5782 | popScope(); | 5779 | popScope(); |
5783 | temp.push_back(indent() + s("end"sv) + nlr(whileNode)); | 5780 | temp.push_back(indent() + "end"s + nlr(whileNode)); |
5784 | temp.push_back(indent() + s("return "sv) + accumVar + nlr(whileNode)); | 5781 | temp.push_back(indent() + "return "s + accumVar + nlr(whileNode)); |
5785 | _enableReturn.pop(); | 5782 | _enableReturn.pop(); |
5786 | popScope(); | 5783 | popScope(); |
5787 | temp.push_back(indent() + s("end)()"sv)); | 5784 | temp.push_back(indent() + "end)()"s); |
5788 | out.push_back(join(temp)); | 5785 | out.push_back(join(temp)); |
5789 | } | 5786 | } |
5790 | 5787 | ||
@@ -5816,7 +5813,7 @@ private: | |||
5816 | void transformSwitch(Switch_t* switchNode, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { | 5813 | void transformSwitch(Switch_t* switchNode, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { |
5817 | str_list temp; | 5814 | str_list temp; |
5818 | if (usage == ExpUsage::Closure) { | 5815 | if (usage == ExpUsage::Closure) { |
5819 | temp.push_back(s("(function()"sv) + nll(switchNode)); | 5816 | temp.push_back("(function()"s + nll(switchNode)); |
5820 | pushScope(); | 5817 | pushScope(); |
5821 | _enableReturn.push(true); | 5818 | _enableReturn.push(true); |
5822 | } | 5819 | } |
@@ -5831,34 +5828,34 @@ private: | |||
5831 | const auto& branches = switchNode->branches.objects(); | 5828 | const auto& branches = switchNode->branches.objects(); |
5832 | for (auto branch_ : branches) { | 5829 | for (auto branch_ : branches) { |
5833 | auto branch = static_cast<SwitchCase_t*>(branch_); | 5830 | auto branch = static_cast<SwitchCase_t*>(branch_); |
5834 | temp.push_back(indent() + s(branches.front() == branch ? "if"sv : "elseif"sv)); | 5831 | temp.push_back(indent() + (branches.front() == branch ? "if"s : "elseif"s)); |
5835 | str_list tmp; | 5832 | str_list tmp; |
5836 | const auto& exprs = branch->valueList->exprs.objects(); | 5833 | const auto& exprs = branch->valueList->exprs.objects(); |
5837 | for (auto exp_ : exprs) { | 5834 | for (auto exp_ : exprs) { |
5838 | auto exp = static_cast<Exp_t*>(exp_); | 5835 | auto exp = static_cast<Exp_t*>(exp_); |
5839 | transformExp(exp, tmp, ExpUsage::Closure); | 5836 | transformExp(exp, tmp, ExpUsage::Closure); |
5840 | if (!singleValueFrom(exp)) { | 5837 | if (!singleValueFrom(exp)) { |
5841 | tmp.back() = s("("sv) + tmp.back() + s(")"sv); | 5838 | tmp.back() = '(' + tmp.back() + ')'; |
5842 | } | 5839 | } |
5843 | temp.back().append(s(" "sv) + tmp.back() + s(" == "sv) + objVar + | 5840 | temp.back().append(' ' + tmp.back() + " == "s + |
5844 | s(exp == exprs.back() ? ""sv : " or"sv)); | 5841 | (exp == exprs.back() ? objVar : objVar + " or"s)); |
5845 | } | 5842 | } |
5846 | temp.back().append(s(" then"sv) + nll(branch)); | 5843 | temp.back().append(" then"s + nll(branch)); |
5847 | pushScope(); | 5844 | pushScope(); |
5848 | transform_plain_body(branch->body, temp, usage, assignList); | 5845 | transform_plain_body(branch->body, temp, usage, assignList); |
5849 | popScope(); | 5846 | popScope(); |
5850 | } | 5847 | } |
5851 | if (switchNode->lastBranch) { | 5848 | if (switchNode->lastBranch) { |
5852 | temp.push_back(indent() + s("else"sv) + nll(switchNode->lastBranch)); | 5849 | temp.push_back(indent() + "else"s + nll(switchNode->lastBranch)); |
5853 | pushScope(); | 5850 | pushScope(); |
5854 | transform_plain_body(switchNode->lastBranch, temp, usage, assignList); | 5851 | transform_plain_body(switchNode->lastBranch, temp, usage, assignList); |
5855 | popScope(); | 5852 | popScope(); |
5856 | } | 5853 | } |
5857 | temp.push_back(indent() + s("end"sv) + nlr(switchNode)); | 5854 | temp.push_back(indent() + "end"s + nlr(switchNode)); |
5858 | if (usage == ExpUsage::Closure) { | 5855 | if (usage == ExpUsage::Closure) { |
5859 | _enableReturn.pop(); | 5856 | _enableReturn.pop(); |
5860 | popScope(); | 5857 | popScope(); |
5861 | temp.push_back(indent() + s("end)()"sv)); | 5858 | temp.push_back(indent() + "end)()"s); |
5862 | } | 5859 | } |
5863 | out.push_back(join(temp)); | 5860 | out.push_back(join(temp)); |
5864 | } | 5861 | } |
@@ -5922,7 +5919,7 @@ private: | |||
5922 | auto x = localAttrib; | 5919 | auto x = localAttrib; |
5923 | auto attrib = _parser.toString(localAttrib->attrib); | 5920 | auto attrib = _parser.toString(localAttrib->attrib); |
5924 | if (attrib != "close"sv && attrib != "const"sv) { | 5921 | if (attrib != "close"sv && attrib != "const"sv) { |
5925 | throw std::logic_error(_info.errorMessage(s("unknown attribute '"sv) + attrib + '\'', localAttrib->attrib)); | 5922 | throw std::logic_error(_info.errorMessage("unknown attribute '"s + attrib + '\'', localAttrib->attrib)); |
5926 | } | 5923 | } |
5927 | if (attrib == "const"sv) { | 5924 | if (attrib == "const"sv) { |
5928 | str_list vars; | 5925 | str_list vars; |
@@ -5932,7 +5929,7 @@ private: | |||
5932 | } | 5929 | } |
5933 | str_list temp; | 5930 | str_list temp; |
5934 | auto varStr = join(vars, ", "sv); | 5931 | auto varStr = join(vars, ", "sv); |
5935 | temp.push_back(indent() + s("local "sv) + varStr + nll(x)); | 5932 | temp.push_back(indent() + "local "s + varStr + nll(x)); |
5936 | auto varList = toAst<ExpList_t>(varStr, x); | 5933 | auto varList = toAst<ExpList_t>(varStr, x); |
5937 | auto assignment = x->new_ptr<ExpListAssign_t>(); | 5934 | auto assignment = x->new_ptr<ExpListAssign_t>(); |
5938 | assignment->expList.set(varList); | 5935 | assignment->expList.set(varList); |
@@ -5969,12 +5966,12 @@ private: | |||
5969 | assignment->action.set(localAttrib->assign); | 5966 | assignment->action.set(localAttrib->assign); |
5970 | str_list temp; | 5967 | str_list temp; |
5971 | transformAssignment(assignment, temp); | 5968 | transformAssignment(assignment, temp); |
5972 | attrib = s(" <"sv) + attrib + '>'; | 5969 | attrib = " <"s + attrib + '>'; |
5973 | for (auto& var : vars) { | 5970 | for (auto& var : vars) { |
5974 | forceAddToScope(var); | 5971 | forceAddToScope(var); |
5975 | var.append(attrib); | 5972 | var.append(attrib); |
5976 | } | 5973 | } |
5977 | temp.push_back(indent() + s("local "sv) + join(vars, ", "sv) + s(" = "sv) + tmpVarStr + nll(x)); | 5974 | temp.push_back(indent() + "local "s + join(vars, ", "sv) + " = "s + tmpVarStr + nll(x)); |
5978 | out.push_back(join(temp)); | 5975 | out.push_back(join(temp)); |
5979 | } | 5976 | } |
5980 | 5977 | ||
@@ -5991,11 +5988,11 @@ private: | |||
5991 | } | 5988 | } |
5992 | 5989 | ||
5993 | void transformLabel(Label_t* label, str_list& out) { | 5990 | void transformLabel(Label_t* label, str_list& out) { |
5994 | out.push_back(indent() + s("::"sv) + _parser.toString(label->label) + s("::"sv) + nll(label)); | 5991 | out.push_back(indent() + "::"s + _parser.toString(label->label) + "::"s + nll(label)); |
5995 | } | 5992 | } |
5996 | 5993 | ||
5997 | void transformGoto(Goto_t* gotoNode, str_list& out) { | 5994 | void transformGoto(Goto_t* gotoNode, str_list& out) { |
5998 | out.push_back(indent() + s("goto "sv) + _parser.toString(gotoNode->label) + nll(gotoNode)); | 5995 | out.push_back(indent() + "goto "s + _parser.toString(gotoNode->label) + nll(gotoNode)); |
5999 | } | 5996 | } |
6000 | }; | 5997 | }; |
6001 | 5998 | ||