aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/yuescript/yue_compiler.cpp34
-rw-r--r--src/yuescript/yue_compiler.h1
2 files changed, 22 insertions, 13 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index 2ed0265..f2a4c7a 100644
--- a/src/yuescript/yue_compiler.cpp
+++ b/src/yuescript/yue_compiler.cpp
@@ -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);
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;