diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/yue.cpp | 10 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 44 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.h | 1 |
3 files changed, 32 insertions, 23 deletions
diff --git a/src/yue.cpp b/src/yue.cpp index 26f581e..f48d14b 100644 --- a/src/yue.cpp +++ b/src/yue.cpp | |||
| @@ -57,8 +57,8 @@ std::future<R> async(const std::function<R()>& f) { | |||
| 57 | #else | 57 | #else |
| 58 | template<class R> | 58 | template<class R> |
| 59 | std::future<R> async(const std::function<R()>& f) { | 59 | std::future<R> async(const std::function<R()>& f) { |
| 60 | // fallback: ignore stack size | 60 | // fallback: ignore stack size |
| 61 | return std::async(std::launch::async, f); | 61 | return std::async(std::launch::async, f); |
| 62 | } | 62 | } |
| 63 | #endif | 63 | #endif |
| 64 | 64 | ||
| @@ -520,9 +520,6 @@ int main(int narg, const char** args) { | |||
| 520 | std::string workPath; | 520 | std::string workPath; |
| 521 | std::list<std::pair<std::string, std::string>> files; | 521 | std::list<std::pair<std::string, std::string>> files; |
| 522 | 522 | ||
| 523 | auto isOptionToken = [](std::string_view s) { | ||
| 524 | return !s.empty() && (s[0] == '-' || (s.size() >= 2 && s.substr(0, 2) == "--"sv)); | ||
| 525 | }; | ||
| 526 | auto takeValue = [&](int& i, std::string_view arg, std::string_view optName) -> std::string { | 523 | auto takeValue = [&](int& i, std::string_view arg, std::string_view optName) -> std::string { |
| 527 | // supports: --opt=value, --opt value, -o value, -t value, etc. | 524 | // supports: --opt=value, --opt value, -o value, -t value, etc. |
| 528 | if (auto eq = arg.find('='); eq != std::string_view::npos) { | 525 | if (auto eq = arg.find('='); eq != std::string_view::npos) { |
| @@ -697,6 +694,9 @@ int main(int narg, const char** args) { | |||
| 697 | if (resultFile.empty()) return 1; | 694 | if (resultFile.empty()) return 1; |
| 698 | } else if (arg == "-w"sv || arg == "--watch"sv || arg.rfind("--watch="sv, 0) == 0) { | 695 | } else if (arg == "-w"sv || arg == "--watch"sv || arg.rfind("--watch="sv, 0) == 0) { |
| 699 | #ifndef YUE_NO_WATCHER | 696 | #ifndef YUE_NO_WATCHER |
| 697 | auto isOptionToken = [](std::string_view s) { | ||
| 698 | return !s.empty() && (s[0] == '-' || (s.size() >= 2 && s.substr(0, 2) == "--"sv)); | ||
| 699 | }; | ||
| 700 | watchFiles = true; | 700 | watchFiles = true; |
| 701 | // accept optional directory value: -w <dir> / --watch <dir> / --watch=<dir> | 701 | // accept optional directory value: -w <dir> / --watch <dir> / --watch=<dir> |
| 702 | if (arg != "-w"sv) { | 702 | if (arg != "-w"sv) { |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index c4fc952..f2a4c7a 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -78,7 +78,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
| 78 | "close"s // Lua 5.4 | 78 | "close"s // Lua 5.4 |
| 79 | }; | 79 | }; |
| 80 | 80 | ||
| 81 | const std::string_view version = "0.32.3"sv; | 81 | const std::string_view version = "0.32.4"sv; |
| 82 | const std::string_view extension = "yue"sv; | 82 | const std::string_view extension = "yue"sv; |
| 83 | 83 | ||
| 84 | class CompileError : public std::logic_error { | 84 | class CompileError : public std::logic_error { |
| @@ -527,6 +527,7 @@ private: | |||
| 527 | } | 527 | } |
| 528 | 528 | ||
| 529 | bool isDefined(const std::string& name) const { | 529 | bool isDefined(const std::string& name) const { |
| 530 | if (name.empty()) return false; | ||
| 530 | bool isDefined = false; | 531 | bool isDefined = false; |
| 531 | int mode = int(std::isupper(name[0]) ? GlobalMode::Capital : GlobalMode::Any); | 532 | int mode = int(std::isupper(name[0]) ? GlobalMode::Capital : GlobalMode::Any); |
| 532 | const auto& current = _scopes.back(); | 533 | const auto& current = _scopes.back(); |
| @@ -554,6 +555,7 @@ private: | |||
| 554 | } | 555 | } |
| 555 | 556 | ||
| 556 | bool isSolidDefined(const std::string& name) const { | 557 | bool isSolidDefined(const std::string& name) const { |
| 558 | if (name.empty()) return false; | ||
| 557 | bool defined = false; | 559 | bool defined = false; |
| 558 | for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { | 560 | for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { |
| 559 | auto vars = it->vars.get(); | 561 | auto vars = it->vars.get(); |
| @@ -567,6 +569,7 @@ private: | |||
| 567 | } | 569 | } |
| 568 | 570 | ||
| 569 | bool isSolidLocal(const std::string& name) { | 571 | bool isSolidLocal(const std::string& name) { |
| 572 | if (name.empty()) return false; | ||
| 570 | bool local = false; | 573 | bool local = false; |
| 571 | for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { | 574 | for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { |
| 572 | auto vars = it->vars.get(); | 575 | auto vars = it->vars.get(); |
| @@ -585,6 +588,7 @@ private: | |||
| 585 | } | 588 | } |
| 586 | 589 | ||
| 587 | bool isLocal(const std::string& name) { | 590 | bool isLocal(const std::string& name) { |
| 591 | if (name.empty()) return false; | ||
| 588 | bool local = false; | 592 | bool local = false; |
| 589 | bool defined = false; | 593 | bool defined = false; |
| 590 | for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { | 594 | for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { |
| @@ -613,6 +617,7 @@ private: | |||
| 613 | } | 617 | } |
| 614 | 618 | ||
| 615 | bool isDeclaredAsGlobal(const std::string& name) const { | 619 | bool isDeclaredAsGlobal(const std::string& name) const { |
| 620 | if (name.empty()) return false; | ||
| 616 | bool global = false; | 621 | bool global = false; |
| 617 | for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { | 622 | for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { |
| 618 | auto vars = it->vars.get(); | 623 | auto vars = it->vars.get(); |
| @@ -631,6 +636,7 @@ private: | |||
| 631 | } | 636 | } |
| 632 | 637 | ||
| 633 | bool isConst(const std::string& name) const { | 638 | bool isConst(const std::string& name) const { |
| 639 | if (name.empty()) return false; | ||
| 634 | bool isConst = false; | 640 | bool isConst = false; |
| 635 | decltype(_scopes.back().allows.get()) allows = nullptr; | 641 | decltype(_scopes.back().allows.get()) allows = nullptr; |
| 636 | for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { | 642 | for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { |
| @@ -1990,7 +1996,7 @@ private: | |||
| 1990 | _config.lintGlobalVariable = false; | 1996 | _config.lintGlobalVariable = false; |
| 1991 | if (!assignment->action.is<Assign_t>()) return vars; | 1997 | if (!assignment->action.is<Assign_t>()) return vars; |
| 1992 | for (auto exp : assignment->expList->exprs.objects()) { | 1998 | for (auto exp : assignment->expList->exprs.objects()) { |
| 1993 | auto var = singleVariableFrom(exp, AccessType::Write); | 1999 | auto var = singleVariableFrom(exp, AccessType::None); |
| 1994 | vars.push_back(var.empty() ? Empty : var); | 2000 | vars.push_back(var.empty() ? Empty : var); |
| 1995 | } | 2001 | } |
| 1996 | _config.lintGlobalVariable = lintGlobal; | 2002 | _config.lintGlobalVariable = lintGlobal; |
| @@ -2002,7 +2008,7 @@ private: | |||
| 2002 | bool lintGlobal = _config.lintGlobalVariable; | 2008 | bool lintGlobal = _config.lintGlobalVariable; |
| 2003 | _config.lintGlobalVariable = false; | 2009 | _config.lintGlobalVariable = false; |
| 2004 | for (auto exp : with->valueList->exprs.objects()) { | 2010 | for (auto exp : with->valueList->exprs.objects()) { |
| 2005 | auto var = singleVariableFrom(exp, AccessType::Write); | 2011 | auto var = singleVariableFrom(exp, AccessType::None); |
| 2006 | vars.push_back(var.empty() ? Empty : var); | 2012 | vars.push_back(var.empty() ? Empty : var); |
| 2007 | } | 2013 | } |
| 2008 | _config.lintGlobalVariable = lintGlobal; | 2014 | _config.lintGlobalVariable = lintGlobal; |
| @@ -4412,7 +4418,7 @@ private: | |||
| 4412 | auto result = YueCompiler{}.compile(codes, config); | 4418 | auto result = YueCompiler{}.compile(codes, config); |
| 4413 | #endif // YUE_NO_MACRO | 4419 | #endif // YUE_NO_MACRO |
| 4414 | if (result.error) { | 4420 | if (result.error) { |
| 4415 | YUEE("failed to compile dues to Yue formatter", x); | 4421 | YUEE("failed to compile dues to Yue formatter\n"s + result.error.value().displayMessage, x); |
| 4416 | } | 4422 | } |
| 4417 | usedVar = result.usedVar; | 4423 | usedVar = result.usedVar; |
| 4418 | if (result.globals) { | 4424 | if (result.globals) { |
| @@ -5353,16 +5359,18 @@ private: | |||
| 5353 | auto stmt = static_cast<Statement_t*>(node); | 5359 | auto stmt = static_cast<Statement_t*>(node); |
| 5354 | if (auto importNode = stmt->content.as<Import_t>(); | 5360 | if (auto importNode = stmt->content.as<Import_t>(); |
| 5355 | importNode && importNode->content.is<ImportAllGlobal_t>()) { | 5361 | importNode && importNode->content.is<ImportAllGlobal_t>()) { |
| 5356 | if (_importedGlobal) { | 5362 | if (!_config.disableGlobalImport) { |
| 5357 | throw CompileError("import global redeclared in same scope"sv, importNode); | 5363 | if (_importedGlobal) { |
| 5358 | } else { | 5364 | throw CompileError("import global redeclared in same scope"sv, importNode); |
| 5359 | auto& scope = currentScope(); | 5365 | } else { |
| 5360 | scope.importedGlobal = std::make_unique<ImportedGlobal>(); | 5366 | auto& scope = currentScope(); |
| 5361 | _importedGlobal = scope.importedGlobal.get(); | 5367 | scope.importedGlobal = std::make_unique<ImportedGlobal>(); |
| 5362 | _importedGlobal->vars = scope.vars.get(); | 5368 | _importedGlobal = scope.importedGlobal.get(); |
| 5363 | _importedGlobal->indent = indent(); | 5369 | _importedGlobal->vars = scope.vars.get(); |
| 5364 | _importedGlobal->nl = nl(stmt); | 5370 | _importedGlobal->indent = indent(); |
| 5365 | _importedGlobal->globalCodeLine = &temp.emplace_back(); | 5371 | _importedGlobal->nl = nl(stmt); |
| 5372 | _importedGlobal->globalCodeLine = &temp.emplace_back(); | ||
| 5373 | } | ||
| 5366 | } | 5374 | } |
| 5367 | } else { | 5375 | } else { |
| 5368 | transformStatement(stmt, temp); | 5376 | transformStatement(stmt, temp); |
| @@ -10872,13 +10880,13 @@ private: | |||
| 10872 | } | 10880 | } |
| 10873 | BLOCK_END | 10881 | BLOCK_END |
| 10874 | if (wrapped) { | 10882 | if (wrapped) { |
| 10875 | auto expList = x->new_ptr<ExpList_t>(); | 10883 | auto expList = tryFunc->new_ptr<ExpList_t>(); |
| 10876 | expList->exprs.push_back(tryFunc); | 10884 | expList->exprs.push_back(tryFunc); |
| 10877 | auto expListAssign = x->new_ptr<ExpListAssign_t>(); | 10885 | auto expListAssign = tryFunc->new_ptr<ExpListAssign_t>(); |
| 10878 | expListAssign->expList.set(expList); | 10886 | expListAssign->expList.set(expList); |
| 10879 | auto stmt = x->new_ptr<Statement_t>(); | 10887 | auto stmt = tryFunc->new_ptr<Statement_t>(); |
| 10880 | stmt->content.set(expListAssign); | 10888 | stmt->content.set(expListAssign); |
| 10881 | auto block = x->new_ptr<Block_t>(); | 10889 | auto block = tryFunc->new_ptr<Block_t>(); |
| 10882 | block->statementOrComments.push_back(stmt); | 10890 | block->statementOrComments.push_back(stmt); |
| 10883 | tryFunc.set(block); | 10891 | tryFunc.set(block); |
| 10884 | } | 10892 | } |
diff --git a/src/yuescript/yue_compiler.h b/src/yuescript/yue_compiler.h index eb5fb16..706e688 100644 --- a/src/yuescript/yue_compiler.h +++ b/src/yuescript/yue_compiler.h | |||
| @@ -33,6 +33,7 @@ struct YueConfig { | |||
| 33 | bool reserveComment = false; | 33 | bool reserveComment = false; |
| 34 | bool lax = false; | 34 | bool lax = false; |
| 35 | // internal options | 35 | // internal options |
| 36 | bool disableGlobalImport = false; | ||
| 36 | bool exporting = false; | 37 | bool exporting = false; |
| 37 | bool profiling = false; | 38 | bool profiling = false; |
| 38 | int lineOffset = 0; | 39 | int lineOffset = 0; |
