From dd64edd58fe25ec74ae5958128cf3f74b0692f3b Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 28 Jan 2026 18:43:14 +0800 Subject: Fixed compiler issues and added 800+ test cases. --- src/yue.cpp | 26 +++++++++++++------------- src/yuescript/yue_ast.h | 6 +++--- src/yuescript/yue_compiler.cpp | 31 ++++++++++++++++++++----------- src/yuescript/yue_parser.cpp | 37 ++++++++++++++++++------------------- 4 files changed, 54 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/yue.cpp b/src/yue.cpp index ee4eb38..a30075d 100644 --- a/src/yue.cpp +++ b/src/yue.cpp @@ -201,8 +201,13 @@ static std::string compileFile(const fs::path& file, yue::YueConfig conf, const conf.module = modulePath.string(); if (!workPath.empty()) { auto it = conf.options.find("path"); - if (it == conf.options.end()) { - conf.options["path"] = (workPath / "?.lua"sv).string(); + if (it != conf.options.end()) { + if (!it->second.empty()) { + it->second += ';'; + } + it->second += (fs::path(workPath) / "?.lua"sv).string(); + } else { + conf.options["path"] = (fs::path(workPath) / "?.lua"sv).string(); } } auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); @@ -852,7 +857,6 @@ int main(int narg, const char** args) { std::list>> results; for (const auto& file : files) { auto task = async>([=]() { - try { std::ifstream input(file.first, std::ios::in); if (input) { std::string s( @@ -862,7 +866,12 @@ int main(int narg, const char** args) { conf.module = file.first; if (!workPath.empty()) { auto it = conf.options.find("path"); - if (it == conf.options.end()) { + if (it != conf.options.end()) { + if (!it->second.empty()) { + it->second += ';'; + } + it->second += (fs::path(workPath) / "?.lua"sv).string(); + } else { conf.options["path"] = (fs::path(workPath) / "?.lua"sv).string(); } } @@ -942,15 +951,6 @@ int main(int narg, const char** args) { } else { return std::tuple{1, std::string(), "Failed to read file: "s + file.first + '\n'}; } - } catch (const std::length_error& e) { - std::ostringstream buf; - buf << "std::length_error: " << e.what() << " for file: " << file.first << '\n'; - return std::tuple{1, std::string(), buf.str()}; - } catch (const std::exception& e) { - std::ostringstream buf; - buf << "Exception: " << e.what() << " for file: " << file.first << '\n'; - return std::tuple{1, std::string(), buf.str()}; - } }); results.push_back(std::move(task)); } diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 5043526..5faeed5 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -689,12 +689,12 @@ AST_END(ChainValue) AST_NODE(AssignableChain) ast_ptr sep; - ast_sel_list items; + ast_sel_list items; AST_MEMBER(AssignableChain, &sep, &items) AST_END(AssignableChain) AST_NODE(Value) - ast_sel item; + ast_sel item; AST_MEMBER(Value, &item) AST_END(Value) @@ -782,7 +782,7 @@ AST_NODE(Export) AST_END(Export) AST_NODE(FnArgDef) - ast_sel name; + ast_sel name; ast_ptr op; ast_ptr label; ast_ptr defaultValue; diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index c5e4a78..835a70a 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -78,7 +78,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.32.5"sv; +const std::string_view version = "0.32.6"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -865,7 +865,7 @@ private: } bool isConstValue(Value_t* value) { - if (auto strNode = value->item.as()) { + if (auto strNode = value->get_by_path()) { switch (strNode->str->get_id()) { case id(): case id(): @@ -1154,7 +1154,9 @@ private: ast_ptr newExp(String_t* string, ast_node* x) { auto value = x->new_ptr(); - value->item.set(string); + auto chainValue = x->new_ptr(); + chainValue->items.push_back(string); + value->item.set(chainValue); return newExp(value, x); } @@ -3487,9 +3489,7 @@ private: newPair->key.set(mp->key); break; case id(): { - auto value = mp->new_ptr(); - value->item.set(mp->key); - newPair->key.set(newExp(value, mp)); + newPair->key.set(newExp(mp->key.to(), mp)); break; } default: YUEE("AST node mismatch", mp->key); break; @@ -3622,7 +3622,7 @@ private: continue; } } - if (auto value = singleValueFrom(exp); !value || !value->item.is()) { + if (auto value = singleValueFrom(exp); !value || !value->item->get_by_path()) { auto var = singleVariableFrom(exp, AccessType::None); if (var.empty()) { if (!des.inlineAssignment) { @@ -4664,7 +4664,6 @@ private: case id(): transformSimpleValue(static_cast(item), out); break; case id(): transform_simple_table(static_cast(item), out); break; case id(): transformChainValue(static_cast(item), out, ExpUsage::Closure); break; - case id(): transformString(static_cast(item), out); break; default: YUEE("AST node mismatch", value); break; } } @@ -5931,6 +5930,14 @@ private: arg.assignment = asmt; break; } + case id(): { + arg.name = getUnusedName("_arg_"sv); + auto simpleValue = def->new_ptr(); + simpleValue->value.set(def->name); + auto asmt = assignmentFrom(newExp(simpleValue, def), toAst(arg.name, def), def); + arg.assignment = asmt; + break; + } default: YUEE("AST node mismatch", def->name.get()); break; } forceAddToScope(arg.name); @@ -6883,7 +6890,9 @@ private: } case id(): transformString(static_cast(item), temp); - temp.back() = '(' + temp.back() + ')'; + if (chainList.size() > 1) { + temp.back() = '(' + temp.back() + ')'; + } break; case id(): transformExp(static_cast(item), temp, ExpUsage::Closure); @@ -7007,7 +7016,7 @@ private: BREAK_IF(!exp); auto value = singleValueFrom(exp); BREAK_IF(!value); - auto lstr = value->get_by_path(); + auto lstr = value->get_by_path(); BREAK_IF(!lstr); str = _parser.toString(lstr->content); rawString = true; @@ -9606,7 +9615,7 @@ private: if (auto dotChain = ast_cast(chain->items.back())) { classTextName = '\"' + _parser.toString(dotChain->name) + '\"'; } else if (auto index = ast_cast(chain->items.back())) { - if (auto name = index->get_by_path()) { + if (auto name = index->get_by_path()) { transformString(name, temp); classTextName = std::move(temp.back()); temp.pop_back(); diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index ad76517..194d04d 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -358,7 +358,7 @@ YueParser::YueParser() { }); advance_match = and_(advance); - push_indent = pl::user(plain_space, [](const item_t& item) { + push_indent = pl::user(plain_space >> not_(stop), [](const item_t& item) { int indent = 0; for (input_it i = item.begin->m_it; i != item.end->m_it; ++i) { switch (*i) { @@ -738,12 +738,18 @@ YueParser::YueParser() { return st->noChainBlockStack.empty() || !st->noChainBlockStack.back(); }) >> +space_break >> advance_match >> ensure( chain_line >> *(+space_break >> chain_line), pop_indent); - ChainValue = - Seperator >> - chain >> - -ExistentialOp >> - -(InvokeArgs | chain_block) >> - -TableAppendingOp; + ChainValue = Seperator >> ( + ( + chain >> + -ExistentialOp >> + -(InvokeArgs | chain_block) >> -TableAppendingOp + ) | String >> -( + and_(white >> set(".\\:")) >> ( + chain_items >> -(InvokeArgs | chain_block) >> -TableAppendingOp | + chain_block >> -TableAppendingOp + ) + ) + ); inc_exp_level = pl::user(true_(), [](const item_t& item) { State* st = reinterpret_cast(item.user_data); @@ -762,7 +768,7 @@ YueParser::YueParser() { }); SimpleTable = Seperator >> key_value >> *(space >> ',' >> space >> key_value); - Value = inc_exp_level >> ensure(SimpleValue | SimpleTable | ChainValue | String, dec_exp_level); + Value = inc_exp_level >> ensure(SimpleValue | SimpleTable | ChainValue, dec_exp_level); single_string_inner = '\\' >> set("'\\") | not_('\'') >> any_char; SingleString = '\'' >> *single_string_inner >> ('\'' | unclosed_single_string_error); @@ -828,21 +834,13 @@ YueParser::YueParser() { TableAppendingOp = and_('[') >> "[]"; PlainItem = +any_char; - chain_call = ( - Callable >> -ExistentialOp >> -chain_items - ) | ( - String >> chain_items - ); + chain_call = Callable >> -ExistentialOp >> -chain_items; chain_index_chain = index >> -ExistentialOp >> -chain_items; chain_dot_chain = DotChainItem >> -ExistentialOp >> -chain_items; chain = chain_call | chain_dot_chain | colon_chain | chain_index_chain; - chain_call_list = ( - Callable >> -ExistentialOp >> chain_items - ) | ( - String >> chain_items - ); + chain_call_list = Callable >> -ExistentialOp >> chain_items; chain_list = chain_call_list | chain_dot_chain | colon_chain | chain_index_chain; AssignableChain = Seperator >> chain_list; @@ -1030,7 +1028,7 @@ YueParser::YueParser() { fn_arg_def_lit_lines = fn_arg_def_lit_line >> *(-(space >> ',') >> space_break >> fn_arg_def_lit_line); - FnArgDef = (Variable | SelfItem >> -ExistentialOp) >> -(space >> '`' >> space >> Name) >> -(space >> '=' >> space >> Exp) | TableLit | SimpleTable; + FnArgDef = (Variable | SelfItem >> -ExistentialOp) >> -(space >> '`' >> space >> Name) >> -(space >> '=' >> space >> Exp) | TableLit | SimpleTable | Comprehension; check_vararg_position = and_(white >> (')' | key("using"))) | white >> -(',' >> white) >> vararg_position_error; @@ -1391,6 +1389,7 @@ std::string ParseInfo::errorMessage(std::string_view msg, int errLine, int errCo } errorDisplayCol = displayCol; } + errorDisplayCol = std::max(errorDisplayCol, 1); for (int lineNum = startLine; lineNum <= endLine; ++lineNum) { int displayLineNum = lineNum + lineOffset; -- cgit v1.2.3-55-g6feb