diff options
author | Li Jin <dragon-fly@qq.com> | 2020-09-11 14:55:22 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2020-09-11 14:55:22 +0800 |
commit | dfd4e3b2b2abc0137d26ed3df303a398741bb6a0 (patch) | |
tree | 59a1690ad02d56d3f303f6ac1dea9e5d6cdc8f20 | |
parent | eba73a696c65932534fd5aefbe2ca4c7260c0fb6 (diff) | |
download | yuescript-dfd4e3b2b2abc0137d26ed3df303a398741bb6a0.tar.gz yuescript-dfd4e3b2b2abc0137d26ed3df303a398741bb6a0.tar.bz2 yuescript-dfd4e3b2b2abc0137d26ed3df303a398741bb6a0.zip |
add implicit objects support while writing tables.
-rw-r--r-- | spec/inputs/tables.moon | 20 | ||||
-rw-r--r-- | src/MoonP/moon_ast.h | 8 | ||||
-rw-r--r-- | src/MoonP/moon_compiler.cpp | 7 | ||||
-rw-r--r-- | src/MoonP/moon_parser.cpp | 10 | ||||
-rw-r--r-- | src/MoonP/moon_parser.h | 1 |
5 files changed, 40 insertions, 6 deletions
diff --git a/spec/inputs/tables.moon b/spec/inputs/tables.moon index 9383538..a1e861b 100644 --- a/spec/inputs/tables.moon +++ b/spec/inputs/tables.moon | |||
@@ -159,5 +159,25 @@ f = { "one", "two": three, "four" } | |||
159 | 159 | ||
160 | j = "one", "two": three, "four": five, 6, 7 | 160 | j = "one", "two": three, "four": five, 6, 7 |
161 | 161 | ||
162 | heroine = | ||
163 | name: "Christina" | ||
164 | age: 18 | ||
165 | job: "Princess" | ||
166 | likes: | ||
167 | # name: "kittens" | ||
168 | img: "/image/kittens.png" | ||
169 | # name: "flower" | ||
170 | img: "/image/flower.png" | ||
171 | items: | ||
172 | # name: "ring" | ||
173 | amount: 2 | ||
174 | # name: "necklace" | ||
175 | amount: 1 | ||
176 | status: | ||
177 | desc: "weak" | ||
178 | # attribute: "health" | ||
179 | value: 50 | ||
180 | # attribute: "mana" | ||
181 | value: 100 | ||
162 | 182 | ||
163 | nil | 183 | nil |
diff --git a/src/MoonP/moon_ast.h b/src/MoonP/moon_ast.h index 91d7614..16f7801 100644 --- a/src/MoonP/moon_ast.h +++ b/src/MoonP/moon_ast.h | |||
@@ -527,9 +527,15 @@ AST_NODE(TableLit) | |||
527 | AST_MEMBER(TableLit, &sep, &values) | 527 | AST_MEMBER(TableLit, &sep, &values) |
528 | AST_END(TableLit) | 528 | AST_END(TableLit) |
529 | 529 | ||
530 | AST_NODE(TableBlockIndent) | ||
531 | ast_ptr<true, Seperator_t> sep; | ||
532 | ast_sel_list<false, variable_pair_t, normal_pair_t, TableBlockIndent_t> values; | ||
533 | AST_MEMBER(TableBlockIndent, &sep, &values) | ||
534 | AST_END(TableBlockIndent) | ||
535 | |||
530 | AST_NODE(TableBlock) | 536 | AST_NODE(TableBlock) |
531 | ast_ptr<true, Seperator_t> sep; | 537 | ast_ptr<true, Seperator_t> sep; |
532 | ast_sel_list<false, variable_pair_t, normal_pair_t> values; | 538 | ast_sel_list<false, variable_pair_t, normal_pair_t, TableBlockIndent_t> values; |
533 | AST_MEMBER(TableBlock, &sep, &values) | 539 | AST_MEMBER(TableBlock, &sep, &values) |
534 | AST_END(TableBlock) | 540 | AST_END(TableBlock) |
535 | 541 | ||
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index 2f16fff..406b412 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp | |||
@@ -49,7 +49,7 @@ inline std::string s(std::string_view sv) { | |||
49 | } | 49 | } |
50 | 50 | ||
51 | const std::string_view version() { | 51 | const std::string_view version() { |
52 | return "0.4.10"sv; | 52 | return "0.4.12"sv; |
53 | } | 53 | } |
54 | 54 | ||
55 | // name of table stored in lua registry | 55 | // name of table stored in lua registry |
@@ -4608,6 +4608,7 @@ private: | |||
4608 | case id<Exp_t>(): transformExp(static_cast<Exp_t*>(pair), temp, ExpUsage::Closure); break; | 4608 | case id<Exp_t>(): transformExp(static_cast<Exp_t*>(pair), temp, ExpUsage::Closure); break; |
4609 | case id<variable_pair_t>(): transform_variable_pair(static_cast<variable_pair_t*>(pair), temp); break; | 4609 | case id<variable_pair_t>(): transform_variable_pair(static_cast<variable_pair_t*>(pair), temp); break; |
4610 | case id<normal_pair_t>(): transform_normal_pair(static_cast<normal_pair_t*>(pair), temp); break; | 4610 | case id<normal_pair_t>(): transform_normal_pair(static_cast<normal_pair_t*>(pair), temp); break; |
4611 | case id<TableBlockIndent_t>(): transformTableBlockIndent(static_cast<TableBlockIndent_t*>(pair), temp); break; | ||
4611 | default: assert(false); break; | 4612 | default: assert(false); break; |
4612 | } | 4613 | } |
4613 | temp.back() = indent() + temp.back() + (pair == pairs.back() ? Empty : s(","sv)) + nll(pair); | 4614 | temp.back() = indent() + temp.back() + (pair == pairs.back() ? Empty : s(","sv)) + nll(pair); |
@@ -4728,6 +4729,10 @@ private: | |||
4728 | addToScope(varName); | 4729 | addToScope(varName); |
4729 | } | 4730 | } |
4730 | 4731 | ||
4732 | void transformTableBlockIndent(TableBlockIndent_t* table, str_list& out) { | ||
4733 | transformTable(table, table->values.objects(), out); | ||
4734 | } | ||
4735 | |||
4731 | void transformTableBlock(TableBlock_t* table, str_list& out) { | 4736 | void transformTableBlock(TableBlock_t* table, str_list& out) { |
4732 | transformTable(table, table->values.objects(), out); | 4737 | transformTable(table, table->values.objects(), out); |
4733 | } | 4738 | } |
diff --git a/src/MoonP/moon_parser.cpp b/src/MoonP/moon_parser.cpp index a13a779..53a6b8f 100644 --- a/src/MoonP/moon_parser.cpp +++ b/src/MoonP/moon_parser.cpp | |||
@@ -430,12 +430,14 @@ MoonParser::MoonParser() { | |||
430 | Space | 430 | Space |
431 | ); | 431 | ); |
432 | 432 | ||
433 | TableBlockInner = Seperator >> KeyValueLine >> *(+(SpaceBreak) >> KeyValueLine); | 433 | TableBlockInner = Seperator >> KeyValueLine >> *(+SpaceBreak >> KeyValueLine); |
434 | TableBlock = +(SpaceBreak) >> Advance >> ensure(TableBlockInner, PopIndent); | 434 | TableBlock = +SpaceBreak >> Advance >> ensure(TableBlockInner, PopIndent); |
435 | TableBlockIndent = sym('#') >> Seperator >> KeyValueList >> -sym(',') >> | ||
436 | -(+SpaceBreak >> Advance >> ensure(KeyValueList >> -sym(',') >> *(+SpaceBreak >> KeyValueLine), PopIndent)); | ||
435 | 437 | ||
436 | class_member_list = Seperator >> KeyValue >> *(sym(',') >> KeyValue); | 438 | class_member_list = Seperator >> KeyValue >> *(sym(',') >> KeyValue); |
437 | ClassLine = CheckIndent >> (class_member_list | Statement) >> -sym(','); | 439 | ClassLine = CheckIndent >> (class_member_list | Statement) >> -sym(','); |
438 | ClassBlock = +(SpaceBreak) >> Advance >>Seperator >> ClassLine >> *(+(SpaceBreak) >> ClassLine) >> PopIndent; | 440 | ClassBlock = +SpaceBreak >> Advance >> Seperator >> ClassLine >> *(+SpaceBreak >> ClassLine) >> PopIndent; |
439 | 441 | ||
440 | ClassDecl = | 442 | ClassDecl = |
441 | key("class") >> not_(expr(':')) >> | 443 | key("class") >> not_(expr(':')) >> |
@@ -489,7 +491,7 @@ MoonParser::MoonParser() { | |||
489 | }); | 491 | }); |
490 | 492 | ||
491 | KeyValueList = KeyValue >> *(sym(',') >> KeyValue); | 493 | KeyValueList = KeyValue >> *(sym(',') >> KeyValue); |
492 | KeyValueLine = CheckIndent >> KeyValueList >> -sym(','); | 494 | KeyValueLine = CheckIndent >> (KeyValueList >> -sym(',') | TableBlockIndent); |
493 | 495 | ||
494 | FnArgDef = (Variable | SelfName) >> -(sym('=') >> Space >> Exp); | 496 | FnArgDef = (Variable | SelfName) >> -(sym('=') >> Space >> Exp); |
495 | 497 | ||
diff --git a/src/MoonP/moon_parser.h b/src/MoonP/moon_parser.h index c6a03f8..6b7f224 100644 --- a/src/MoonP/moon_parser.h +++ b/src/MoonP/moon_parser.h | |||
@@ -264,6 +264,7 @@ private: | |||
264 | AST_RULE(existential_op) | 264 | AST_RULE(existential_op) |
265 | AST_RULE(TableLit) | 265 | AST_RULE(TableLit) |
266 | AST_RULE(TableBlock) | 266 | AST_RULE(TableBlock) |
267 | AST_RULE(TableBlockIndent) | ||
267 | AST_RULE(class_member_list) | 268 | AST_RULE(class_member_list) |
268 | AST_RULE(ClassBlock) | 269 | AST_RULE(ClassBlock) |
269 | AST_RULE(ClassDecl) | 270 | AST_RULE(ClassDecl) |