diff options
author | Li Jin <dragon-fly@qq.com> | 2023-01-29 16:39:35 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-01-29 16:39:35 +0800 |
commit | 8d648dc8c075fc1cf8d89d74c1425cdeb47e8089 (patch) | |
tree | d038d12f7e683e490a36d431376c2da6dd2b17bf /src | |
parent | e10b1b163a9a173f32b956bf1fb9be00194352b5 (diff) | |
download | yuescript-8d648dc8c075fc1cf8d89d74c1425cdeb47e8089.tar.gz yuescript-8d648dc8c075fc1cf8d89d74c1425cdeb47e8089.tar.bz2 yuescript-8d648dc8c075fc1cf8d89d74c1425cdeb47e8089.zip |
refactor break loop keyword checking.
Diffstat (limited to 'src')
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 86 |
1 files changed, 74 insertions, 12 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index c7f0f27..83933d1 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -71,7 +71,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
71 | "close"s // Lua 5.4 | 71 | "close"s // Lua 5.4 |
72 | }; | 72 | }; |
73 | 73 | ||
74 | const std::string_view version = "0.15.24"sv; | 74 | const std::string_view version = "0.15.25"sv; |
75 | const std::string_view extension = "yue"sv; | 75 | const std::string_view extension = "yue"sv; |
76 | 76 | ||
77 | class YueCompilerImpl { | 77 | class YueCompilerImpl { |
@@ -296,6 +296,7 @@ private: | |||
296 | }; | 296 | }; |
297 | std::stack<VarArgState> _varArgs; | 297 | std::stack<VarArgState> _varArgs; |
298 | std::stack<bool> _enableReturn; | 298 | std::stack<bool> _enableReturn; |
299 | std::stack<bool> _enableBreakLoop; | ||
299 | std::stack<std::string> _withVars; | 300 | std::stack<std::string> _withVars; |
300 | struct ContinueVar { | 301 | struct ContinueVar { |
301 | std::string var; | 302 | std::string var; |
@@ -1072,12 +1073,14 @@ private: | |||
1072 | 1073 | ||
1073 | void pushFunctionScope() { | 1074 | void pushFunctionScope() { |
1074 | _enableReturn.push(true); | 1075 | _enableReturn.push(true); |
1076 | _enableBreakLoop.push(false); | ||
1075 | _gotoScopes.push(_gotoScope); | 1077 | _gotoScopes.push(_gotoScope); |
1076 | _gotoScope++; | 1078 | _gotoScope++; |
1077 | } | 1079 | } |
1078 | 1080 | ||
1079 | void popFunctionScope() { | 1081 | void popFunctionScope() { |
1080 | _enableReturn.pop(); | 1082 | _enableReturn.pop(); |
1083 | _enableBreakLoop.pop(); | ||
1081 | _gotoScopes.pop(); | 1084 | _gotoScopes.pop(); |
1082 | } | 1085 | } |
1083 | 1086 | ||
@@ -1224,8 +1227,16 @@ private: | |||
1224 | } | 1227 | } |
1225 | default: YUEE("AST node mismatch", appendix->item.get()); break; | 1228 | default: YUEE("AST node mismatch", appendix->item.get()); break; |
1226 | } | 1229 | } |
1227 | } else if (statement->content.is<Return_t>() && !statement->appendix->item.is<IfLine_t>()) { | 1230 | } else if (!statement->appendix->item.is<IfLine_t>()) { |
1228 | throw std::logic_error(_info.errorMessage("loop line decorator can not be used in a return statement"sv, statement->appendix->item)); | 1231 | auto appendix = statement->appendix->item.get(); |
1232 | switch (statement->content->getId()) { | ||
1233 | case id<Return_t>(): | ||
1234 | throw std::logic_error(_info.errorMessage("loop line decorator can not be used in a return statement"sv, appendix)); | ||
1235 | break; | ||
1236 | case id<BreakLoop_t>(): | ||
1237 | throw std::logic_error(_info.errorMessage("loop line decorator can not be used in a break-loop statement"sv, appendix)); | ||
1238 | break; | ||
1239 | } | ||
1229 | } | 1240 | } |
1230 | auto appendix = statement->appendix.get(); | 1241 | auto appendix = statement->appendix.get(); |
1231 | switch (appendix->item->getId()) { | 1242 | switch (appendix->item->getId()) { |
@@ -6099,18 +6110,62 @@ private: | |||
6099 | if (auto stmt = ast_cast<Statement_t>(node)) { | 6110 | if (auto stmt = ast_cast<Statement_t>(node)) { |
6100 | if (stmt->content.is<BreakLoop_t>()) { | 6111 | if (stmt->content.is<BreakLoop_t>()) { |
6101 | return _parser.toString(stmt->content) == "continue"sv ? traversal::Stop : traversal::Return; | 6112 | return _parser.toString(stmt->content) == "continue"sv ? traversal::Stop : traversal::Return; |
6102 | } else if (expListFrom(stmt)) { | 6113 | } else if (auto expList = expListFrom(stmt)) { |
6103 | return traversal::Continue; | 6114 | BLOCK_START |
6115 | auto value = singleValueFrom(expList); | ||
6116 | BREAK_IF(!value); | ||
6117 | auto simpleValue = value->item.as<SimpleValue_t>(); | ||
6118 | BREAK_IF(!simpleValue); | ||
6119 | auto sVal = simpleValue->value.get(); | ||
6120 | switch (sVal->getId()) { | ||
6121 | case id<With_t>(): { | ||
6122 | auto withNode = static_cast<With_t*>(sVal); | ||
6123 | if (hasContinueStatement(withNode->body)) { | ||
6124 | return traversal::Stop; | ||
6125 | } | ||
6126 | break; | ||
6127 | } | ||
6128 | case id<Do_t>(): { | ||
6129 | auto doNode = static_cast<Do_t*>(sVal); | ||
6130 | if (hasContinueStatement(doNode->body)) { | ||
6131 | return traversal::Stop; | ||
6132 | } | ||
6133 | break; | ||
6134 | } | ||
6135 | case id<If_t>(): { | ||
6136 | auto ifNode = static_cast<If_t*>(sVal); | ||
6137 | for (auto n : ifNode->nodes.objects()) { | ||
6138 | if (hasContinueStatement(n)) { | ||
6139 | return traversal::Stop; | ||
6140 | } | ||
6141 | } | ||
6142 | break; | ||
6143 | } | ||
6144 | case id<Switch_t>(): { | ||
6145 | auto switchNode = static_cast<Switch_t*>(sVal); | ||
6146 | for (auto branch : switchNode->branches.objects()) { | ||
6147 | if (hasContinueStatement(static_cast<SwitchCase_t*>(branch)->body)) { | ||
6148 | return traversal::Stop; | ||
6149 | } | ||
6150 | } | ||
6151 | if (switchNode->lastBranch) { | ||
6152 | if (hasContinueStatement(switchNode->lastBranch)) { | ||
6153 | return traversal::Stop; | ||
6154 | } | ||
6155 | } | ||
6156 | break; | ||
6157 | } | ||
6158 | } | ||
6159 | BLOCK_END | ||
6104 | } | 6160 | } |
6105 | return traversal::Return; | 6161 | } else { |
6106 | } else | ||
6107 | switch (node->getId()) { | 6162 | switch (node->getId()) { |
6108 | case id<FunLit_t>(): | 6163 | case id<Body_t>(): |
6109 | case id<Invoke_t>(): | 6164 | case id<Block_t>(): |
6110 | case id<InvokeArgs_t>(): | 6165 | return traversal::Continue; |
6111 | return traversal::Return; | ||
6112 | } | 6166 | } |
6113 | return traversal::Continue; | 6167 | } |
6168 | return traversal::Return; | ||
6114 | }); | 6169 | }); |
6115 | } | 6170 | } |
6116 | 6171 | ||
@@ -6171,7 +6226,9 @@ private: | |||
6171 | } | 6226 | } |
6172 | addDoToLastLineReturn(body); | 6227 | addDoToLastLineReturn(body); |
6173 | } | 6228 | } |
6229 | _enableBreakLoop.push(true); | ||
6174 | transform_plain_body(body, temp, usage, assignList); | 6230 | transform_plain_body(body, temp, usage, assignList); |
6231 | _enableBreakLoop.pop(); | ||
6175 | if (withContinue) { | 6232 | if (withContinue) { |
6176 | if (target < 502) { | 6233 | if (target < 502) { |
6177 | if (extraDo) { | 6234 | if (extraDo) { |
@@ -6250,7 +6307,9 @@ private: | |||
6250 | } | 6307 | } |
6251 | addDoToLastLineReturn(body); | 6308 | addDoToLastLineReturn(body); |
6252 | } | 6309 | } |
6310 | _enableBreakLoop.push(true); | ||
6253 | transform_plain_body(body, temp, ExpUsage::Common); | 6311 | transform_plain_body(body, temp, ExpUsage::Common); |
6312 | _enableBreakLoop.pop(); | ||
6254 | if (withContinue) { | 6313 | if (withContinue) { |
6255 | if (target < 502) { | 6314 | if (target < 502) { |
6256 | transformAssignment(_continueVars.top().condAssign, temp); | 6315 | transformAssignment(_continueVars.top().condAssign, temp); |
@@ -8203,6 +8262,9 @@ private: | |||
8203 | 8262 | ||
8204 | void transformBreakLoop(BreakLoop_t* breakLoop, str_list& out) { | 8263 | void transformBreakLoop(BreakLoop_t* breakLoop, str_list& out) { |
8205 | auto keyword = _parser.toString(breakLoop); | 8264 | auto keyword = _parser.toString(breakLoop); |
8265 | if (_enableBreakLoop.empty() || !_enableBreakLoop.top()) { | ||
8266 | throw std::logic_error(_info.errorMessage(keyword + " is not inside a loop"s, breakLoop)); | ||
8267 | } | ||
8206 | if (keyword == "break"sv) { | 8268 | if (keyword == "break"sv) { |
8207 | out.push_back(indent() + keyword + nll(breakLoop)); | 8269 | out.push_back(indent() + keyword + nll(breakLoop)); |
8208 | return; | 8270 | return; |