aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-10-31 11:32:33 +0800
committerLi Jin <dragon-fly@qq.com>2022-11-09 11:29:32 +0800
commit417ec1a37922c6178900adfec70628cad46731ff (patch)
treea5a2d74927ad2c41b5a16264a78409e1c0334b72 /src
parent3dd607c8887d2fe0186668aabca31bb84a41e2da (diff)
downloadyuescript-417ec1a37922c6178900adfec70628cad46731ff.tar.gz
yuescript-417ec1a37922c6178900adfec70628cad46731ff.tar.bz2
yuescript-417ec1a37922c6178900adfec70628cad46731ff.zip
fix issue #112 and issue #113.
Diffstat (limited to 'src')
-rwxr-xr-xsrc/yuescript/yue_ast.h8
-rw-r--r--src/yuescript/yue_compiler.cpp34
-rwxr-xr-xsrc/yuescript/yue_parser.cpp18
-rwxr-xr-xsrc/yuescript/yue_parser.h4
4 files changed, 52 insertions, 12 deletions
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)
798 AST_MEMBER(if_line, &type, &condition) 798 AST_MEMBER(if_line, &type, &condition)
799AST_END(if_line, "if_line"sv) 799AST_END(if_line, "if_line"sv)
800 800
801AST_NODE(while_line)
802 ast_ptr<true, WhileType_t> type;
803 ast_ptr<true, Exp_t> condition;
804 AST_MEMBER(while_line, &type, &condition)
805AST_END(while_line, "while_line"sv)
806
801AST_LEAF(BreakLoop) 807AST_LEAF(BreakLoop)
802AST_END(BreakLoop, "break_loop"sv) 808AST_END(BreakLoop, "break_loop"sv)
803 809
@@ -808,7 +814,7 @@ AST_NODE(PipeBody)
808AST_END(PipeBody, "pipe_body"sv) 814AST_END(PipeBody, "pipe_body"sv)
809 815
810AST_NODE(statement_appendix) 816AST_NODE(statement_appendix)
811 ast_sel<true, if_line_t, CompInner_t> item; 817 ast_sel<true, if_line_t, while_line_t, CompInner_t> item;
812 AST_MEMBER(statement_appendix, &item) 818 AST_MEMBER(statement_appendix, &item)
813AST_END(statement_appendix, "statement_appendix"sv) 819AST_END(statement_appendix, "statement_appendix"sv)
814 820
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 {
60 60
61typedef std::list<std::string> str_list; 61typedef std::list<std::string> str_list;
62 62
63const std::string_view version = "0.15.6"sv; 63const std::string_view version = "0.15.7"sv;
64const std::string_view extension = "yue"sv; 64const std::string_view extension = "yue"sv;
65 65
66class YueCompilerImpl { 66class YueCompilerImpl {
@@ -1130,6 +1130,10 @@ private:
1130 transformStatement(statement, out); 1130 transformStatement(statement, out);
1131 return; 1131 return;
1132 } 1132 }
1133 case id<while_line_t>(): {
1134 throw std::logic_error(_info.errorMessage("while-loop line decorator is not supported here"sv, appendix->item.get()));
1135 break;
1136 }
1133 case id<CompInner_t>(): { 1137 case id<CompInner_t>(): {
1134 throw std::logic_error(_info.errorMessage("for-loop line decorator is not supported here"sv, appendix->item.get())); 1138 throw std::logic_error(_info.errorMessage("for-loop line decorator is not supported here"sv, appendix->item.get()));
1135 break; 1139 break;
@@ -1160,6 +1164,27 @@ private:
1160 statement->content.set(expListAssign); 1164 statement->content.set(expListAssign);
1161 break; 1165 break;
1162 } 1166 }
1167 case id<while_line_t>(): {
1168 auto while_line = static_cast<while_line_t*>(appendix->item.get());
1169 auto whileNode = x->new_ptr<While_t>();
1170 whileNode->type.set(while_line->type);
1171 whileNode->condition.set(while_line->condition);
1172
1173 auto stmt = x->new_ptr<Statement_t>();
1174 stmt->content.set(statement->content);
1175 whileNode->body.set(stmt);
1176
1177 statement->appendix.set(nullptr);
1178 auto simpleValue = x->new_ptr<SimpleValue_t>();
1179 simpleValue->value.set(whileNode);
1180 auto exp = newExp(simpleValue, x);
1181 auto expList = x->new_ptr<ExpList_t>();
1182 expList->exprs.push_back(exp);
1183 auto expListAssign = x->new_ptr<ExpListAssign_t>();
1184 expListAssign->expList.set(expList);
1185 statement->content.set(expListAssign);
1186 break;
1187 }
1163 case id<CompInner_t>(): { 1188 case id<CompInner_t>(): {
1164 auto compInner = appendix->item.to<CompInner_t>(); 1189 auto compInner = appendix->item.to<CompInner_t>();
1165 auto comp = x->new_ptr<Comprehension_t>(); 1190 auto comp = x->new_ptr<Comprehension_t>();
@@ -3445,7 +3470,8 @@ private:
3445 BREAK_IF(!last); 3470 BREAK_IF(!last);
3446 auto x = last; 3471 auto x = last;
3447 auto expList = expListFrom(last); 3472 auto expList = expListFrom(last);
3448 BREAK_IF(!expList || (last->appendix && last->appendix->item.is<CompInner_t>())); 3473 BREAK_IF(!expList);
3474 BREAK_IF(last->appendix && !last->appendix->item.is<if_line_t>());
3449 auto expListLow = x->new_ptr<ExpListLow_t>(); 3475 auto expListLow = x->new_ptr<ExpListLow_t>();
3450 expListLow->exprs.dup(expList->exprs); 3476 expListLow->exprs.dup(expList->exprs);
3451 auto returnNode = x->new_ptr<Return_t>(); 3477 auto returnNode = x->new_ptr<Return_t>();
@@ -5100,7 +5126,9 @@ private:
5100 } 5126 }
5101 5127
5102 void transformNum(Num_t* num, str_list& out) { 5128 void transformNum(Num_t* num, str_list& out) {
5103 out.push_back(_parser.toString(num)); 5129 std::string numStr = _parser.toString(num);
5130 numStr.erase(std::remove(numStr.begin(), numStr.end(), '_'), numStr.end());
5131 out.push_back(numStr);
5104 } 5132 }
5105 5133
5106 bool hasSpreadExp(const node_container& items) { 5134 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() {
50 EmptyLine = SpaceBreak; 50 EmptyLine = SpaceBreak;
51 AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; 51 AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_';
52 Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; 52 Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum;
53 num_expo = set("eE") >> -expr('-') >> +range('0', '9'); 53 num_expo = set("eE") >> -expr('-') >> num_char;
54 lj_num = -set("uU") >> set("lL") >> set("lL"); 54 lj_num = -set("uU") >> set("lL") >> set("lL");
55 num_char = range('0', '9') >> *(range('0', '9') | expr('_') >> and_(range('0', '9')));
56 num_char_hex = range('0', '9') | range('a', 'f') | range('A', 'F');
57 num_lit = num_char_hex >> *(num_char_hex | expr('_') >> and_(num_char_hex));
55 Num = ( 58 Num = (
56 "0x" >> 59 "0x" >> +num_lit >> -lj_num
57 +(range('0', '9') | range('a', 'f') | range('A', 'F')) >>
58 -lj_num
59 ) | ( 60 ) | (
60 +range('0', '9') >> ( 61 +num_char >> (
61 '.' >> +range('0', '9') >> -num_expo | 62 '.' >> +num_char >> -num_expo |
62 num_expo | 63 num_expo |
63 lj_num | 64 lj_num |
64 true_() 65 true_()
65 ) 66 )
66 ) | ( 67 ) | (
67 '.' >> +range('0', '9') >> -num_expo 68 '.' >> +num_char >> -num_expo
68 ); 69 );
69 70
70 Cut = false_(); 71 Cut = false_();
@@ -640,6 +641,7 @@ YueParser::YueParser() {
640 ExpListAssign = ExpList >> -(Update | Assign); 641 ExpListAssign = ExpList >> -(Update | Assign);
641 642
642 if_line = Space >> IfType >> IfCond; 643 if_line = Space >> IfType >> IfCond;
644 while_line = Space >> WhileType >> Exp;
643 645
644 YueLineComment = *(not_(set("\r\n")) >> Any); 646 YueLineComment = *(not_(set("\r\n")) >> Any);
645 yue_line_comment = expr("--") >> YueLineComment >> and_(Stop); 647 yue_line_comment = expr("--") >> YueLineComment >> and_(Stop);
@@ -647,7 +649,7 @@ YueParser::YueParser() {
647 yue_multiline_comment = multi_line_open >> YueMultilineComment >> multi_line_close; 649 yue_multiline_comment = multi_line_open >> YueMultilineComment >> multi_line_close;
648 yue_comment = check_indent >> (yue_multiline_comment >> *(set(" \t") | yue_multiline_comment) >> -yue_line_comment | yue_line_comment) >> and_(Break); 650 yue_comment = check_indent >> (yue_multiline_comment >> *(set(" \t") | yue_multiline_comment) >> -yue_line_comment | yue_line_comment) >> and_(Break);
649 651
650 statement_appendix = (if_line | CompInner) >> Space; 652 statement_appendix = (if_line | while_line | CompInner) >> Space;
651 statement_sep = and_(*SpaceBreak >> CheckIndent >> Space >> (set("($'\"") | expr("[[") | expr("[="))); 653 statement_sep = and_(*SpaceBreak >> CheckIndent >> Space >> (set("($'\"") | expr("[[") | expr("[=")));
652 Statement = Seperator >> -(yue_comment >> *(Break >> yue_comment) >> Break >> CheckIndent) >> Space >> ( 654 Statement = Seperator >> -(yue_comment >> *(Break >> yue_comment) >> Break >> CheckIndent) >> Space >> (
653 Import | While | Repeat | For | ForEach | 655 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:
101 return Cut; 101 return Cut;
102 } 102 }
103 103
104 rule num_char;
105 rule num_char_hex;
106 rule num_lit;
104 rule num_expo; 107 rule num_expo;
105 rule lj_num; 108 rule lj_num;
106 rule plain_space; 109 rule plain_space;
@@ -333,6 +336,7 @@ private:
333 AST_RULE(unary_exp) 336 AST_RULE(unary_exp)
334 AST_RULE(ExpListAssign) 337 AST_RULE(ExpListAssign)
335 AST_RULE(if_line) 338 AST_RULE(if_line)
339 AST_RULE(while_line)
336 AST_RULE(BreakLoop) 340 AST_RULE(BreakLoop)
337 AST_RULE(statement_appendix) 341 AST_RULE(statement_appendix)
338 AST_RULE(statement_sep) 342 AST_RULE(statement_sep)