diff options
Diffstat (limited to 'src/MoonP')
-rw-r--r-- | src/MoonP/moon_ast.h | 11 | ||||
-rw-r--r-- | src/MoonP/moon_compiler.cpp | 59 | ||||
-rw-r--r-- | src/MoonP/moon_parser.cpp | 11 | ||||
-rw-r--r-- | src/MoonP/moon_parser.h | 1 | ||||
-rw-r--r-- | src/MoonP/stacktraceplus.h | 4 |
5 files changed, 80 insertions, 6 deletions
diff --git a/src/MoonP/moon_ast.h b/src/MoonP/moon_ast.h index 42522dd..1491344 100644 --- a/src/MoonP/moon_ast.h +++ b/src/MoonP/moon_ast.h | |||
@@ -113,6 +113,15 @@ AST_NODE(Local) | |||
113 | AST_MEMBER(Local, &item) | 113 | AST_MEMBER(Local, &item) |
114 | AST_END(Local) | 114 | AST_END(Local) |
115 | 115 | ||
116 | class Assign_t; | ||
117 | |||
118 | AST_NODE(LocalAttrib) | ||
119 | ast_ptr<true, Name_t> attrib; | ||
120 | ast_ptr<true, NameList_t> nameList; | ||
121 | ast_ptr<true, Assign_t> assign; | ||
122 | AST_MEMBER(LocalAttrib, &attrib, &nameList, &assign) | ||
123 | AST_END(LocalAttrib) | ||
124 | |||
116 | AST_NODE(colon_import_name) | 125 | AST_NODE(colon_import_name) |
117 | ast_ptr<true, Variable_t> name; | 126 | ast_ptr<true, Variable_t> name; |
118 | AST_MEMBER(colon_import_name, &name) | 127 | AST_MEMBER(colon_import_name, &name) |
@@ -686,7 +695,7 @@ AST_END(statement_sep) | |||
686 | AST_NODE(Statement) | 695 | AST_NODE(Statement) |
687 | ast_sel<true, Import_t, While_t, Repeat_t, For_t, ForEach_t, | 696 | ast_sel<true, Import_t, While_t, Repeat_t, For_t, ForEach_t, |
688 | Return_t, Local_t, Global_t, Export_t, Macro_t, BreakLoop_t, | 697 | Return_t, Local_t, Global_t, Export_t, Macro_t, BreakLoop_t, |
689 | Label_t, Goto_t, Backcall_t, ExpListAssign_t> content; | 698 | Label_t, Goto_t, Backcall_t, LocalAttrib_t, ExpListAssign_t> content; |
690 | ast_ptr<false, statement_appendix_t> appendix; | 699 | ast_ptr<false, statement_appendix_t> appendix; |
691 | ast_ptr<false, statement_sep_t> needSep; | 700 | ast_ptr<false, statement_sep_t> needSep; |
692 | AST_MEMBER(Statement, &content, &appendix, &needSep) | 701 | AST_MEMBER(Statement, &content, &appendix, &needSep) |
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index f7462dc..8dc5c94 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp | |||
@@ -25,6 +25,12 @@ extern "C" { | |||
25 | 25 | ||
26 | } // extern "C" | 26 | } // extern "C" |
27 | 27 | ||
28 | #if LUA_VERSION_NUM > 501 | ||
29 | #ifndef LUA_COMPAT_5_1 | ||
30 | #define lua_objlen lua_rawlen | ||
31 | #endif // LUA_COMPAT_5_1 | ||
32 | #endif // LUA_VERSION_NUM | ||
33 | |||
28 | namespace MoonP { | 34 | namespace MoonP { |
29 | using namespace std::string_view_literals; | 35 | using namespace std::string_view_literals; |
30 | using namespace parserlib; | 36 | using namespace parserlib; |
@@ -43,7 +49,7 @@ inline std::string s(std::string_view sv) { | |||
43 | } | 49 | } |
44 | 50 | ||
45 | const std::string_view version() { | 51 | const std::string_view version() { |
46 | return "0.4.0"sv; | 52 | return "0.4.1"sv; |
47 | } | 53 | } |
48 | 54 | ||
49 | // name of table stored in lua registry | 55 | // name of table stored in lua registry |
@@ -769,6 +775,7 @@ private: | |||
769 | case id<BreakLoop_t>(): transformBreakLoop(static_cast<BreakLoop_t*>(content), out); break; | 775 | case id<BreakLoop_t>(): transformBreakLoop(static_cast<BreakLoop_t*>(content), out); break; |
770 | case id<Label_t>(): transformLabel(static_cast<Label_t*>(content), out); break; | 776 | case id<Label_t>(): transformLabel(static_cast<Label_t*>(content), out); break; |
771 | case id<Goto_t>(): transformGoto(static_cast<Goto_t*>(content), out); break; | 777 | case id<Goto_t>(): transformGoto(static_cast<Goto_t*>(content), out); break; |
778 | case id<LocalAttrib_t>(): transformLocalAttrib(static_cast<LocalAttrib_t*>(content), out); break; | ||
772 | case id<ExpListAssign_t>(): { | 779 | case id<ExpListAssign_t>(): { |
773 | auto expListAssign = static_cast<ExpListAssign_t*>(content); | 780 | auto expListAssign = static_cast<ExpListAssign_t*>(content); |
774 | if (expListAssign->action) { | 781 | if (expListAssign->action) { |
@@ -2071,7 +2078,18 @@ private: | |||
2071 | last->needSep.set(nullptr); | 2078 | last->needSep.set(nullptr); |
2072 | auto bLast = ++nodes.rbegin(); | 2079 | auto bLast = ++nodes.rbegin(); |
2073 | if (bLast != nodes.rend()) { | 2080 | if (bLast != nodes.rend()) { |
2074 | static_cast<Statement_t*>(*bLast)->needSep.set(nullptr); | 2081 | bool isMacro = false; |
2082 | BLOCK_START | ||
2083 | BREAK_IF(expListLow->exprs.size() != 1); | ||
2084 | auto exp = static_cast<Exp_t*>(expListLow->exprs.back()); | ||
2085 | BREAK_IF(!exp->opValues.empty()); | ||
2086 | auto chainValue = exp->getByPath<unary_exp_t, Value_t, ChainValue_t>(); | ||
2087 | BREAK_IF(!chainValue); | ||
2088 | isMacro = isMacroChain(chainValue); | ||
2089 | BLOCK_END | ||
2090 | if (!isMacro) { | ||
2091 | ast_to<Statement_t>(*bLast)->needSep.set(nullptr); | ||
2092 | } | ||
2075 | } | 2093 | } |
2076 | BLOCK_END | 2094 | BLOCK_END |
2077 | break; | 2095 | break; |
@@ -5099,6 +5117,43 @@ private: | |||
5099 | out.push_back(join(temp)); | 5117 | out.push_back(join(temp)); |
5100 | } | 5118 | } |
5101 | 5119 | ||
5120 | void transformLocalAttrib(LocalAttrib_t* localAttrib, str_list& out) { | ||
5121 | auto x = localAttrib; | ||
5122 | auto attrib = _parser.toString(localAttrib->attrib); | ||
5123 | if (attrib != "close"sv && attrib != "const"sv) { | ||
5124 | throw std::logic_error(_info.errorMessage(s("unknown attribute '"sv) + attrib + '\'', localAttrib->attrib)); | ||
5125 | } | ||
5126 | auto expList = x->new_ptr<ExpList_t>(); | ||
5127 | str_list tmpVars; | ||
5128 | str_list vars; | ||
5129 | for (auto name : localAttrib->nameList->names.objects()) { | ||
5130 | auto callable = x->new_ptr<Callable_t>(); | ||
5131 | callable->item.set(name); | ||
5132 | auto chainValue = x->new_ptr<ChainValue_t>(); | ||
5133 | chainValue->items.push_back(callable); | ||
5134 | auto value = x->new_ptr<Value_t>(); | ||
5135 | value->item.set(chainValue); | ||
5136 | auto exp = newExp(value, x); | ||
5137 | expList->exprs.push_back(exp); | ||
5138 | tmpVars.push_back(getUnusedName("_var_"sv)); | ||
5139 | vars.push_back(_parser.toString(name)); | ||
5140 | } | ||
5141 | auto tmpVarStr = join(tmpVars, ","sv); | ||
5142 | auto tmpVarList = toAst<ExpList_t>(tmpVarStr, x); | ||
5143 | auto assignment = x->new_ptr<ExpListAssign_t>(); | ||
5144 | assignment->expList.set(tmpVarList); | ||
5145 | assignment->action.set(localAttrib->assign); | ||
5146 | str_list temp; | ||
5147 | transformAssignment(assignment, temp); | ||
5148 | attrib = s(" <"sv) + attrib + '>'; | ||
5149 | for (auto& var : vars) { | ||
5150 | forceAddToScope(var); | ||
5151 | var.append(attrib); | ||
5152 | } | ||
5153 | temp.push_back(indent() + s("local "sv) + join(vars) + s(" = "sv) + tmpVarStr + nll(x)); | ||
5154 | out.push_back(join(temp)); | ||
5155 | } | ||
5156 | |||
5102 | void transformBreakLoop(BreakLoop_t* breakLoop, str_list& out) { | 5157 | void transformBreakLoop(BreakLoop_t* breakLoop, str_list& out) { |
5103 | auto keyword = _parser.toString(breakLoop); | 5158 | auto keyword = _parser.toString(breakLoop); |
5104 | if (keyword == "break"sv) { | 5159 | if (keyword == "break"sv) { |
diff --git a/src/MoonP/moon_parser.cpp b/src/MoonP/moon_parser.cpp index 25e67b3..4dee5b1 100644 --- a/src/MoonP/moon_parser.cpp +++ b/src/MoonP/moon_parser.cpp | |||
@@ -174,6 +174,14 @@ MoonParser::MoonParser() { | |||
174 | local_values = NameList >> -(sym('=') >> (TableBlock | ExpListLow)); | 174 | local_values = NameList >> -(sym('=') >> (TableBlock | ExpListLow)); |
175 | Local = key("local") >> (Space >> local_flag | local_values); | 175 | Local = key("local") >> (Space >> local_flag | local_values); |
176 | 176 | ||
177 | LocalAttrib = and_(key(pl::user(Name, [](const item_t& item) { | ||
178 | State* st = reinterpret_cast<State*>(item.user_data); | ||
179 | for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it); | ||
180 | auto it = Keywords.find(st->buffer); | ||
181 | st->buffer.clear(); | ||
182 | return it == Keywords.end(); | ||
183 | })) >> NameList >> sym('=') >> not_('=')) >> Space >> Name >> NameList >> Assign; | ||
184 | |||
177 | colon_import_name = sym('\\') >> Space >> Variable; | 185 | colon_import_name = sym('\\') >> Space >> Variable; |
178 | ImportName = colon_import_name | Space >> Variable; | 186 | ImportName = colon_import_name | Space >> Variable; |
179 | ImportNameList = Seperator >> *SpaceBreak >> ImportName >> *((+SpaceBreak | sym(',') >> *SpaceBreak) >> ImportName); | 187 | ImportNameList = Seperator >> *SpaceBreak >> ImportName >> *((+SpaceBreak | sym(',') >> *SpaceBreak) >> ImportName); |
@@ -552,7 +560,8 @@ MoonParser::MoonParser() { | |||
552 | Statement = ( | 560 | Statement = ( |
553 | Import | While | Repeat | For | ForEach | | 561 | Import | While | Repeat | For | ForEach | |
554 | Return | Local | Global | Export | Macro | | 562 | Return | Local | Global | Export | Macro | |
555 | Space >> BreakLoop | Label | Goto | Backcall | ExpListAssign | 563 | Space >> BreakLoop | Label | Goto | Backcall | |
564 | LocalAttrib | ExpListAssign | ||
556 | ) >> Space >> | 565 | ) >> Space >> |
557 | -statement_appendix >> -statement_sep; | 566 | -statement_appendix >> -statement_sep; |
558 | 567 | ||
diff --git a/src/MoonP/moon_parser.h b/src/MoonP/moon_parser.h index 5787438..c6a03f8 100644 --- a/src/MoonP/moon_parser.h +++ b/src/MoonP/moon_parser.h | |||
@@ -199,6 +199,7 @@ private: | |||
199 | AST_RULE(local_flag) | 199 | AST_RULE(local_flag) |
200 | AST_RULE(local_values) | 200 | AST_RULE(local_values) |
201 | AST_RULE(Local) | 201 | AST_RULE(Local) |
202 | AST_RULE(LocalAttrib); | ||
202 | AST_RULE(colon_import_name) | 203 | AST_RULE(colon_import_name) |
203 | AST_RULE(import_literal_inner) | 204 | AST_RULE(import_literal_inner) |
204 | AST_RULE(ImportLiteral) | 205 | AST_RULE(ImportLiteral) |
diff --git a/src/MoonP/stacktraceplus.h b/src/MoonP/stacktraceplus.h index 3d32322..d658c5c 100644 --- a/src/MoonP/stacktraceplus.h +++ b/src/MoonP/stacktraceplus.h | |||
@@ -406,8 +406,8 @@ function _M.stacktrace(thread, message, level) | |||
406 | end | 406 | end |
407 | end | 407 | end |
408 | if fname then | 408 | if fname then |
409 | fname = fname:gsub("%[string \"", "") | 409 | local fn = fname:match("%[string \"(.-)\"%]") |
410 | fname = fname:gsub("\"%]", "") | 410 | if fn then fname = fn end |
411 | fname = fname:gsub("^%s*(.-)%s*$", "%1") | 411 | fname = fname:gsub("^%s*(.-)%s*$", "%1") |
412 | fname, line = getMoonLineNumber(fname, line) | 412 | fname, line = getMoonLineNumber(fname, line) |
413 | if _M.simplified then | 413 | if _M.simplified then |