From 417ec1a37922c6178900adfec70628cad46731ff Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 31 Oct 2022 11:32:33 +0800 Subject: fix issue #112 and issue #113. --- src/yuescript/yue_ast.h | 8 +++++++- src/yuescript/yue_compiler.cpp | 34 +++++++++++++++++++++++++++++++--- src/yuescript/yue_parser.cpp | 18 ++++++++++-------- src/yuescript/yue_parser.h | 4 ++++ 4 files changed, 52 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index ed963be..0925f5a 100755 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -798,6 +798,12 @@ AST_NODE(if_line) AST_MEMBER(if_line, &type, &condition) AST_END(if_line, "if_line"sv) +AST_NODE(while_line) + ast_ptr type; + ast_ptr condition; + AST_MEMBER(while_line, &type, &condition) +AST_END(while_line, "while_line"sv) + AST_LEAF(BreakLoop) AST_END(BreakLoop, "break_loop"sv) @@ -808,7 +814,7 @@ AST_NODE(PipeBody) AST_END(PipeBody, "pipe_body"sv) AST_NODE(statement_appendix) - ast_sel item; + ast_sel item; AST_MEMBER(statement_appendix, &item) AST_END(statement_appendix, "statement_appendix"sv) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index ff55e0e..22a4a02 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -60,7 +60,7 @@ namespace yue { typedef std::list str_list; -const std::string_view version = "0.15.6"sv; +const std::string_view version = "0.15.7"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -1130,6 +1130,10 @@ private: transformStatement(statement, out); return; } + case id(): { + throw std::logic_error(_info.errorMessage("while-loop line decorator is not supported here"sv, appendix->item.get())); + break; + } case id(): { throw std::logic_error(_info.errorMessage("for-loop line decorator is not supported here"sv, appendix->item.get())); break; @@ -1160,6 +1164,27 @@ private: statement->content.set(expListAssign); break; } + case id(): { + auto while_line = static_cast(appendix->item.get()); + auto whileNode = x->new_ptr(); + whileNode->type.set(while_line->type); + whileNode->condition.set(while_line->condition); + + auto stmt = x->new_ptr(); + stmt->content.set(statement->content); + whileNode->body.set(stmt); + + statement->appendix.set(nullptr); + auto simpleValue = x->new_ptr(); + simpleValue->value.set(whileNode); + auto exp = newExp(simpleValue, x); + auto expList = x->new_ptr(); + expList->exprs.push_back(exp); + auto expListAssign = x->new_ptr(); + expListAssign->expList.set(expList); + statement->content.set(expListAssign); + break; + } case id(): { auto compInner = appendix->item.to(); auto comp = x->new_ptr(); @@ -3445,7 +3470,8 @@ private: BREAK_IF(!last); auto x = last; auto expList = expListFrom(last); - BREAK_IF(!expList || (last->appendix && last->appendix->item.is())); + BREAK_IF(!expList); + BREAK_IF(last->appendix && !last->appendix->item.is()); auto expListLow = x->new_ptr(); expListLow->exprs.dup(expList->exprs); auto returnNode = x->new_ptr(); @@ -5100,7 +5126,9 @@ private: } void transformNum(Num_t* num, str_list& out) { - out.push_back(_parser.toString(num)); + std::string numStr = _parser.toString(num); + numStr.erase(std::remove(numStr.begin(), numStr.end(), '_'), numStr.end()); + out.push_back(numStr); } bool hasSpreadExp(const node_container& items) { diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index ef700ae..07fcd27 100755 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -50,21 +50,22 @@ YueParser::YueParser() { EmptyLine = SpaceBreak; AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; - num_expo = set("eE") >> -expr('-') >> +range('0', '9'); + num_expo = set("eE") >> -expr('-') >> num_char; lj_num = -set("uU") >> set("lL") >> set("lL"); + num_char = range('0', '9') >> *(range('0', '9') | expr('_') >> and_(range('0', '9'))); + num_char_hex = range('0', '9') | range('a', 'f') | range('A', 'F'); + num_lit = num_char_hex >> *(num_char_hex | expr('_') >> and_(num_char_hex)); Num = ( - "0x" >> - +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> - -lj_num + "0x" >> +num_lit >> -lj_num ) | ( - +range('0', '9') >> ( - '.' >> +range('0', '9') >> -num_expo | + +num_char >> ( + '.' >> +num_char >> -num_expo | num_expo | lj_num | true_() ) ) | ( - '.' >> +range('0', '9') >> -num_expo + '.' >> +num_char >> -num_expo ); Cut = false_(); @@ -640,6 +641,7 @@ YueParser::YueParser() { ExpListAssign = ExpList >> -(Update | Assign); if_line = Space >> IfType >> IfCond; + while_line = Space >> WhileType >> Exp; YueLineComment = *(not_(set("\r\n")) >> Any); yue_line_comment = expr("--") >> YueLineComment >> and_(Stop); @@ -647,7 +649,7 @@ YueParser::YueParser() { yue_multiline_comment = multi_line_open >> YueMultilineComment >> multi_line_close; yue_comment = check_indent >> (yue_multiline_comment >> *(set(" \t") | yue_multiline_comment) >> -yue_line_comment | yue_line_comment) >> and_(Break); - statement_appendix = (if_line | CompInner) >> Space; + statement_appendix = (if_line | while_line | CompInner) >> Space; statement_sep = and_(*SpaceBreak >> CheckIndent >> Space >> (set("($'\"") | expr("[[") | expr("[="))); Statement = Seperator >> -(yue_comment >> *(Break >> yue_comment) >> Break >> CheckIndent) >> Space >> ( Import | While | Repeat | For | ForEach | diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index 62f507e..750341b 100755 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -101,6 +101,9 @@ private: return Cut; } + rule num_char; + rule num_char_hex; + rule num_lit; rule num_expo; rule lj_num; rule plain_space; @@ -333,6 +336,7 @@ private: AST_RULE(unary_exp) AST_RULE(ExpListAssign) AST_RULE(if_line) + AST_RULE(while_line) AST_RULE(BreakLoop) AST_RULE(statement_appendix) AST_RULE(statement_sep) -- cgit v1.2.3-55-g6feb