aboutsummaryrefslogtreecommitdiff
path: root/MoonParser
diff options
context:
space:
mode:
Diffstat (limited to 'MoonParser')
-rw-r--r--MoonParser/ast.hpp9
-rw-r--r--MoonParser/moon_ast.cpp557
-rw-r--r--MoonParser/moon_ast.h43
-rw-r--r--MoonParser/moon_parser.cpp94
-rw-r--r--MoonParser/moon_parser.h6
5 files changed, 548 insertions, 161 deletions
diff --git a/MoonParser/ast.hpp b/MoonParser/ast.hpp
index d239eac..e7ae74f 100644
--- a/MoonParser/ast.hpp
+++ b/MoonParser/ast.hpp
@@ -367,13 +367,20 @@ public:
367 clear(); 367 clear();
368 } 368 }
369 369
370 void add(ast_node* node) { 370 void push_back(ast_node* node) {
371 if (accept(node)) { 371 if (accept(node)) {
372 m_objects.push_back(node); 372 m_objects.push_back(node);
373 node->retain(); 373 node->retain();
374 } 374 }
375 } 375 }
376 376
377 void push_front(ast_node* node) {
378 if (accept(node)) {
379 m_objects.push_front(node);
380 node->retain();
381 }
382 }
383
377 const container& objects() const { 384 const container& objects() const {
378 return m_objects; 385 return m_objects;
379 } 386 }
diff --git a/MoonParser/moon_ast.cpp b/MoonParser/moon_ast.cpp
index fa95097..af34395 100644
--- a/MoonParser/moon_ast.cpp
+++ b/MoonParser/moon_ast.cpp
@@ -23,8 +23,8 @@ const input& AstLeaf::getValue() {
23 ast<type##_t> __##type##_t(type); 23 ast<type##_t> __##type##_t(type);
24 24
25AST_IMPL(Num) 25AST_IMPL(Num)
26AST_IMPL(_Name)
27AST_IMPL(Name) 26AST_IMPL(Name)
27AST_IMPL(Variable)
28AST_IMPL(self) 28AST_IMPL(self)
29AST_IMPL(self_name) 29AST_IMPL(self_name)
30AST_IMPL(self_class) 30AST_IMPL(self_class)
@@ -235,7 +235,7 @@ private:
235 return !defined; 235 return !defined;
236 } 236 }
237 237
238 std::string getValidName(std::string_view name) { 238 std::string getUnusedName(std::string_view name) {
239 int index = 0; 239 int index = 0;
240 std::string newName; 240 std::string newName;
241 do { 241 do {
@@ -344,6 +344,71 @@ private:
344 return parse<T>(_codeCache.back(), r, el, &st); 344 return parse<T>(_codeCache.back(), r, el, &st);
345 } 345 }
346 346
347 Invoke_t* startWithInvoke(ast_node* chain) {
348 ast_node* invoke = nullptr;
349 chain->traverse([&](ast_node* node) {
350 switch (node->getId()) {
351 case "Invoke"_id:
352 invoke = node;
353 return traversal::Stop;
354 case "DotChainItem"_id:
355 case "ColonChainItem"_id:
356 case "Slice"_id:
357 case "Index"_id:
358 case "Callable"_id:
359 case "String"_id:
360 return traversal::Stop;
361 default:
362 return traversal::Continue;
363 }
364 });
365 return static_cast<Invoke_t*>(invoke);
366 }
367
368 Invoke_t* endWithInvoke(Chain_t* chain) {
369 ast_node* last = nullptr;
370 chain->traverse([&](ast_node* node) {
371 switch (node->getId()) {
372 case "Invoke"_id:
373 case "DotChainItem"_id:
374 case "ColonChainItem"_id:
375 case "Slice"_id:
376 case "Index"_id:
377 case "Callable"_id:
378 case "String"_id:
379 last = node;
380 return traversal::Return;
381 default:
382 return traversal::Continue;
383 }
384 });
385 if (last && last->getId() == "Invoke"_id) {
386 return static_cast<Invoke_t*>(last);
387 } else {
388 return nullptr;
389 }
390 }
391
392 std::vector<ast_node*> getChainList(ast_node* chain) {
393 std::vector<ast_node*> list;
394 chain->traverse([&](ast_node* node) {
395 switch (node->getId()) {
396 case "Invoke"_id:
397 case "DotChainItem"_id:
398 case "ColonChainItem"_id:
399 case "Slice"_id:
400 case "Index"_id:
401 case "Callable"_id:
402 case "String"_id:
403 list.push_back(node);
404 return traversal::Return;
405 default:
406 return traversal::Continue;
407 }
408 });
409 return list;
410 }
411
347 void transformStatement(Statement_t* statement, std::vector<std::string>& out) { 412 void transformStatement(Statement_t* statement, std::vector<std::string>& out) {
348 if (statement->appendix) { 413 if (statement->appendix) {
349 auto appendix = statement->appendix; 414 auto appendix = statement->appendix;
@@ -354,7 +419,7 @@ private:
354 ifCond->condition = if_else_line->condition; 419 ifCond->condition = if_else_line->condition;
355 420
356 auto exprList = new_ptr<ExpList_t>(); 421 auto exprList = new_ptr<ExpList_t>();
357 exprList->exprs.add(if_else_line->elseExpr); 422 exprList->exprs.push_back(if_else_line->elseExpr);
358 auto stmt = new_ptr<Statement_t>(); 423 auto stmt = new_ptr<Statement_t>();
359 stmt->content.set(exprList); 424 stmt->content.set(exprList);
360 auto body = new_ptr<Body_t>(); 425 auto body = new_ptr<Body_t>();
@@ -369,7 +434,7 @@ private:
369 auto ifNode = new_ptr<If_t>(); 434 auto ifNode = new_ptr<If_t>();
370 ifNode->firstCondition.set(ifCond); 435 ifNode->firstCondition.set(ifCond);
371 ifNode->firstBody.set(body); 436 ifNode->firstBody.set(body);
372 ifNode->branches.add(ifElseIf); 437 ifNode->branches.push_back(ifElseIf);
373 438
374 statement->appendix.set(nullptr); 439 statement->appendix.set(nullptr);
375 auto simpleValue = new_ptr<SimpleValue_t>(); 440 auto simpleValue = new_ptr<SimpleValue_t>();
@@ -379,7 +444,7 @@ private:
379 auto exp = new_ptr<Exp_t>(); 444 auto exp = new_ptr<Exp_t>();
380 exp->value.set(value); 445 exp->value.set(value);
381 exprList = new_ptr<ExpList_t>(); 446 exprList = new_ptr<ExpList_t>();
382 exprList->exprs.add(exp); 447 exprList->exprs.push_back(exp);
383 statement->content.set(exprList); 448 statement->content.set(exprList);
384 break; 449 break;
385 } 450 }
@@ -416,14 +481,38 @@ private:
416 break; 481 break;
417 } 482 }
418 if (auto singleValue = singleValueFrom(expList)) { 483 if (auto singleValue = singleValueFrom(expList)) {
419 if (auto ifNode = static_cast<If_t*>(singleValue->getByPath({"SimpleValue"_id, "If"_id}))) { 484 if (auto simpleValue = static_cast<SimpleValue_t*>(singleValue->getByPath({"SimpleValue"_id}))) {
420 transformIf(ifNode, out); 485 auto value = simpleValue->getFirstChild();
421 break; 486 bool specialSingleValue = true;
487 switch (value->getId()) {
488 case "If"_id: transformIf(static_cast<If_t*>(value), out); break;
489 case "ClassDecl"_id: transformClassDecl(static_cast<ClassDecl_t*>(value), out); break;
490 case "Unless"_id: transformUnless(static_cast<Unless_t*>(value), out); break;
491 case "Switch"_id: transformSwitch(static_cast<Switch_t*>(value), out); break;
492 case "With"_id: transformWith(static_cast<With_t*>(value), out); break;
493 case "ForEach"_id: transformForEach(static_cast<ForEach_t*>(value), out); break;
494 case "For"_id: transformFor(static_cast<For_t*>(value), out); break;
495 case "While"_id: transformWhile(static_cast<While_t*>(value), out); break;
496 case "Do"_id: transformDo(static_cast<Do_t*>(value), out); break;
497 default: specialSingleValue = false; break;
498 }
499 if (specialSingleValue) {
500 break;
501 }
422 } 502 }
423 if (singleValue->getByPath({"ChainValue"_id, "InvokeArgs"_id})) { 503 if (auto chainValue = static_cast<ChainValue_t*>(singleValue->getByPath({"ChainValue"_id}))) {
424 transformValue(singleValue, out); 504 if (chainValue->arguments) {
425 out.back() = indent() + out.back() + nlr(singleValue); 505 transformValue(singleValue, out);
426 break; 506 out.back() = indent() + out.back() + nlr(singleValue);
507 break;
508 } else {
509 auto chain = static_cast<Chain_t*>(chainValue->getByPath({"Chain"_id}));
510 if (chain && endWithInvoke(chain)) {
511 transformValue(singleValue, out);
512 out.back() = indent() + out.back() + nlr(singleValue);
513 break;
514 }
515 }
427 } 516 }
428 } 517 }
429 std::string preDefine; 518 std::string preDefine;
@@ -445,7 +534,7 @@ private:
445 std::vector<ast_node*> values; 534 std::vector<ast_node*> values;
446 expList->traverse([&](ast_node* child) { 535 expList->traverse([&](ast_node* child) {
447 if (child->getId() == "Value"_id) { 536 if (child->getId() == "Value"_id) {
448 auto target = child->getByPath({"ChainValue"_id, "Callable"_id, "Name"_id}); 537 auto target = child->getByPath({"ChainValue"_id, "Callable"_id, "Variable"_id});
449 if (target) { 538 if (target) {
450 auto name = toString(target); 539 auto name = toString(target);
451 if (addToScope(name)) { 540 if (addToScope(name)) {
@@ -557,6 +646,14 @@ private:
557 out.push_back(preDefine + nll(statement) + temp.front()); 646 out.push_back(preDefine + nll(statement) + temp.front());
558 return; 647 return;
559 } 648 }
649 case "ClassDecl"_id: {
650 std::vector<std::string> temp;
651 auto expList = assignment->assignable.get();
652 std::string preDefine = transformAssignDefs(expList);
653 transformClassDecl(static_cast<ClassDecl_t*>(valueItem), temp, ClassDeclUsage::Assignment, expList);
654 out.push_back(preDefine + nll(statement) + temp.front());
655 return;
656 }
560 } 657 }
561 } 658 }
562 } 659 }
@@ -575,7 +672,7 @@ private:
575 if (auto body = node->getByPath({"Body"_id})) { 672 if (auto body = node->getByPath({"Body"_id})) {
576 if (traversal::Stop == body->traverse([&](ast_node* n) { 673 if (traversal::Stop == body->traverse([&](ast_node* n) {
577 if (n->getId() == "Callable"_id) { 674 if (n->getId() == "Callable"_id) {
578 if (auto name = n->getByPath({"Name"_id})) { 675 if (auto name = n->getByPath({"Variable"_id})) {
579 if (temp.front() ==toString(name)) { 676 if (temp.front() ==toString(name)) {
580 return traversal::Stop; 677 return traversal::Stop;
581 } 678 }
@@ -721,7 +818,7 @@ private:
721 void transformCallable(Callable_t* callable, std::vector<std::string>& out, bool invoke) { 818 void transformCallable(Callable_t* callable, std::vector<std::string>& out, bool invoke) {
722 auto item = callable->item.get(); 819 auto item = callable->item.get();
723 switch (item->getId()) { 820 switch (item->getId()) {
724 case "Name"_id: transformName(static_cast<Name_t*>(item), out); break; 821 case "Variable"_id: transformVariable(static_cast<Variable_t*>(item), out); break;
725 case "SelfName"_id: transformSelfName(static_cast<SelfName_t*>(item), out, invoke); break; 822 case "SelfName"_id: transformSelfName(static_cast<SelfName_t*>(item), out, invoke); break;
726 case "VarArg"_id: out.push_back(s("..."sv)); break; 823 case "VarArg"_id: out.push_back(s("..."sv)); break;
727 case "Parens"_id: transformParens(static_cast<Parens_t*>(item), out); break; 824 case "Parens"_id: transformParens(static_cast<Parens_t*>(item), out); break;
@@ -742,7 +839,7 @@ private:
742 case "If"_id: transformIfClosure(static_cast<If_t*>(value), out); break; 839 case "If"_id: transformIfClosure(static_cast<If_t*>(value), out); break;
743 case "Switch"_id: transformSwitch(value, out); break; 840 case "Switch"_id: transformSwitch(value, out); break;
744 case "With"_id: transformWith(value, out); break; 841 case "With"_id: transformWith(value, out); break;
745 case "ClassDecl"_id: transformClassDecl(static_cast<ClassDecl_t*>(value), out); break; 842 case "ClassDecl"_id: transformClassDeclClosure(static_cast<ClassDecl_t*>(value), out); break;
746 case "ForEach"_id: transformForEachClosure(static_cast<ForEach_t*>(value), out); break; 843 case "ForEach"_id: transformForEachClosure(static_cast<ForEach_t*>(value), out); break;
747 case "For"_id: transformForClosure(static_cast<For_t*>(value), out); break; 844 case "For"_id: transformForClosure(static_cast<For_t*>(value), out); break;
748 case "While"_id: transformWhile(value, out); break; 845 case "While"_id: transformWhile(value, out); break;
@@ -829,6 +926,8 @@ private:
829 if (auto singleValue = singleValueFrom(valueList)) { 926 if (auto singleValue = singleValueFrom(valueList)) {
830 if (auto comp = singleValue->getByPath({"SimpleValue"_id, "Comprehension"_id})) { 927 if (auto comp = singleValue->getByPath({"SimpleValue"_id, "Comprehension"_id})) {
831 transformCompReturn(static_cast<Comprehension_t*>(comp), out); 928 transformCompReturn(static_cast<Comprehension_t*>(comp), out);
929 } else if (auto classDecl = singleValue->getByPath({"SimpleValue"_id, "ClassDecl"_id})) {
930 transformClassDecl(static_cast<ClassDecl_t*>(classDecl), out, ClassDeclUsage::Return);
832 } else { 931 } else {
833 transformValue(singleValue, out); 932 transformValue(singleValue, out);
834 out.back() = indent() + s("return "sv) + out.back() + nlr(returnNode); 933 out.back() = indent() + s("return "sv) + out.back() + nlr(returnNode);
@@ -877,7 +976,7 @@ private:
877 auto def = static_cast<FnArgDef_t*>(_def); 976 auto def = static_cast<FnArgDef_t*>(_def);
878 auto& arg = argItems.emplace_back(); 977 auto& arg = argItems.emplace_back();
879 switch (def->name->getId()) { 978 switch (def->name->getId()) {
880 case "Name"_id: arg.name = toString(def->name); break; 979 case "Variable"_id: arg.name = toString(def->name); break;
881 case "SelfName"_id: { 980 case "SelfName"_id: {
882 assignSelf = true; 981 assignSelf = true;
883 auto selfName = static_cast<SelfName_t*>(def->name.get()); 982 auto selfName = static_cast<SelfName_t*>(def->name.get());
@@ -971,7 +1070,10 @@ private:
971 std::vector<std::string> temp; 1070 std::vector<std::string> temp;
972 auto caller = chain_call->caller.get(); 1071 auto caller = chain_call->caller.get();
973 switch (caller->getId()) { 1072 switch (caller->getId()) {
974 case "Callable"_id: transformCallable(static_cast<Callable_t*>(caller), temp, true); break; 1073 case "Callable"_id: {
1074 transformCallable(static_cast<Callable_t*>(caller), temp, startWithInvoke(chain_call->chain));
1075 break;
1076 }
975 case "String"_id: transformString(static_cast<String_t*>(caller), temp); break; 1077 case "String"_id: transformString(static_cast<String_t*>(caller), temp); break;
976 default: break; 1078 default: break;
977 } 1079 }
@@ -1030,7 +1132,9 @@ private:
1030 1132
1031 void transformColonChain(ColonChain_t* colonChain, std::vector<std::string>& out) { 1133 void transformColonChain(ColonChain_t* colonChain, std::vector<std::string>& out) {
1032 std::vector<std::string> temp; 1134 std::vector<std::string> temp;
1033 temp.push_back(s(":"sv) + toString(colonChain->colonChain->name)); 1135 temp.push_back(
1136 s(colonChain->colonChain->switchToDot ? "."sv : ":"sv) +
1137 toString(colonChain->colonChain->name));
1034 if (colonChain->invokeChain) { 1138 if (colonChain->invokeChain) {
1035 transform_invoke_chain(colonChain->invokeChain, temp); 1139 transform_invoke_chain(colonChain->invokeChain, temp);
1036 } 1140 }
@@ -1053,7 +1157,7 @@ private:
1053 out.push_back(join(temp)); 1157 out.push_back(join(temp));
1054 } 1158 }
1055 1159
1056 void transformName(Name_t* name, std::vector<std::string>& out) { 1160 void transformVariable(Variable_t* name, std::vector<std::string>& out) {
1057 out.push_back(toString(name)); 1161 out.push_back(toString(name));
1058 } 1162 }
1059 1163
@@ -1086,8 +1190,8 @@ private:
1086 1190
1087 void transformComprehension(Comprehension_t* comp, std::vector<std::string>& out) { 1191 void transformComprehension(Comprehension_t* comp, std::vector<std::string>& out) {
1088 std::vector<std::string> temp; 1192 std::vector<std::string> temp;
1089 std::string accum = getValidName("_accum_"); 1193 std::string accum = getUnusedName("_accum_");
1090 std::string len = getValidName("_len_"); 1194 std::string len = getUnusedName("_len_");
1091 addToScope(accum); 1195 addToScope(accum);
1092 addToScope(len); 1196 addToScope(len);
1093 transformExp(comp->value, temp); 1197 transformExp(comp->value, temp);
@@ -1177,8 +1281,8 @@ private:
1177 switch (loopTarget->getId()) { 1281 switch (loopTarget->getId()) {
1178 case "star_exp"_id: { 1282 case "star_exp"_id: {
1179 auto star_exp = static_cast<star_exp_t*>(loopTarget); 1283 auto star_exp = static_cast<star_exp_t*>(loopTarget);
1180 auto listName = getValidName("_list_"); 1284 auto listName = getUnusedName("_list_");
1181 auto indexName = getValidName("_index_"); 1285 auto indexName = getUnusedName("_index_");
1182 addToScope(listName); 1286 addToScope(listName);
1183 addToScope(indexName); 1287 addToScope(indexName);
1184 transformExp(star_exp->value, temp); 1288 transformExp(star_exp->value, temp);
@@ -1211,8 +1315,8 @@ private:
1211 for (auto _item : nameList->items.objects()) { 1315 for (auto _item : nameList->items.objects()) {
1212 auto item = static_cast<NameOrDestructure_t*>(_item)->item.get(); 1316 auto item = static_cast<NameOrDestructure_t*>(_item)->item.get();
1213 switch (item->getId()) { 1317 switch (item->getId()) {
1214 case "Name"_id: 1318 case "Variable"_id:
1215 transformName(static_cast<Name_t*>(item), temp); 1319 transformVariable(static_cast<Variable_t*>(item), temp);
1216 break; 1320 break;
1217 case "TableLit"_id: 1321 case "TableLit"_id:
1218 transformTableLit(static_cast<TableLit_t*>(item), temp); 1322 transformTableLit(static_cast<TableLit_t*>(item), temp);
@@ -1262,8 +1366,8 @@ private:
1262 1366
1263 void transformForClosure(For_t* forNode, std::vector<std::string>& out) { 1367 void transformForClosure(For_t* forNode, std::vector<std::string>& out) {
1264 std::vector<std::string> temp; 1368 std::vector<std::string> temp;
1265 std::string accum = getValidName("_accum_"); 1369 std::string accum = getUnusedName("_accum_");
1266 std::string len = getValidName("_len_"); 1370 std::string len = getUnusedName("_len_");
1267 addToScope(accum); 1371 addToScope(accum);
1268 addToScope(len); 1372 addToScope(len);
1269 _buf << "(function()"sv << nll(forNode); 1373 _buf << "(function()"sv << nll(forNode);
@@ -1299,8 +1403,8 @@ private:
1299 1403
1300 void transformForInPlace(For_t* forNode, std::vector<std::string>& out, ExpList_t* assignExpList) { 1404 void transformForInPlace(For_t* forNode, std::vector<std::string>& out, ExpList_t* assignExpList) {
1301 std::vector<std::string> temp; 1405 std::vector<std::string> temp;
1302 std::string accum = getValidName("_accum_"); 1406 std::string accum = getUnusedName("_accum_");
1303 std::string len = getValidName("_len_"); 1407 std::string len = getUnusedName("_len_");
1304 _buf << indent() << "do"sv << nll(forNode); 1408 _buf << indent() << "do"sv << nll(forNode);
1305 pushScope(); 1409 pushScope();
1306 addToScope(accum); 1410 addToScope(accum);
@@ -1351,8 +1455,8 @@ private:
1351 1455
1352 void transformForEachClosure(ForEach_t* forEach, std::vector<std::string>& out) { 1456 void transformForEachClosure(ForEach_t* forEach, std::vector<std::string>& out) {
1353 std::vector<std::string> temp; 1457 std::vector<std::string> temp;
1354 std::string accum = getValidName("_accum_"); 1458 std::string accum = getUnusedName("_accum_");
1355 std::string len = getValidName("_len_"); 1459 std::string len = getUnusedName("_len_");
1356 addToScope(accum); 1460 addToScope(accum);
1357 addToScope(len); 1461 addToScope(len);
1358 _buf << "(function()"sv << nll(forEach); 1462 _buf << "(function()"sv << nll(forEach);
@@ -1388,8 +1492,8 @@ private:
1388 1492
1389 void transformForEachInPlace(ForEach_t* forEach, std::vector<std::string>& out, ExpList_t* assignExpList) { 1493 void transformForEachInPlace(ForEach_t* forEach, std::vector<std::string>& out, ExpList_t* assignExpList) {
1390 std::vector<std::string> temp; 1494 std::vector<std::string> temp;
1391 std::string accum = getValidName("_accum_"); 1495 std::string accum = getUnusedName("_accum_");
1392 std::string len = getValidName("_len_"); 1496 std::string len = getUnusedName("_len_");
1393 _buf << indent() << "do"sv << nll(forEach); 1497 _buf << indent() << "do"sv << nll(forEach);
1394 pushScope(); 1498 pushScope();
1395 addToScope(accum); 1499 addToScope(accum);
@@ -1462,7 +1566,7 @@ private:
1462 auto name = keyName->name.get(); 1566 auto name = keyName->name.get();
1463 switch (name->getId()) { 1567 switch (name->getId()) {
1464 case "SelfName"_id: transformSelfName(static_cast<SelfName_t*>(name), out, false); break; 1568 case "SelfName"_id: transformSelfName(static_cast<SelfName_t*>(name), out, false); break;
1465 case "_Name"_id: out.push_back(toString(name)); break; 1569 case "Name"_id: out.push_back(toString(name)); break;
1466 default: break; 1570 default: break;
1467 } 1571 }
1468 } 1572 }
@@ -1504,47 +1608,309 @@ private:
1504 } 1608 }
1505 } 1609 }
1506 1610
1507 void transformClassDecl(ClassDecl_t* classDecl, std::vector<std::string>& out) { 1611 std::pair<std::string,bool> defineClassVariable(Assignable_t* assignable) {
1508 std::vector<std::string> temp; 1612 if (assignable->item->getId() == "Variable"_id) {
1509 if (classDecl->name) { 1613 auto variable = static_cast<Variable_t*>(assignable->item.get());
1510 transformAssignable(classDecl->name, temp); 1614 auto name = toString(variable);
1511 } 1615 if (addToScope(name)) {
1512 if (classDecl->extend) { 1616 return {name, true};
1513 transformExp(classDecl->extend, temp); 1617 } else {
1514 } 1618 return {name, false};
1515 if (classDecl->body) { 1619 }
1516 transformClassBlock(classDecl->body, temp);
1517 } 1620 }
1518 out.push_back(join(temp, "\n"sv)); 1621 return {Empty, false};
1519 } 1622 }
1520 1623
1521 void transformClassBlock(ClassBlock_t* classBlock, std::vector<std::string>& out) { 1624 enum class ClassDeclUsage {
1625 Return,
1626 Assignment,
1627 Common
1628 };
1629
1630 void transformClassDeclClosure(ClassDecl_t* classDecl, std::vector<std::string>& out) {
1522 std::vector<std::string> temp; 1631 std::vector<std::string> temp;
1523 for (auto _line : classBlock->lines.objects()) { 1632 temp.push_back(s("(function()"sv) + nll(classDecl));
1524 auto line = static_cast<ClassLine_t*>(_line); 1633 pushScope();
1525 transformClassLine(line, temp); 1634 transformClassDecl(classDecl, temp, ClassDeclUsage::Return);
1526 } 1635 popScope();
1527 out.push_back(join(temp,"\n"sv)); 1636 temp.push_back(s("end)()"sv));
1637 out.push_back(join(temp));
1528 } 1638 }
1529 1639
1530 void transformClassLine(ClassLine_t* classLine, std::vector<std::string>& out) { 1640 struct ClassMember {
1531 auto content = classLine->content.get(); 1641 std::string key;
1532 switch (content->getId()) { 1642 bool isBuiltin;
1533 case "class_member_list"_id: 1643 ast_node* node;
1534 transform_class_member_list(static_cast<class_member_list_t*>(content), out); 1644 };
1535 break; 1645
1536 case "Statement"_id: 1646 void transformClassDecl(ClassDecl_t* classDecl, std::vector<std::string>& out, ClassDeclUsage usage = ClassDeclUsage::Common, ExpList_t* expList = nullptr) {
1537 transformStatement(static_cast<Statement_t*>(content), out); 1647 std::vector<std::string> temp;
1648 auto body = classDecl->body.get();
1649 auto assignable = classDecl->name.get();
1650 auto extend = classDecl->extend.get();
1651 std::string className;
1652 if (assignable) {
1653 bool newDefined = false;
1654 std::tie(className, newDefined) = defineClassVariable(assignable);
1655 if (newDefined) {
1656 temp.push_back(indent() + s("local "sv) + className + nll(classDecl));
1657 }
1658 }
1659 temp.push_back(indent() + s("do"sv) + nll(classDecl));
1660 pushScope();
1661 auto classVar = getUnusedName("_class_"sv);
1662 addToScope(classVar);
1663 temp.push_back(indent() + s("local "sv) + classVar + nll(classDecl));
1664 if (body) {
1665 body->traverse([&](ast_node* node) {
1666 if (node->getId() == "Statement"_id) {
1667 if (auto assignment = static_cast<Assignment_t*>(node->getByPath({"Assignment"_id}))) {
1668 std::string preDefine = transformAssignDefs(assignment->assignable.get());
1669 if (!preDefine.empty()) temp.push_back(preDefine + nll(assignment));
1670 }
1671 return traversal::Return;
1672 }
1673 return traversal::Continue;
1674 });
1675 }
1676 std::string parent, parentVar;
1677 if (extend) {
1678 parentVar = getUnusedName("_parent_"sv);
1679 addToScope(parentVar);
1680 transformExp(extend, temp);
1681 parent = temp.back();
1682 temp.pop_back();
1683 temp.push_back(indent() + s("local "sv) + parentVar + s(" = "sv) + parent + nll(classDecl));
1684 }
1685 auto baseVar = getUnusedName("_base_"sv);
1686 auto selfVar = getUnusedName("_self_"sv);
1687 addToScope(baseVar);
1688 addToScope(selfVar);
1689 temp.push_back(indent() + s("local "sv) + baseVar + s(" = "sv));
1690 std::vector<std::string> builtins;
1691 std::vector<std::string> customs;
1692 std::vector<std::string> statements;
1693 if (body) {
1694 std::list<ClassMember> members;
1695 for (auto _classLine : classDecl->body->lines.objects()) {
1696 auto classLine = static_cast<ClassLine_t*>(_classLine);
1697 auto content = classLine->content.get();
1698 switch (content->getId()) {
1699 case "class_member_list"_id:
1700 pushScope();
1701 transform_class_member_list(static_cast<class_member_list_t*>(content), members, classVar);
1702 popScope();
1703 members.back().key = indent(1) + members.back().key;
1704 break;
1705 case "Statement"_id:
1706 transformStatement(static_cast<Statement_t*>(content), statements);
1707 break;
1708 default: break;
1709 }
1710 }
1711 for (auto& member : members) {
1712 if (member.isBuiltin) {
1713 builtins.push_back((builtins.empty() ? Empty : s(","sv) + nll(member.node)) + member.key);
1714 } else {
1715 customs.push_back((customs.empty() ? Empty : s(","sv) + nll(member.node)) + member.key);
1716 }
1717 }
1718 if (!customs.empty()) {
1719 temp.back() += s("{"sv) + nll(body);
1720 temp.push_back(join(customs) + nll(body));
1721 temp.push_back(indent() + s("}"sv) + nll(body));
1722 } else {
1723 temp.back() += s("{ }"sv) + nll(body);
1724 }
1725 temp.push_back(indent() + baseVar + s(".__index = "sv) + baseVar + nll(classDecl));
1726 } else {
1727 temp.back() += s("{ }"sv) + nll(classDecl);
1728 }
1729 if (extend) {
1730 _buf << indent() << "setmetatable("sv << baseVar << ", "sv << parentVar << ".__base)"sv << nll(classDecl);
1731 }
1732 _buf << indent() << classVar << " = setmetatable({" << nll(classDecl);
1733 if (!builtins.empty()) {
1734 _buf << join(builtins) << ","sv << nll(classDecl);
1735 } else {
1736 if (extend) {
1737 _buf << indent(1) << "__init = function(self, ...)"sv << nll(classDecl);
1738 _buf << indent(2) << "return _class_0.__parent.__init(self, ...)"sv << nll(classDecl);
1739 _buf << indent(1) << "end,"sv << nll(classDecl);
1740 } else {
1741 _buf << indent(1) << "__init = function() end,"sv << nll(classDecl);
1742 }
1743 }
1744 _buf << indent(1) << "__base = "sv << baseVar;
1745 if (!className.empty()) {
1746 _buf << ","sv << nll(classDecl) << indent(1) << "__name = \""sv << className << "\""sv << (extend ? s(","sv) : Empty) << nll(classDecl);
1747 } else {
1748 _buf << nll(classDecl);
1749 }
1750 if (extend) {
1751 _buf << indent(1) << "__parent = "sv << parentVar << nll(classDecl);
1752 }
1753 _buf << indent() << "}, {"sv << nll(classDecl);
1754 if (extend) {
1755 _buf << indent(1) << "__index = function(cls, name)"sv << nll(classDecl);
1756 _buf << indent(2) << "local val = rawget("sv << baseVar << ", name)"sv << nll(classDecl);
1757 _buf << indent(2) << "if val == nil then"sv << nll(classDecl);
1758 _buf << indent(3) << "local parent = rawget(cls, \"__parent\")"sv << nll(classDecl);
1759 _buf << indent(3) << "if parent then"sv << nll(classDecl);
1760 _buf << indent(4) << "return parent[name]"sv << nll(classDecl);
1761 _buf << indent(3) << "end"sv << nll(classDecl);
1762 _buf << indent(2) << "else"sv << nll(classDecl);
1763 _buf << indent(3) << "return val"sv << nll(classDecl);
1764 _buf << indent(2) << "end"sv << nll(classDecl);
1765 _buf << indent(1) << "end,"sv << nll(classDecl);
1766 } else {
1767 _buf << indent(1) << "__index = "sv << baseVar << ","sv << nll(classDecl);
1768 }
1769 _buf << indent(1) << "__call = function(cls, ...)"sv << nll(classDecl);
1770 _buf << indent(2) << "local " << selfVar << " = setmetatable({}, "sv << baseVar << ")"sv << nll(classDecl);
1771 _buf << indent(2) << "cls.__init("sv << selfVar << ", ...)"sv << nll(classDecl);
1772 _buf << indent(2) << "return "sv << selfVar << nll(classDecl);
1773 _buf << indent(1) << "end"sv << nll(classDecl);
1774 _buf << indent() << "})"sv << nll(classDecl);
1775 _buf << indent() << baseVar << ".__class = "sv << classVar << nll(classDecl);
1776 if (extend) {
1777 _buf << indent() << "if "sv << parentVar << ".__inherited then"sv << nll(classDecl);
1778 _buf << indent(1) << parentVar << ".__inherited("sv << parentVar << ", "sv << classVar << ")"sv << nll(classDecl);
1779 _buf << indent() << "end"sv << nll(classDecl);
1780 }
1781 if (!statements.empty()) _buf << indent() << "local self = "sv << classVar << nll(classDecl);
1782 _buf << join(statements);
1783 if (!className.empty()) _buf << indent() << className << " = "sv << classVar << nll(classDecl);
1784 switch (usage) {
1785 case ClassDeclUsage::Return: {
1786 _buf << indent() << "return "sv << classVar << nlr(classDecl);
1538 break; 1787 break;
1539 case "Exp"_id: 1788 }
1540 transformExp(static_cast<Exp_t*>(content), out); 1789 case ClassDeclUsage::Assignment: {
1790 std::vector<std::string> tmp;
1791 transformExpList(expList, tmp);
1792 _buf << indent() << tmp.back() << " = "sv << classVar << nlr(classDecl);
1541 break; 1793 break;
1794 }
1795 default: break;
1542 } 1796 }
1797 temp.push_back(clearBuf());
1798 popScope();
1799 temp.push_back(indent() + s("end"sv) + nlr(classDecl));
1800 out.push_back(join(temp));
1543 } 1801 }
1544 1802
1545 void transform_class_member_list(class_member_list_t* class_member_list, std::vector<std::string>& out) {noop(class_member_list, out);} 1803 void transform_class_member_list(class_member_list_t* class_member_list, std::list<ClassMember>& out, const std::string& classVar) {
1804 std::vector<std::string> temp;
1805 for (auto _keyValue : class_member_list->values.objects()) {
1806 auto keyValue = static_cast<KeyValue_t*>(_keyValue);
1807 bool isBuiltin = false;
1808 do {
1809 auto keyName = static_cast<KeyName_t*>(
1810 keyValue->getByPath({"normal_pair"_id, "KeyName"_id})
1811 );
1812 if (!keyName) break;
1813 auto normal_pair = static_cast<normal_pair_t*>(keyValue->getFirstChild());
1814 auto nameNode = keyName->getByPath({"Name"_id});
1815 if (!nameNode) break;
1816 auto name = toString(nameNode);
1817 input newSuperCall;
1818 isBuiltin = name == "new"sv;
1819 if (isBuiltin) {
1820 keyName->name.set(toAst<Name_t>("__init"sv, Name));
1821 newSuperCall = _converter.from_bytes(classVar) + L".__parent.__init";
1822 } else {
1823 newSuperCall = _converter.from_bytes(classVar) + L".__parent.__base." + _converter.from_bytes(name);
1824 }
1825 normal_pair->value->traverse([&](ast_node* node) {
1826 if (node->getId() == "ClassDecl"_id) return traversal::Return;
1827 if (auto chainValue = ast_cast<ChainValue_t>(node)) {
1828 if (auto var = chainValue->caller->getByPath({"Variable"_id})) {
1829 if (toString(var) == "super"sv) {
1830 if (chainValue->arguments && chainValue->arguments->argsList) {
1831 chainValue->arguments->argsList->exprs.push_front(toAst<Exp_t>("self"sv, Exp));
1832 _codeCache.push_back(newSuperCall);
1833 var->m_begin.m_it = _codeCache.back().begin();
1834 var->m_end.m_it = _codeCache.back().end();
1835 } else {
1836 _codeCache.push_back(_converter.from_bytes(classVar) + L".__parent");
1837 var->m_begin.m_it = _codeCache.back().begin();
1838 var->m_end.m_it = _codeCache.back().end();
1839 }
1840 }
1841 } else if (auto var = chainValue->caller->getByPath({"chain_call"_id, "Callable"_id, "Variable"_id})) {
1842 if (toString(var) == "super"sv) {
1843 auto chainList = getChainList(chainValue->caller);
1844 if (auto args = chainValue->getByPath({"InvokeArgs"_id, "ExpList"_id})) {
1845 chainList.push_back(args);
1846 }
1847 auto insertSelfToArguments = [&](ast_node* item) {
1848 switch (item->getId()) {
1849 case "ExpList"_id:
1850 static_cast<ExpList_t*>(item)->exprs.push_front(toAst<Exp_t>("self"sv, Exp));
1851 break;
1852 case "Invoke"_id: {
1853 auto invoke = static_cast<Invoke_t*>(item);
1854 if (auto fnArgs = invoke->argument.as<FnArgs_t>()) {
1855 fnArgs->args.push_front(toAst<Exp_t>("self"sv, Exp));
1856 } else {
1857 auto string = new_ptr<String_t>();
1858 string->str.set(invoke->argument);
1859 auto value = new_ptr<Value_t>();
1860 value->item.set(string);
1861 auto exp = new_ptr<Exp_t>();
1862 exp->value.set(value);
1863 auto newFnArgs = new_ptr<FnArgs_t>();
1864 fnArgs->args.push_back(toAst<Exp_t>("self"sv, Exp));
1865 newFnArgs->args.push_back(exp);
1866 invoke->argument.set(newFnArgs);
1867 }
1868 break;
1869 }
1870 default: break;
1871 }
1872 };
1873 if (chainList.size() == 2) {
1874 _codeCache.push_back(newSuperCall);
1875 var->m_begin.m_it = _codeCache.back().begin();
1876 var->m_end.m_it = _codeCache.back().end();
1877 auto item = chainList.back();
1878 insertSelfToArguments(item);
1879 } else if (chainList.size() > 2) {
1880 _codeCache.push_back(_converter.from_bytes(classVar) + L".__parent");
1881 var->m_begin.m_it = _codeCache.back().begin();
1882 var->m_end.m_it = _codeCache.back().end();
1883 if (auto colonChainItem = ast_cast<ColonChainItem_t>(chainList[1])) {
1884 colonChainItem->switchToDot = true;
1885 auto item = chainList[2];
1886 insertSelfToArguments(item);
1887 }
1888 } else {
1889 _codeCache.push_back(_converter.from_bytes(classVar) + L".__parent");
1890 var->m_begin.m_it = _codeCache.back().begin();
1891 var->m_end.m_it = _codeCache.back().end();
1892 }
1893 }
1894 }
1895 }
1896 return traversal::Continue;
1897 });
1898 } while (false);
1899 transformKeyValue(keyValue, temp);
1900 out.push_back({temp.back(), isBuiltin, keyValue});
1901 temp.clear();
1902 }
1903 }
1546 1904
1547 void transformAssignable(ast_node* node, std::vector<std::string>& out) {noop(node, out);} 1905 void transformAssignable(Assignable_t* assignable, std::vector<std::string>& out) {
1906 auto item = assignable->item.get();
1907 switch (item->getId()) {
1908 case "Chain"_id: transformChain(static_cast<Chain_t*>(item), out); break;
1909 case "Variable"_id: transformVariable(static_cast<Variable_t*>(item), out); break;
1910 case "SelfName"_id: transformSelfName(static_cast<SelfName_t*>(item), out, false); break;
1911 default: break;
1912 }
1913 }
1548 1914
1549 void transformUpdate(ast_node* node, std::vector<std::string>& out) {noop(node, out);} 1915 void transformUpdate(ast_node* node, std::vector<std::string>& out) {noop(node, out);}
1550 void transformImport(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} 1916 void transformImport(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);}
@@ -1565,40 +1931,69 @@ private:
1565 void transformCompFor(ast_node* node, std::vector<std::string>& out) {noop(node, out);} 1931 void transformCompFor(ast_node* node, std::vector<std::string>& out) {noop(node, out);}
1566 void transformCompClause(ast_node* node, std::vector<std::string>& out) {noop(node, out);} 1932 void transformCompClause(ast_node* node, std::vector<std::string>& out) {noop(node, out);}
1567 void transform_invoke_args_with_table(ast_node* node, std::vector<std::string>& out) {noop(node, out);} 1933 void transform_invoke_args_with_table(ast_node* node, std::vector<std::string>& out) {noop(node, out);}
1934 void transformUnless(Unless_t* node, std::vector<std::string>& out) {noop(node, out);}
1568}; 1935};
1569 1936
1570const std::string MoonCompliler::Empty; 1937const std::string MoonCompliler::Empty;
1571 1938
1572int main() 1939int main()
1573{ 1940{
1574 std::string s = R"TestCodesHere( 1941 std::string s = R"TestCodesHere(class Hello
1575-- vararg bubbling 1942 new: (@test, @world) =>
1576f = (...) -> #{...} 1943 print "creating object.."
1944 hello: =>
1945 print @test, @world
1946 __tostring: => "hello world"
1947
1948x = Hello 1,2
1949x\hello()
1950
1951print x
1952
1953class Simple
1954 cool: => print "cool"
1955
1956class Yikes extends Simple
1957 new: => print "created hello"
1958
1959x = Yikes()
1960x\cool()
1961
1962
1963class Hi
1964 new: (arg) =>
1965 print "init arg", arg
1577 1966
1578dont_bubble = -> 1967 cool: (num) =>
1579 [x for x in ((...)-> print ...)("hello")] 1968 print "num", num
1580 1969
1581k = [x for x in ((...)-> print ...)("hello")]
1582 1970
1583j = for i=1,10 1971class Simple extends Hi
1584 (...) -> print ... 1972 new: => super "man"
1973 cool: => super 120302
1585 1974
1586-- bubble me 1975x = Simple()
1976x\cool()
1587 1977
1588m = (...) -> 1978print x.__class == Simple
1589 [x for x in *{...} when f(...) > 4]
1590 1979
1591x = for i in *{...} do i
1592y = [x for x in *{...}]
1593z = [x for x in hallo when f(...) > 4]
1594 1980
1981class Okay
1982 -- what is going on
1983 something: 20323
1984 -- yeaha
1595 1985
1596a = for i=1,10 do ...
1597 1986
1598b = for i=1,10 1987class Biggie extends Okay
1599 -> print ... 1988 something: =>
1989 super 1,2,3,4
1990 super.something another_self, 1,2,3,4
1991 assert super == Okay
1600 1992
1601 1993
1994class Yeah
1995 okay: =>
1996 super\something 1,2,3,4
1602)TestCodesHere"; 1997)TestCodesHere";
1603 1998
1604 MoonCompliler{}.complile(s); 1999 MoonCompliler{}.complile(s);
diff --git a/MoonParser/moon_ast.h b/MoonParser/moon_ast.h
index 9eab2f9..9733bf9 100644
--- a/MoonParser/moon_ast.h
+++ b/MoonParser/moon_ast.h
@@ -48,25 +48,25 @@ public: \
48AST_LEAF(Num, "Num"_id) 48AST_LEAF(Num, "Num"_id)
49AST_END(Num) 49AST_END(Num)
50 50
51AST_LEAF(_Name, "_Name"_id) 51AST_LEAF(Name, "Name"_id)
52AST_END(_Name)
53
54AST_NODE(Name, "Name"_id)
55 ast_ptr<_Name_t> name;
56AST_END(Name) 52AST_END(Name)
57 53
54AST_NODE(Variable, "Variable"_id)
55 ast_ptr<Name_t> name;
56AST_END(Variable)
57
58AST_LEAF(self, "self"_id) 58AST_LEAF(self, "self"_id)
59AST_END(self) 59AST_END(self)
60 60
61AST_NODE(self_name, "self_name"_id) 61AST_NODE(self_name, "self_name"_id)
62 ast_ptr<_Name_t> name; 62 ast_ptr<Name_t> name;
63AST_END(self_name) 63AST_END(self_name)
64 64
65AST_LEAF(self_class, "self_class"_id) 65AST_LEAF(self_class, "self_class"_id)
66AST_END(self_class) 66AST_END(self_class)
67 67
68AST_NODE(self_class_name, "self_class_name"_id) 68AST_NODE(self_class_name, "self_class_name"_id)
69 ast_ptr<_Name_t> name; 69 ast_ptr<Name_t> name;
70AST_END(self_class_name) 70AST_END(self_class_name)
71 71
72AST_NODE(SelfName, "SelfName"_id) 72AST_NODE(SelfName, "SelfName"_id)
@@ -74,7 +74,7 @@ AST_NODE(SelfName, "SelfName"_id)
74AST_END(SelfName) 74AST_END(SelfName)
75 75
76AST_NODE(KeyName, "KeyName"_id) 76AST_NODE(KeyName, "KeyName"_id)
77 ast_ptr<ast_node> name; // SelfName_t | _Name_t 77 ast_ptr<ast_node> name; // SelfName_t | Name_t
78AST_END(KeyName) 78AST_END(KeyName)
79 79
80AST_LEAF(VarArg, "VarArg"_id) 80AST_LEAF(VarArg, "VarArg"_id)
@@ -88,7 +88,7 @@ AST_END(Seperator)
88 88
89AST_NODE(NameList, "NameList"_id) 89AST_NODE(NameList, "NameList"_id)
90 ast_ptr<Seperator_t> sep; 90 ast_ptr<Seperator_t> sep;
91 ast_list<Name_t> names; 91 ast_list<Variable_t> names;
92AST_END(NameList) 92AST_END(NameList)
93 93
94AST_NODE(Local, "Local"_id) 94AST_NODE(Local, "Local"_id)
@@ -96,13 +96,13 @@ AST_NODE(Local, "Local"_id)
96AST_END(Local) 96AST_END(Local)
97 97
98AST_NODE(colon_import_name, "colon_import_name"_id) 98AST_NODE(colon_import_name, "colon_import_name"_id)
99 ast_ptr<Name_t> name; 99 ast_ptr<Variable_t> name;
100AST_END(colon_import_name) 100AST_END(colon_import_name)
101 101
102class Exp_t; 102class Exp_t;
103 103
104AST_NODE(ImportName, "ImportName"_id) 104AST_NODE(ImportName, "ImportName"_id)
105 ast_ptr<ast_node> name; // colon_import_name_t | Name_t 105 ast_ptr<ast_node> name; // colon_import_name_t | Variable_t
106AST_END(ImportName) 106AST_END(ImportName)
107 107
108AST_NODE(Import, "Import"_id) 108AST_NODE(Import, "Import"_id)
@@ -182,7 +182,7 @@ AST_NODE(for_step_value, "for_step_value"_id)
182AST_END(for_step_value) 182AST_END(for_step_value)
183 183
184AST_NODE(For, "For"_id) 184AST_NODE(For, "For"_id)
185 ast_ptr<Name_t> varName; 185 ast_ptr<Variable_t> varName;
186 ast_ptr<Exp_t> startValue; 186 ast_ptr<Exp_t> startValue;
187 ast_ptr<Exp_t> stopValue; 187 ast_ptr<Exp_t> stopValue;
188 ast_ptr<for_step_value_t, true> stepValue; 188 ast_ptr<for_step_value_t, true> stepValue;
@@ -228,7 +228,7 @@ AST_NODE(CompForEach, "CompForEach"_id)
228AST_END(CompForEach) 228AST_END(CompForEach)
229 229
230AST_NODE(CompFor, "CompFor"_id) 230AST_NODE(CompFor, "CompFor"_id)
231 ast_ptr<Name_t> varName; 231 ast_ptr<Variable_t> varName;
232 ast_ptr<Exp_t> startValue; 232 ast_ptr<Exp_t> startValue;
233 ast_ptr<Exp_t> stopValue; 233 ast_ptr<Exp_t> stopValue;
234 ast_ptr<for_step_value_t, true> stepValue; 234 ast_ptr<for_step_value_t, true> stepValue;
@@ -264,7 +264,7 @@ AST_END(BinaryOperator)
264class Chain_t; 264class Chain_t;
265 265
266AST_NODE(Assignable, "Assignable"_id) 266AST_NODE(Assignable, "Assignable"_id)
267 ast_ptr<ast_node> item; // Chain_t | Name_t | SelfName_t 267 ast_ptr<ast_node> item; // Chain_t | Variable_t | SelfName_t
268AST_END(Assignable) 268AST_END(Assignable)
269 269
270class Value_t; 270class Value_t;
@@ -280,7 +280,7 @@ AST_NODE(Exp, "Exp"_id)
280AST_END(Exp) 280AST_END(Exp)
281 281
282AST_NODE(Callable, "Callable"_id) 282AST_NODE(Callable, "Callable"_id)
283 ast_ptr<ast_node> item; // Name_t | SelfName_t | VarArg_t | Parens_t 283 ast_ptr<ast_node> item; // Variable_t | SelfName_t | VarArg_t | Parens_t
284AST_END(Callable) 284AST_END(Callable)
285 285
286class InvokeArgs_t; 286class InvokeArgs_t;
@@ -359,11 +359,12 @@ AST_NODE(chain_item, "chain_item"_id)
359AST_END(chain_item) 359AST_END(chain_item)
360 360
361AST_NODE(DotChainItem, "DotChainItem"_id) 361AST_NODE(DotChainItem, "DotChainItem"_id)
362 ast_ptr<_Name_t> name; 362 ast_ptr<Name_t> name;
363AST_END(DotChainItem) 363AST_END(DotChainItem)
364 364
365AST_NODE(ColonChainItem, "ColonChainItem"_id) 365AST_NODE(ColonChainItem, "ColonChainItem"_id)
366 ast_ptr<_Name_t> name; 366 ast_ptr<Name_t> name;
367 bool switchToDot = false;
367AST_END(ColonChainItem) 368AST_END(ColonChainItem)
368 369
369AST_NODE(chain_dot_chain, "chain_dot_chain"_id) 370AST_NODE(chain_dot_chain, "chain_dot_chain"_id)
@@ -430,7 +431,7 @@ AST_NODE(class_member_list, "class_member_list"_id)
430AST_END(class_member_list) 431AST_END(class_member_list)
431 432
432AST_NODE(ClassLine, "ClassLine"_id) 433AST_NODE(ClassLine, "ClassLine"_id)
433 ast_ptr<ast_node> content; // class_member_list_t | Statement_t | Exp_t 434 ast_ptr<ast_node> content; // class_member_list_t | Statement_t
434AST_END(ClassLine) 435AST_END(ClassLine)
435 436
436AST_NODE(ClassBlock, "ClassBlock"_id) 437AST_NODE(ClassBlock, "ClassBlock"_id)
@@ -457,7 +458,7 @@ AST_NODE(Export, "Export"_id)
457AST_END(Export) 458AST_END(Export)
458 459
459AST_NODE(variable_pair, "variable_pair"_id) 460AST_NODE(variable_pair, "variable_pair"_id)
460 ast_ptr<Name_t> name; 461 ast_ptr<Variable_t> name;
461AST_END(variable_pair) 462AST_END(variable_pair)
462 463
463AST_NODE(normal_pair, "normal_pair"_id) 464AST_NODE(normal_pair, "normal_pair"_id)
@@ -470,7 +471,7 @@ AST_NODE(KeyValue, "KeyValue"_id)
470AST_END(KeyValue) 471AST_END(KeyValue)
471 472
472AST_NODE(FnArgDef, "FnArgDef"_id) 473AST_NODE(FnArgDef, "FnArgDef"_id)
473 ast_ptr<ast_node> name; // Name_t | SelfName_t 474 ast_ptr<ast_node> name; // Variable_t | SelfName_t
474 ast_ptr<Exp_t, true> defaultValue; 475 ast_ptr<Exp_t, true> defaultValue;
475AST_END(FnArgDef) 476AST_END(FnArgDef)
476 477
@@ -499,7 +500,7 @@ AST_NODE(FunLit, "FunLit"_id)
499AST_END(FunLit) 500AST_END(FunLit)
500 501
501AST_NODE(NameOrDestructure, "NameOrDestructure"_id) 502AST_NODE(NameOrDestructure, "NameOrDestructure"_id)
502 ast_ptr<ast_node> item; // Name_t | TableLit_t 503 ast_ptr<ast_node> item; // Variable_t | TableLit_t
503AST_END(NameOrDestructure) 504AST_END(NameOrDestructure)
504 505
505AST_NODE(AssignableNameList, "AssignableNameList"_id) 506AST_NODE(AssignableNameList, "AssignableNameList"_id)
diff --git a/MoonParser/moon_parser.cpp b/MoonParser/moon_parser.cpp
index 3009ee5..ec8df38 100644
--- a/MoonParser/moon_parser.cpp
+++ b/MoonParser/moon_parser.cpp
@@ -20,9 +20,8 @@ rule SomeSpace = +set(" \t") >> -Comment;
20rule SpaceBreak = Space >> Break; 20rule SpaceBreak = Space >> Break;
21rule EmptyLine = SpaceBreak; 21rule EmptyLine = SpaceBreak;
22rule AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; 22rule AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_';
23rule _Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; 23rule Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum;
24rule SpaceName = Space >> _Name; 24rule Num =
25rule _Num =
26 ( 25 (
27 "0x" >> 26 "0x" >>
28 +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> 27 +(range('0', '9') | range('a', 'f') | range('A', 'F')) >>
@@ -35,7 +34,6 @@ rule _Num =
35 ('.' >> +range('0', '9')) 34 ('.' >> +range('0', '9'))
36 ) >> -(set("eE") >> -expr('-') >> +range('0', '9')) 35 ) >> -(set("eE") >> -expr('-') >> +range('0', '9'))
37 ); 36 );
38rule Num = Space >> _Num;
39rule Cut = false_(); 37rule Cut = false_();
40rule Seperator = true_(); 38rule Seperator = true_();
41 39
@@ -44,8 +42,7 @@ rule Seperator = true_();
44#define ensure(patt, finally) (((patt) >> (finally)) | ((finally) >> (Cut))) 42#define ensure(patt, finally) (((patt) >> (finally)) | ((finally) >> (Cut)))
45#define key(str) (Space >> str >> not_(AlphaNum)) 43#define key(str) (Space >> str >> not_(AlphaNum))
46 44
47rule Name = user(_Name, [](const item_t& item) 45rule Variable = user(Name, [](const item_t& item) {
48{
49 State* st = reinterpret_cast<State*>(item.user_data); 46 State* st = reinterpret_cast<State*>(item.user_data);
50 for (auto it = item.begin; it != item.end; ++it) st->buffer << static_cast<char>(*it); 47 for (auto it = item.begin; it != item.end; ++it) st->buffer << static_cast<char>(*it);
51 std::string name; 48 std::string name;
@@ -57,21 +54,18 @@ rule Name = user(_Name, [](const item_t& item)
57}); 54});
58 55
59rule self = expr('@'); 56rule self = expr('@');
60rule self_name = '@' >> _Name; 57rule self_name = '@' >> Name;
61rule self_class = expr("@@"); 58rule self_class = expr("@@");
62rule self_class_name = "@@" >> _Name; 59rule self_class_name = "@@" >> Name;
63 60
64rule SelfName = Space >> (self_class_name | self_class | self_name | self); 61rule SelfName = Space >> (self_class_name | self_class | self_name | self);
65rule KeyName = SelfName | Space >> _Name; 62rule KeyName = SelfName | Space >> Name;
66rule VarArg = Space >> "..."; 63rule VarArg = Space >> "...";
67 64
68rule check_indent = user(Indent, [](const item_t& item) 65rule check_indent = user(Indent, [](const item_t& item) {
69{
70 int indent = 0; 66 int indent = 0;
71 for (input_it i = item.begin; i != item.end; ++i) 67 for (input_it i = item.begin; i != item.end; ++i) {
72 { 68 switch (*i) {
73 switch (*i)
74 {
75 case ' ': indent++; break; 69 case ' ': indent++; break;
76 case '\t': indent += 4; break; 70 case '\t': indent += 4; break;
77 } 71 }
@@ -81,21 +75,17 @@ rule check_indent = user(Indent, [](const item_t& item)
81}); 75});
82rule CheckIndent = and_(check_indent); 76rule CheckIndent = and_(check_indent);
83 77
84rule advance = user(Indent, [](const item_t& item) 78rule advance = user(Indent, [](const item_t& item) {
85{
86 int indent = 0; 79 int indent = 0;
87 for (input_it i = item.begin; i != item.end; ++i) 80 for (input_it i = item.begin; i != item.end; ++i) {
88 { 81 switch (*i) {
89 switch (*i)
90 {
91 case ' ': indent++; break; 82 case ' ': indent++; break;
92 case '\t': indent += 4; break; 83 case '\t': indent += 4; break;
93 } 84 }
94 } 85 }
95 State* st = reinterpret_cast<State*>(item.user_data); 86 State* st = reinterpret_cast<State*>(item.user_data);
96 int top = st->indents.top(); 87 int top = st->indents.top();
97 if (top != -1 && indent > top) 88 if (top != -1 && indent > top) {
98 {
99 st->indents.push(indent); 89 st->indents.push(indent);
100 return true; 90 return true;
101 } 91 }
@@ -103,13 +93,10 @@ rule advance = user(Indent, [](const item_t& item)
103}); 93});
104rule Advance = and_(advance); 94rule Advance = and_(advance);
105 95
106rule push_indent = user(Indent, [](const item_t& item) 96rule push_indent = user(Indent, [](const item_t& item) {
107{
108 int indent = 0; 97 int indent = 0;
109 for (input_it i = item.begin; i != item.end; ++i) 98 for (input_it i = item.begin; i != item.end; ++i) {
110 { 99 switch (*i) {
111 switch (*i)
112 {
113 case ' ': indent++; break; 100 case ' ': indent++; break;
114 case '\t': indent += 4; break; 101 case '\t': indent += 4; break;
115 } 102 }
@@ -120,15 +107,13 @@ rule push_indent = user(Indent, [](const item_t& item)
120}); 107});
121rule PushIndent = and_(push_indent); 108rule PushIndent = and_(push_indent);
122 109
123rule PreventIndent = user(true_(), [](const item_t& item) 110rule PreventIndent = user(true_(), [](const item_t& item) {
124{
125 State* st = reinterpret_cast<State*>(item.user_data); 111 State* st = reinterpret_cast<State*>(item.user_data);
126 st->indents.push(-1); 112 st->indents.push(-1);
127 return true; 113 return true;
128}); 114});
129 115
130rule PopIndent = user(true_(), [](const item_t& item) 116rule PopIndent = user(true_(), [](const item_t& item) {
131{
132 State* st = reinterpret_cast<State*>(item.user_data); 117 State* st = reinterpret_cast<State*>(item.user_data);
133 st->indents.pop(); 118 st->indents.pop();
134 return true; 119 return true;
@@ -143,8 +128,8 @@ extern rule NameList;
143rule local_flag = expr('*') | expr('^'); 128rule local_flag = expr('*') | expr('^');
144rule Local = key("local") >> ((Space >> local_flag) | NameList); 129rule Local = key("local") >> ((Space >> local_flag) | NameList);
145 130
146rule colon_import_name = sym('\\') >> Space >> Name; 131rule colon_import_name = sym('\\') >> Space >> Variable;
147rule ImportName = colon_import_name | Space >> Name; 132rule ImportName = colon_import_name | Space >> Variable;
148rule ImportNameList = Seperator >> *SpaceBreak >> ImportName >> *((+SpaceBreak | sym(',') >> *SpaceBreak) >> ImportName); 133rule ImportNameList = Seperator >> *SpaceBreak >> ImportName >> *((+SpaceBreak | sym(',') >> *SpaceBreak) >> ImportName);
149 134
150extern rule Exp; 135extern rule Exp;
@@ -183,7 +168,7 @@ rule Unless = key("unless") >> IfCond >> -key("then") >> Body >> Seperator >> *I
183rule While = key("while") >> DisableDo >> ensure(Exp, PopDo) >> -key("do") >> Body; 168rule While = key("while") >> DisableDo >> ensure(Exp, PopDo) >> -key("do") >> Body;
184 169
185rule for_step_value = sym(',') >> Exp; 170rule for_step_value = sym(',') >> Exp;
186rule for_args = Space >> Name >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value; 171rule for_args = Space >> Variable >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value;
187 172
188rule For = key("for") >> DisableDo >> 173rule For = key("for") >> DisableDo >>
189 ensure(for_args, PopDo) >> 174 ensure(for_args, PopDo) >>
@@ -230,7 +215,7 @@ extern rule CompForEach, CompFor, CompClause;
230rule CompInner = (CompForEach | CompFor) >> Seperator >> *CompClause; 215rule CompInner = (CompForEach | CompFor) >> Seperator >> *CompClause;
231rule star_exp = sym('*') >> Exp; 216rule star_exp = sym('*') >> Exp;
232rule CompForEach = key("for") >> AssignableNameList >> key("in") >> (star_exp | Exp); 217rule CompForEach = key("for") >> AssignableNameList >> key("in") >> (star_exp | Exp);
233rule CompFor = key("for") >> Space >> Name >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value; 218rule CompFor = key("for") >> Space >> Variable >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value;
234rule CompClause = CompFor | CompForEach | key("when") >> Exp; 219rule CompClause = CompFor | CompForEach | key("when") >> Exp;
235 220
236extern rule TableBlock; 221extern rule TableBlock;
@@ -269,7 +254,7 @@ rule BinaryOperator =
269 254
270extern rule Chain; 255extern rule Chain;
271 256
272rule Assignable = Chain | Space >> Name | SelfName; 257rule Assignable = Chain | Space >> Variable | SelfName;
273 258
274extern rule Value; 259extern rule Value;
275 260
@@ -324,7 +309,7 @@ rule LuaString = user(LuaStringOpen >> -Break >> LuaStringContent >> LuaStringCl
324}); 309});
325 310
326rule Parens = sym('(') >> *SpaceBreak >> Exp >> *SpaceBreak >> sym(')'); 311rule Parens = sym('(') >> *SpaceBreak >> Exp >> *SpaceBreak >> sym(')');
327rule Callable = Space >> Name | SelfName | VarArg | Parens; 312rule Callable = Space >> Variable | SelfName | VarArg | Parens;
328rule FnArgsExpList = Exp >> *((Break | sym(',')) >> White >> Exp); 313rule FnArgsExpList = Exp >> *((Break | sym(',')) >> White >> Exp);
329 314
330rule FnArgs = Seperator >> 315rule FnArgs = Seperator >>
@@ -356,8 +341,8 @@ extern rule Invoke, Slice;
356 341
357rule Index = symx('[') >> Exp >> sym(']'); 342rule Index = symx('[') >> Exp >> sym(']');
358rule ChainItem = Invoke | DotChainItem | Slice | Index; 343rule ChainItem = Invoke | DotChainItem | Slice | Index;
359rule DotChainItem = symx('.') >> _Name; 344rule DotChainItem = symx('.') >> Name;
360rule ColonChainItem = symx('\\') >> _Name; 345rule ColonChainItem = symx('\\') >> Name;
361rule invoke_chain = Invoke >> -ChainItems; 346rule invoke_chain = Invoke >> -ChainItems;
362rule ColonChain = ColonChainItem >> -invoke_chain; 347rule ColonChain = ColonChainItem >> -invoke_chain;
363 348
@@ -406,7 +391,7 @@ rule TableBlock = +(SpaceBreak) >> Advance >> ensure(TableBlockInner, PopIndent)
406extern rule Statement; 391extern rule Statement;
407 392
408rule class_member_list = Seperator >> KeyValue >> *(sym(',') >> KeyValue); 393rule class_member_list = Seperator >> KeyValue >> *(sym(',') >> KeyValue);
409rule ClassLine = CheckIndent >> (class_member_list | Statement | Exp) >> -sym(','); 394rule ClassLine = CheckIndent >> (class_member_list | Statement) >> -sym(',');
410rule ClassBlock = +(SpaceBreak) >> Advance >>Seperator >> ClassLine >> *(+(SpaceBreak) >> ClassLine) >> PopIndent; 395rule ClassBlock = +(SpaceBreak) >> Advance >>Seperator >> ClassLine >> *(+(SpaceBreak) >> ClassLine) >> PopIndent;
411 396
412rule ClassDecl = 397rule ClassDecl =
@@ -419,7 +404,7 @@ rule export_values = NameList >> -(sym('=') >> ExpListLow);
419rule export_op = expr('*') | expr('^'); 404rule export_op = expr('*') | expr('^');
420rule Export = key("export") >> (ClassDecl | (Space >> export_op) | export_values); 405rule Export = key("export") >> (ClassDecl | (Space >> export_op) | export_values);
421 406
422rule variable_pair = sym(':') >> not_(SomeSpace) >> Space >> Name; 407rule variable_pair = sym(':') >> not_(SomeSpace) >> Space >> Variable;
423 408
424rule normal_pair = 409rule normal_pair =
425( 410(
@@ -436,7 +421,7 @@ rule KeyValue = variable_pair | normal_pair;
436rule KeyValueList = KeyValue >> *(sym(',') >> KeyValue); 421rule KeyValueList = KeyValue >> *(sym(',') >> KeyValue);
437rule KeyValueLine = CheckIndent >> KeyValueList >> -sym(','); 422rule KeyValueLine = CheckIndent >> KeyValueList >> -sym(',');
438 423
439rule FnArgDef = (Space >> Name | SelfName) >> -(sym('=') >> Exp); 424rule FnArgDef = (Space >> Variable | SelfName) >> -(sym('=') >> Exp);
440 425
441rule FnArgDefList = Seperator >> 426rule FnArgDefList = Seperator >>
442( 427(
@@ -455,8 +440,8 @@ rule FnArgsDef = sym('(') >> White >> -FnArgDefList >> -outer_var_shadow >> Whit
455rule fn_arrow = expr("->") | expr("=>"); 440rule fn_arrow = expr("->") | expr("=>");
456rule FunLit = -FnArgsDef >> Space >> fn_arrow >> -Body; 441rule FunLit = -FnArgsDef >> Space >> fn_arrow >> -Body;
457 442
458rule NameList = Seperator >> Space >> Name >> *(sym(',') >> Space >> Name); 443rule NameList = Seperator >> Space >> Variable >> *(sym(',') >> Space >> Variable);
459rule NameOrDestructure = Space >> Name | TableLit; 444rule NameOrDestructure = Space >> Variable | TableLit;
460rule AssignableNameList = Seperator >> NameOrDestructure >> *(sym(',') >> NameOrDestructure); 445rule AssignableNameList = Seperator >> NameOrDestructure >> *(sym(',') >> NameOrDestructure);
461 446
462rule ExpList = Seperator >> Exp >> *(sym(',') >> Exp); 447rule ExpList = Seperator >> Exp >> *(sym(',') >> Exp);
@@ -479,18 +464,19 @@ rule InvokeArgs =
479 TableBlock 464 TableBlock
480 ); 465 );
481 466
482rule const_value = key("nil") | key("true") | key("false"); 467rule const_value = (expr("nil") | expr("true") | expr("false")) >> not_(AlphaNum);
483rule minus_exp = sym('-') >> not_(SomeSpace) >> Exp; 468rule minus_exp = expr('-') >> not_(SomeSpace) >> Exp;
484rule sharp_exp = sym('#') >> Exp; 469rule sharp_exp = expr('#') >> Exp;
485rule tilde_exp = sym('~') >> Exp; 470rule tilde_exp = expr('~') >> Exp;
486rule not_exp = key("not") >> Exp; 471rule not_exp = expr("not") >> not_(AlphaNum) >> Exp;
487rule unary_exp = minus_exp | sharp_exp | tilde_exp | not_exp; 472rule unary_exp = minus_exp | sharp_exp | tilde_exp | not_exp;
488 473
489rule SimpleValue = 474rule SimpleValue =
490 const_value | 475 (Space >> const_value) |
491 If | Unless | Switch | With | ClassDecl | ForEach | For | While | Do | 476 If | Unless | Switch | With | ClassDecl | ForEach | For | While | Do |
492 unary_exp | 477 (Space >> unary_exp) |
493 TblComprehension | TableLit | Comprehension | FunLit | Num; 478 TblComprehension | TableLit | Comprehension | FunLit |
479 (Space >> Num);
494 480
495rule Assignment = ExpList >> (Update | Assign); 481rule Assignment = ExpList >> (Update | Assign);
496 482
diff --git a/MoonParser/moon_parser.h b/MoonParser/moon_parser.h
index 6f9ef8f..5327b05 100644
--- a/MoonParser/moon_parser.h
+++ b/MoonParser/moon_parser.h
@@ -10,10 +10,8 @@
10#include "parserlib.hpp" 10#include "parserlib.hpp"
11using namespace parserlib; 11using namespace parserlib;
12 12
13struct State 13struct State {
14{ 14 State() {
15 State()
16 {
17 indents.push(0); 15 indents.push(0);
18 stringOpen = -1; 16 stringOpen = -1;
19 } 17 }