aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--MoonParser/ast.hpp60
-rw-r--r--MoonParser/moon_ast.cpp397
-rw-r--r--MoonParser/moon_ast.h126
-rw-r--r--MoonParser/moon_parser.cpp28
4 files changed, 237 insertions, 374 deletions
diff --git a/MoonParser/ast.hpp b/MoonParser/ast.hpp
index e7ae74f..1077f5d 100644
--- a/MoonParser/ast.hpp
+++ b/MoonParser/ast.hpp
@@ -25,8 +25,7 @@ typedef std::vector<ast_node*> ast_stack;
25extern int ast_type_id; 25extern int ast_type_id;
26 26
27template<class T> 27template<class T>
28int ast_type() 28int ast_type() {
29{
30 static int type = ast_type_id++; 29 static int type = ast_type_id++;
31 return type; 30 return type;
32} 31}
@@ -211,7 +210,7 @@ public:
211 210
212 template <class T> 211 template <class T>
213 T* to() const { 212 T* to() const {
214 assert(m_ptr->getId() == ast_type<T>()); 213 assert(m_ptr->getId() != ast_type<T>());
215 return static_cast<T*>(m_ptr); 214 return static_cast<T*>(m_ptr);
216 } 215 }
217 216
@@ -313,13 +312,13 @@ inline ast_ptr<T, false, false> new_ptr() {
313 return ast_ptr<T, false, false>(new T); 312 return ast_ptr<T, false, false>(new T);
314} 313}
315 314
316template <class ...Args> class ast_choice : public _ast_ptr { 315template <class ...Args> class ast_sel : public _ast_ptr {
317public: 316public:
318 ast_choice() : _ast_ptr(nullptr, true) {} 317 ast_sel() : _ast_ptr(nullptr, true) {}
319 318
320 ast_choice(const ast_choice<Args...>& other) : _ast_ptr(other.get(), true) {} 319 ast_sel(const ast_sel<Args...>& other) : _ast_ptr(other.get(), true) {}
321 320
322 ast_choice<Args...>& operator=(const ast_choice<Args...>& other) { 321 ast_sel<Args...>& operator=(const ast_sel<Args...>& other) {
323 set(other.get()); 322 set(other.get());
324 return *this; 323 return *this;
325 } 324 }
@@ -340,7 +339,7 @@ public:
340 339
341 ast_node* node = st.back(); 340 ast_node* node = st.back();
342 341
343 if (!ast_choice::accept(node)) throw std::logic_error("invalid AST node"); 342 if (!ast_sel::accept(node)) throw std::logic_error("invalid AST node");
344 343
345 st.pop_back(); 344 st.pop_back();
346 345
@@ -351,7 +350,7 @@ private:
351 virtual bool accept(ast_node* node) override { 350 virtual bool accept(ast_node* node) override {
352 if (!node) return false; 351 if (!node) return false;
353 using swallow = bool[]; 352 using swallow = bool[];
354 bool* result = nullptr; 353 bool result = false;
355 (void)swallow{result || (result = ast_type<Args>() == node->get_type())...}; 354 (void)swallow{result || (result = ast_type<Args>() == node->get_type())...};
356 return result; 355 return result;
357 } 356 }
@@ -449,6 +448,49 @@ private:
449 } 448 }
450}; 449};
451 450
451template <class ...Args> class ast_sel_list : public _ast_list {
452public:
453 ///the default constructor.
454 ast_sel_list() {}
455
456 ast_sel_list(const ast_sel_list<Args...>& other) {
457 clear();
458 dup(other);
459 }
460
461 ast_sel_list<Args...>& operator=(const ast_sel_list<Args...>& other) {
462 clear();
463 dup(other);
464 return *this;
465 }
466
467 /** Pops objects of type T from the stack until no more objects can be popped.
468 @param st stack.
469 */
470 virtual void construct(ast_stack &st) override {
471 while (!st.empty()) {
472 ast_node* node = st.back();
473
474 //if the object was not not of the appropriate type,
475 //end the list parsing
476 if (!ast_sel_list<Args...>::accept(node)) return;
477
478 st.pop_back();
479
480 //insert the object in the list, in reverse order
481 m_objects.push_front(node);
482 node->retain();
483 }
484 }
485private:
486 virtual bool accept(ast_node* node) override {
487 if (!node) return false;
488 using swallow = bool[];
489 bool result = false;
490 (void)swallow{result || (result = ast_type<Args>() == node->get_type())...};
491 return result;
492 }
493};
452 494
453/** AST function which creates an object of type T 495/** AST function which creates an object of type T
454 and pushes it to the node stack. 496 and pushes it to the node stack.
diff --git a/MoonParser/moon_ast.cpp b/MoonParser/moon_ast.cpp
index af34395..f7dcae6 100644
--- a/MoonParser/moon_ast.cpp
+++ b/MoonParser/moon_ast.cpp
@@ -82,20 +82,11 @@ AST_IMPL(double_string_content)
82AST_IMPL(DoubleString) 82AST_IMPL(DoubleString)
83AST_IMPL(String) 83AST_IMPL(String)
84AST_IMPL(Parens) 84AST_IMPL(Parens)
85AST_IMPL(FnArgs)
86AST_IMPL(chain_call)
87AST_IMPL(chain_item)
88AST_IMPL(DotChainItem) 85AST_IMPL(DotChainItem)
89AST_IMPL(ColonChainItem) 86AST_IMPL(ColonChainItem)
90AST_IMPL(chain_dot_chain)
91AST_IMPL(ChainItem)
92AST_IMPL(ChainItems)
93AST_IMPL(invoke_chain)
94AST_IMPL(ColonChain)
95AST_IMPL(default_value) 87AST_IMPL(default_value)
96AST_IMPL(Slice) 88AST_IMPL(Slice)
97AST_IMPL(Invoke) 89AST_IMPL(Invoke)
98AST_IMPL(TableValue)
99AST_IMPL(TableLit) 90AST_IMPL(TableLit)
100AST_IMPL(TableBlock) 91AST_IMPL(TableBlock)
101AST_IMPL(class_member_list) 92AST_IMPL(class_member_list)
@@ -107,7 +98,6 @@ AST_IMPL(export_op)
107AST_IMPL(Export) 98AST_IMPL(Export)
108AST_IMPL(variable_pair) 99AST_IMPL(variable_pair)
109AST_IMPL(normal_pair) 100AST_IMPL(normal_pair)
110AST_IMPL(KeyValue)
111AST_IMPL(FnArgDef) 101AST_IMPL(FnArgDef)
112AST_IMPL(FnArgDefList) 102AST_IMPL(FnArgDefList)
113AST_IMPL(outer_var_shadow) 103AST_IMPL(outer_var_shadow)
@@ -116,8 +106,6 @@ AST_IMPL(fn_arrow)
116AST_IMPL(FunLit) 106AST_IMPL(FunLit)
117AST_IMPL(NameOrDestructure) 107AST_IMPL(NameOrDestructure)
118AST_IMPL(AssignableNameList) 108AST_IMPL(AssignableNameList)
119AST_IMPL(ArgBlock)
120AST_IMPL(invoke_args_with_table)
121AST_IMPL(InvokeArgs) 109AST_IMPL(InvokeArgs)
122AST_IMPL(const_value) 110AST_IMPL(const_value)
123AST_IMPL(unary_exp) 111AST_IMPL(unary_exp)
@@ -354,7 +342,7 @@ private:
354 case "DotChainItem"_id: 342 case "DotChainItem"_id:
355 case "ColonChainItem"_id: 343 case "ColonChainItem"_id:
356 case "Slice"_id: 344 case "Slice"_id:
357 case "Index"_id: 345 case "Exp"_id:
358 case "Callable"_id: 346 case "Callable"_id:
359 case "String"_id: 347 case "String"_id:
360 return traversal::Stop; 348 return traversal::Stop;
@@ -373,7 +361,7 @@ private:
373 case "DotChainItem"_id: 361 case "DotChainItem"_id:
374 case "ColonChainItem"_id: 362 case "ColonChainItem"_id:
375 case "Slice"_id: 363 case "Slice"_id:
376 case "Index"_id: 364 case "Exp"_id:
377 case "Callable"_id: 365 case "Callable"_id:
378 case "String"_id: 366 case "String"_id:
379 last = node; 367 last = node;
@@ -389,24 +377,22 @@ private:
389 } 377 }
390 } 378 }
391 379
392 std::vector<ast_node*> getChainList(ast_node* chain) { 380 std::vector<ast_node*> getChainList(ChainValue_t* chainValue) {
393 std::vector<ast_node*> list; 381 std::vector<ast_node*> temp;
394 chain->traverse([&](ast_node* node) { 382 switch (chainValue->caller->getId()) {
395 switch (node->getId()) { 383 case "Callable"_id:
396 case "Invoke"_id: 384 temp.push_back(chainValue->caller);
397 case "DotChainItem"_id: 385 break;
398 case "ColonChainItem"_id: 386 case "Chain"_id:
399 case "Slice"_id: 387 const auto& items = chainValue->caller.to<Chain_t>()->items.objects();
400 case "Index"_id: 388 temp.resize(items.size());
401 case "Callable"_id: 389 std::copy(items.begin(), items.end(), temp.begin());
402 case "String"_id: 390 break;
403 list.push_back(node); 391 }
404 return traversal::Return; 392 if (chainValue->arguments) {
405 default: 393 temp.push_back(chainValue->arguments);
406 return traversal::Continue; 394 }
407 } 395 return temp;
408 });
409 return list;
410 } 396 }
411 397
412 void transformStatement(Statement_t* statement, std::vector<std::string>& out) { 398 void transformStatement(Statement_t* statement, std::vector<std::string>& out) {
@@ -1055,99 +1041,64 @@ private:
1055 } 1041 }
1056 } 1042 }
1057 1043
1058 void transformChain(Chain_t* chain, std::vector<std::string>& out) { 1044 void transformChain(Chain_t* chain, std::vector<std::string>& out, bool argsFollowed = false) {
1059 auto item = chain->item.get();
1060 switch (item->getId()) {
1061 case "chain_call"_id: transform_chain_call(static_cast<chain_call_t*>(item), out); break;
1062 case "chain_item"_id: transformChainItems(static_cast<chain_item_t*>(item)->chain, out); break;
1063 case "chain_dot_chain"_id: transform_chain_dot_chain(item, out); break;
1064 case "ColonChain"_id: transformColonChain(static_cast<ColonChain_t*>(item), out); break;
1065 default: break;
1066 }
1067 }
1068
1069 void transform_chain_call(chain_call_t* chain_call, std::vector<std::string>& out) {
1070 std::vector<std::string> temp; 1045 std::vector<std::string> temp;
1071 auto caller = chain_call->caller.get(); 1046 const auto& chainList = chain->items.objects();
1072 switch (caller->getId()) { 1047 if (!argsFollowed && chainList.size() == 2 && chainList.back()->getId() == "ColonChainItem"_id) {
1073 case "Callable"_id: {
1074 transformCallable(static_cast<Callable_t*>(caller), temp, startWithInvoke(chain_call->chain));
1075 break;
1076 }
1077 case "String"_id: transformString(static_cast<String_t*>(caller), temp); break;
1078 default: break;
1079 }
1080 transformChainItems(chain_call->chain, temp);
1081 out.push_back(join(temp));
1082 }
1083 1048
1084 void transformChainItems(ChainItems_t* chainItems, std::vector<std::string>& out) {
1085 std::vector<std::string> temp;
1086 for (auto _chainItem : chainItems->simpleChain.objects()) {
1087 auto chainItem = static_cast<ChainItem_t*>(_chainItem);
1088 transformChainItem(chainItem, temp);
1089 } 1049 }
1090 if (chainItems->colonChain) { 1050 for (auto it = chainList.begin(); it != chainList.end(); ++it) {
1091 transformColonChain(chainItems->colonChain, temp); 1051 auto item = *it;
1052 switch (item->getId()) {
1053 case "Invoke"_id: transformInvoke(static_cast<Invoke_t*>(item), temp); break;
1054 case "DotChainItem"_id: transformDotChainItem(static_cast<DotChainItem_t*>(item), temp); break;
1055 case "ColonChainItem"_id: transformColonChainItem(static_cast<ColonChainItem_t*>(item), temp); break;
1056 case "Slice"_id: transformSlice(static_cast<Slice_t*>(item), temp); break;
1057 case "Callable"_id: {
1058 auto next = it; ++next;
1059 auto followItem = next != chainList.end() ? *next : nullptr;
1060 transformCallable(static_cast<Callable_t*>(item), temp,
1061 followItem && followItem->getId() == "Invoke"_id);
1062 break;
1063 }
1064 case "String"_id:
1065 transformString(static_cast<String_t*>(item), temp);
1066 temp.back() = s("("sv) + temp.back() + s(")"sv);
1067 break;
1068 case "Exp"_id:
1069 transformExp(static_cast<Exp_t*>(item), temp);
1070 temp.back() = s("["sv) + temp.back() + s("]"sv);
1071 break;
1072 default: break;
1073 }
1092 } 1074 }
1093 out.push_back(join(temp)); 1075 out.push_back(join(temp));
1094 } 1076 }
1095 1077
1096 void transformChainItem(ChainItem_t* chainItem, std::vector<std::string>& out) { 1078 void transformDotChainItem(DotChainItem_t* dotChainItem, std::vector<std::string>& out) {
1097 auto item = chainItem->item.get(); 1079 out.push_back(s("."sv) + toString(dotChainItem->name));
1098 switch (item->getId()) {
1099 case "Invoke"_id: transformInvoke(static_cast<Invoke_t*>(item), out); break;
1100 case "DotChainItem"_id:
1101 out.push_back(s("."sv) + toString(item->getFirstChild()));
1102 break;
1103 case "Slice"_id: transformSlice(item, out); break;
1104 case "Exp"_id:
1105 transformExp(static_cast<Exp_t*>(item), out);
1106 out.back() = s("["sv) + out.back() + s("]"sv);
1107 break;
1108 default: break;
1109 }
1110 } 1080 }
1111 1081
1112 void transformInvoke(Invoke_t* invoke, std::vector<std::string>& out) { 1082 void transformColonChainItem(ColonChainItem_t* colonChainItem, std::vector<std::string>& out) {
1113 auto argument = invoke->argument.get(); 1083 out.push_back(s(colonChainItem->switchToDot ? "."sv : ":"sv) + toString(colonChainItem->name));
1114 switch (argument->getId()) {
1115 case "FnArgs"_id: transformFnArgs(static_cast<FnArgs_t*>(argument), out); break;
1116 case "SingleString"_id: transformSingleString(static_cast<SingleString_t*>(argument), out); break;
1117 case "DoubleString"_id: transformDoubleString(static_cast<DoubleString_t*>(argument), out); break;
1118 case "LuaString"_id: transformLuaString(static_cast<LuaString_t*>(argument), out); break;
1119 default: break;
1120 }
1121 out.back() = s("("sv) + out.back() + s(")"sv);
1122 } 1084 }
1123 1085
1124 void transformFnArgs(FnArgs_t* fnArgs, std::vector<std::string>& out) { 1086 void transformSlice(Slice_t* slice, std::vector<std::string>& out) {
1125 std::vector<std::string> temp; 1087 noop(slice, out);
1126 for (auto node : fnArgs->args.objects()) {
1127 transformExp(static_cast<Exp_t*>(node), temp);
1128 }
1129 std::string args = join(temp, ", "sv);
1130 out.push_back(args);
1131 } 1088 }
1132 1089
1133 void transformColonChain(ColonChain_t* colonChain, std::vector<std::string>& out) { 1090 void transformInvoke(Invoke_t* invoke, std::vector<std::string>& out) {
1134 std::vector<std::string> temp;
1135 temp.push_back(
1136 s(colonChain->colonChain->switchToDot ? "."sv : ":"sv) +
1137 toString(colonChain->colonChain->name));
1138 if (colonChain->invokeChain) {
1139 transform_invoke_chain(colonChain->invokeChain, temp);
1140 }
1141 out.push_back(join(temp));
1142 }
1143
1144 void transform_invoke_chain(invoke_chain_t* invoke_chain, std::vector<std::string>& out) {
1145 std::vector<std::string> temp; 1091 std::vector<std::string> temp;
1146 transformInvoke(invoke_chain->invoke, temp); 1092 for (auto arg : invoke->args.objects()) {
1147 if (invoke_chain->chain) { 1093 switch (arg->getId()) {
1148 transformChainItems(invoke_chain->chain, temp); 1094 case "Exp"_id: transformExp(static_cast<Exp_t*>(arg), temp); break;
1095 case "SingleString"_id: transformSingleString(static_cast<SingleString_t*>(arg), temp); break;
1096 case "DoubleString"_id: transformDoubleString(static_cast<DoubleString_t*>(arg), temp); break;
1097 case "LuaString"_id: transformLuaString(static_cast<LuaString_t*>(arg), temp); break;
1098 default: break;
1099 }
1149 } 1100 }
1150 out.push_back(join(temp)); 1101 out.push_back(s("("sv) + join(temp, ", "sv) + s(")"sv));
1151 } 1102 }
1152 1103
1153 void transform_unary_exp(unary_exp_t* unary_exp, std::vector<std::string>& out) { 1104 void transform_unary_exp(unary_exp_t* unary_exp, std::vector<std::string>& out) {
@@ -1168,22 +1119,21 @@ private:
1168 void transformTableLit(TableLit_t* tableLit, std::vector<std::string>& out) { 1119 void transformTableLit(TableLit_t* tableLit, std::vector<std::string>& out) {
1169 std::vector<std::string> temp; 1120 std::vector<std::string> temp;
1170 ast_node* lastNode = nullptr; 1121 ast_node* lastNode = nullptr;
1171 for (auto _tableValue : tableLit->values.objects()) { 1122 for (auto value : tableLit->values.objects()) {
1172 auto tableValue = static_cast<TableValue_t*>(_tableValue);
1173 auto value = tableValue->value.get();
1174 switch (value->getId()) { 1123 switch (value->getId()) {
1175 case "KeyValue"_id: 1124 case "variable_pair"_id:
1125 transform_variable_pair(static_cast<variable_pair_t*>(value), temp);
1126 break;
1127 case "normal_pair"_id:
1128 transform_normal_pair(static_cast<normal_pair_t*>(value), temp);
1129 break;
1176 case "Exp"_id: 1130 case "Exp"_id:
1177 if (value->getId() == "Exp"_id) { 1131 transformExp(static_cast<Exp_t*>(value), temp);
1178 transformExp(static_cast<Exp_t*>(value), temp);
1179 } else {
1180 transformKeyValue(static_cast<KeyValue_t*>(value), temp);
1181 }
1182 temp.back() = (lastNode ? s(","sv) + nll(lastNode) : Empty) + indent(1) + temp.back();
1183 lastNode = value;
1184 break; 1132 break;
1185 default: break; 1133 default: break;
1186 } 1134 }
1135 temp.back() = (lastNode ? s(","sv) + nll(lastNode) : Empty) + indent(1) + temp.back();
1136 lastNode = value;
1187 } 1137 }
1188 out.push_back(s("{"sv) + nll(tableLit) + join(temp) + nlr(tableLit) + indent() + s("}"sv)); 1138 out.push_back(s("{"sv) + nll(tableLit) + join(temp) + nlr(tableLit) + indent() + s("}"sv));
1189 } 1139 }
@@ -1329,14 +1279,12 @@ private:
1329 1279
1330 void transformInvokeArgs(InvokeArgs_t* invokeArgs, std::vector<std::string>& out) { 1280 void transformInvokeArgs(InvokeArgs_t* invokeArgs, std::vector<std::string>& out) {
1331 std::vector<std::string> temp; 1281 std::vector<std::string> temp;
1332 if (invokeArgs->argsList) { 1282 for (auto arg : invokeArgs->args.objects()) {
1333 transformExpList(invokeArgs->argsList, temp); 1283 switch (arg->getId()) {
1334 } 1284 case "Exp"_id: transformExp(static_cast<Exp_t*>(arg), temp); break;
1335 if (invokeArgs->argsTableBlock) { 1285 case "TableBlock"_id: transformTableBlock(arg, temp); break;
1336 transform_invoke_args_with_table(invokeArgs->argsTableBlock, temp); 1286 default: break;
1337 } 1287 }
1338 if (invokeArgs->tableBlock) {
1339 transformTableBlock(invokeArgs->tableBlock, temp);
1340 } 1288 }
1341 out.push_back(join(temp, ", "sv)); 1289 out.push_back(join(temp, ", "sv));
1342 } 1290 }
@@ -1529,37 +1477,35 @@ private:
1529 out.push_back(join(temp)); 1477 out.push_back(join(temp));
1530 } 1478 }
1531 1479
1532 void transformKeyValue(KeyValue_t* keyValue, std::vector<std::string>& out) { 1480 void transform_variable_pair(variable_pair_t* pair, std::vector<std::string>& out) {
1533 auto item = keyValue->item.get(); 1481 out.push_back(toString(pair->name));
1534 switch (item->getId()) { 1482 }
1535 case "variable_pair"_id: 1483
1536 out.push_back(toString(static_cast<variable_pair_t*>(item)->name)); 1484 void transform_normal_pair(normal_pair_t* pair, std::vector<std::string>& out) {
1485 auto key = pair->key.get();
1486 std::vector<std::string> temp;
1487 switch (key->getId()) {
1488 case "KeyName"_id: transformKeyName(static_cast<KeyName_t*>(key), temp); break;
1489 case "Exp"_id:
1490 transformExp(static_cast<Exp_t*>(key), temp);
1491 temp.back() = s("["sv) + temp.back() + s("]"sv);
1537 break; 1492 break;
1538 case "normal_pair"_id: { 1493 case "DoubleString"_id:
1539 auto pair = static_cast<normal_pair_t*>(item); 1494 transformDoubleString(static_cast<DoubleString_t*>(key), temp);
1540 auto key = pair->key.get(); 1495 temp.back() = s("["sv) + temp.back() + s("]"sv);
1541 std::vector<std::string> temp; 1496 break;
1542 switch (key->getId()) { 1497 case "SingleString"_id: transformSingleString(static_cast<SingleString_t*>(key), temp);
1543 case "KeyName"_id: transformKeyName(static_cast<KeyName_t*>(key), temp); break; 1498 temp.back() = s("["sv) + temp.back() + s("]"sv);
1544 case "Exp"_id:
1545 transformExp(static_cast<Exp_t*>(key), temp);
1546 temp.back() = s("["sv) + temp.back() + s("]"sv);
1547 break;
1548 case "DoubleString"_id: transformDoubleString(static_cast<DoubleString_t*>(key), temp); break;
1549 case "SingleString"_id: transformSingleString(static_cast<SingleString_t*>(key), temp); break;
1550 default: break;
1551 }
1552 auto value = pair->value.get();
1553 switch (value->getId()) {
1554 case "Exp"_id: transformExp(static_cast<Exp_t*>(value), temp); break;
1555 case "TableBlock"_id: transformTableBlock(static_cast<TableBlock_t*>(value), temp); break;
1556 default: break;
1557 }
1558 out.push_back(temp[0] + s(" = "sv) + temp[1]);
1559 break; 1499 break;
1560 }
1561 default: break; 1500 default: break;
1562 } 1501 }
1502 auto value = pair->value.get();
1503 switch (value->getId()) {
1504 case "Exp"_id: transformExp(static_cast<Exp_t*>(value), temp); break;
1505 case "TableBlock"_id: transformTableBlock(static_cast<TableBlock_t*>(value), temp); break;
1506 default: break;
1507 }
1508 out.push_back(temp[0] + s(" = "sv) + temp[1]);
1563 } 1509 }
1564 1510
1565 void transformKeyName(KeyName_t* keyName, std::vector<std::string>& out) { 1511 void transformKeyName(KeyName_t* keyName, std::vector<std::string>& out) {
@@ -1802,16 +1748,14 @@ private:
1802 1748
1803 void transform_class_member_list(class_member_list_t* class_member_list, std::list<ClassMember>& out, const std::string& classVar) { 1749 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; 1750 std::vector<std::string> temp;
1805 for (auto _keyValue : class_member_list->values.objects()) { 1751 for (auto keyValue : class_member_list->values.objects()) {
1806 auto keyValue = static_cast<KeyValue_t*>(_keyValue);
1807 bool isBuiltin = false; 1752 bool isBuiltin = false;
1808 do { 1753 do {
1809 auto keyName = static_cast<KeyName_t*>( 1754 auto normal_pair = ast_cast<normal_pair_t>(keyValue);
1810 keyValue->getByPath({"normal_pair"_id, "KeyName"_id}) 1755 if (!normal_pair) break;
1811 ); 1756 auto keyName = normal_pair->key.as<KeyName_t>();
1812 if (!keyName) break; 1757 if (!keyName) break;
1813 auto normal_pair = static_cast<normal_pair_t*>(keyValue->getFirstChild()); 1758 auto nameNode = keyName->name.as<Name_t>();
1814 auto nameNode = keyName->getByPath({"Name"_id});
1815 if (!nameNode) break; 1759 if (!nameNode) break;
1816 auto name = toString(nameNode); 1760 auto name = toString(nameNode);
1817 input newSuperCall; 1761 input newSuperCall;
@@ -1827,8 +1771,8 @@ private:
1827 if (auto chainValue = ast_cast<ChainValue_t>(node)) { 1771 if (auto chainValue = ast_cast<ChainValue_t>(node)) {
1828 if (auto var = chainValue->caller->getByPath({"Variable"_id})) { 1772 if (auto var = chainValue->caller->getByPath({"Variable"_id})) {
1829 if (toString(var) == "super"sv) { 1773 if (toString(var) == "super"sv) {
1830 if (chainValue->arguments && chainValue->arguments->argsList) { 1774 if (chainValue->arguments) {
1831 chainValue->arguments->argsList->exprs.push_front(toAst<Exp_t>("self"sv, Exp)); 1775 chainValue->arguments->args.push_front(toAst<Exp_t>("self"sv, Exp));
1832 _codeCache.push_back(newSuperCall); 1776 _codeCache.push_back(newSuperCall);
1833 var->m_begin.m_it = _codeCache.back().begin(); 1777 var->m_begin.m_it = _codeCache.back().begin();
1834 var->m_end.m_it = _codeCache.back().end(); 1778 var->m_end.m_it = _codeCache.back().end();
@@ -1838,44 +1782,33 @@ private:
1838 var->m_end.m_it = _codeCache.back().end(); 1782 var->m_end.m_it = _codeCache.back().end();
1839 } 1783 }
1840 } 1784 }
1841 } else if (auto var = chainValue->caller->getByPath({"chain_call"_id, "Callable"_id, "Variable"_id})) { 1785 } else if (auto var = chainValue->caller->getByPath({"Callable"_id, "Variable"_id})) {
1842 if (toString(var) == "super"sv) { 1786 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) { 1787 auto insertSelfToArguments = [&](ast_node* item) {
1848 switch (item->getId()) { 1788 switch (item->getId()) {
1849 case "ExpList"_id: 1789 case "InvokeArgs"_id: {
1850 static_cast<ExpList_t*>(item)->exprs.push_front(toAst<Exp_t>("self"sv, Exp)); 1790 auto invoke = static_cast<InvokeArgs_t*>(item);
1851 break; 1791 invoke->args.push_front(toAst<Exp_t>("self"sv, Exp));
1792 return true;
1793 }
1852 case "Invoke"_id: { 1794 case "Invoke"_id: {
1853 auto invoke = static_cast<Invoke_t*>(item); 1795 auto invoke = static_cast<Invoke_t*>(item);
1854 if (auto fnArgs = invoke->argument.as<FnArgs_t>()) { 1796 invoke->args.push_front(toAst<Exp_t>("self"sv, Exp));
1855 fnArgs->args.push_front(toAst<Exp_t>("self"sv, Exp)); 1797 return true;
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 } 1798 }
1870 default: break; 1799 default:
1800 return false;
1871 } 1801 }
1872 }; 1802 };
1803 auto chainList = getChainList(chainValue);
1873 if (chainList.size() == 2) { 1804 if (chainList.size() == 2) {
1874 _codeCache.push_back(newSuperCall); 1805 if (insertSelfToArguments(chainList.back())) {
1806 _codeCache.push_back(newSuperCall);
1807 } else {
1808 _codeCache.push_back(_converter.from_bytes(classVar) + L".__parent");
1809 }
1875 var->m_begin.m_it = _codeCache.back().begin(); 1810 var->m_begin.m_it = _codeCache.back().begin();
1876 var->m_end.m_it = _codeCache.back().end(); 1811 var->m_end.m_it = _codeCache.back().end();
1877 auto item = chainList.back();
1878 insertSelfToArguments(item);
1879 } else if (chainList.size() > 2) { 1812 } else if (chainList.size() > 2) {
1880 _codeCache.push_back(_converter.from_bytes(classVar) + L".__parent"); 1813 _codeCache.push_back(_converter.from_bytes(classVar) + L".__parent");
1881 var->m_begin.m_it = _codeCache.back().begin(); 1814 var->m_begin.m_it = _codeCache.back().begin();
@@ -1896,7 +1829,14 @@ private:
1896 return traversal::Continue; 1829 return traversal::Continue;
1897 }); 1830 });
1898 } while (false); 1831 } while (false);
1899 transformKeyValue(keyValue, temp); 1832 switch (keyValue->getId()) {
1833 case "variable_pair"_id:
1834 transform_variable_pair(static_cast<variable_pair_t*>(keyValue), temp);
1835 break;
1836 case "normal_pair"_id:
1837 transform_normal_pair(static_cast<normal_pair_t*>(keyValue), temp);
1838 break;
1839 }
1900 out.push_back({temp.back(), isBuiltin, keyValue}); 1840 out.push_back({temp.back(), isBuiltin, keyValue});
1901 temp.clear(); 1841 temp.clear();
1902 } 1842 }
@@ -1926,7 +1866,6 @@ private:
1926 void transform_const_value(ast_node* node, std::vector<std::string>& out) {noop(node, out);} 1866 void transform_const_value(ast_node* node, std::vector<std::string>& out) {noop(node, out);}
1927 void transformDo(ast_node* node, std::vector<std::string>& out) {noop(node, out);} 1867 void transformDo(ast_node* node, std::vector<std::string>& out) {noop(node, out);}
1928 void transformTblComprehension(ast_node* node, std::vector<std::string>& out) {noop(node, out);} 1868 void transformTblComprehension(ast_node* node, std::vector<std::string>& out) {noop(node, out);}
1929 void transform_chain_dot_chain(ast_node* node, std::vector<std::string>& out) {noop(node, out);}
1930 void transformSlice(ast_node* node, std::vector<std::string>& out) {noop(node, out);} 1869 void transformSlice(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);} 1870 void transformCompFor(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);} 1871 void transformCompClause(ast_node* node, std::vector<std::string>& out) {noop(node, out);}
@@ -1938,64 +1877,18 @@ const std::string MoonCompliler::Empty;
1938 1877
1939int main() 1878int main()
1940{ 1879{
1941 std::string s = R"TestCodesHere(class Hello 1880 std::string s = R"TestCodesHere(my_func 5,6,7,
1942 new: (@test, @world) => 1881 6, another_func 6,7,8,
1943 print "creating object.." 1882 9,1,2,
1944 hello: => 1883 5,4, :name, :value)TestCodesHere";
1945 print @test, @world 1884/*
1946 __tostring: => "hello world" 1885return my_func(5, 6, 7, 6, another_func(6, 7, 8, 9, 1, 2), 5, 4)
1947 1886
1948x = Hello 1,2 1887with Hello!
1949x\hello() 1888 x = \something!
1950 1889 print x
1951print x 1890 x!
1952 1891*/
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
1966
1967 cool: (num) =>
1968 print "num", num
1969
1970
1971class Simple extends Hi
1972 new: => super "man"
1973 cool: => super 120302
1974
1975x = Simple()
1976x\cool()
1977
1978print x.__class == Simple
1979
1980
1981class Okay
1982 -- what is going on
1983 something: 20323
1984 -- yeaha
1985
1986
1987class Biggie extends Okay
1988 something: =>
1989 super 1,2,3,4
1990 super.something another_self, 1,2,3,4
1991 assert super == Okay
1992
1993
1994class Yeah
1995 okay: =>
1996 super\something 1,2,3,4
1997)TestCodesHere";
1998
1999 MoonCompliler{}.complile(s); 1892 MoonCompliler{}.complile(s);
2000 1893
2001 return 0; 1894 return 0;
diff --git a/MoonParser/moon_ast.h b/MoonParser/moon_ast.h
index 9733bf9..c4f20c3 100644
--- a/MoonParser/moon_ast.h
+++ b/MoonParser/moon_ast.h
@@ -290,11 +290,18 @@ AST_NODE(ChainValue, "ChainValue"_id)
290 ast_ptr<InvokeArgs_t, true> arguments; 290 ast_ptr<InvokeArgs_t, true> arguments;
291AST_END(ChainValue) 291AST_END(ChainValue)
292 292
293class KeyValue_t; 293AST_NODE(variable_pair, "variable_pair"_id)
294 ast_ptr<Variable_t> name;
295AST_END(variable_pair)
296
297AST_NODE(normal_pair, "normal_pair"_id)
298 ast_ptr<ast_node> key; // KeyName_t | [Exp_t] | DoubleString_t | SingleString_t
299 ast_ptr<ast_node> value; // Exp_t | TableBlock_t
300AST_END(normal_pair)
294 301
295AST_NODE(simple_table, "simple_table"_id) 302AST_NODE(simple_table, "simple_table"_id)
296 ast_ptr<Seperator_t> sep; 303 ast_ptr<Seperator_t> sep;
297 ast_list<KeyValue_t> pairs; 304 ast_sel_list<variable_pair_t, normal_pair_t> pairs;
298AST_END(simple_table) 305AST_END(simple_table)
299 306
300class String_t; 307class String_t;
@@ -308,14 +315,6 @@ AST_NODE(SimpleValue, "SimpleValue"_id)
308 */ 315 */
309AST_END(SimpleValue) 316AST_END(SimpleValue)
310 317
311AST_NODE(Chain, "Chain"_id)
312 ast_ptr<ast_node> item; // chain_call_t | chain_item_t | chain_dot_chain_t | ColonChain_t
313AST_END(Chain)
314
315AST_NODE(Value, "Value"_id)
316 ast_ptr<ast_node> item; // SimpleValue_t | simple_table_t | ChainValue_t | String_t
317AST_END(Value)
318
319AST_LEAF(LuaString, "LuaString"_id) 318AST_LEAF(LuaString, "LuaString"_id)
320AST_END(LuaString) 319AST_END(LuaString)
321 320
@@ -338,26 +337,6 @@ AST_NODE(String, "String"_id)
338 ast_ptr<ast_node> str; // DoubleString_t | SingleString_t | LuaString_t 337 ast_ptr<ast_node> str; // DoubleString_t | SingleString_t | LuaString_t
339AST_END(String) 338AST_END(String)
340 339
341AST_NODE(Parens, "Parens"_id)
342 ast_ptr<Exp_t> expr;
343AST_END(Parens)
344
345AST_NODE(FnArgs, "FnArgs"_id)
346 ast_ptr<Seperator_t> sep;
347 ast_list<Exp_t> args;
348AST_END(FnArgs)
349
350class ChainItems_t;
351
352AST_NODE(chain_call, "chain_call"_id)
353 ast_ptr<ast_node> caller; // Callable_t | String_t
354 ast_ptr<ChainItems_t> chain;
355AST_END(chain_call)
356
357AST_NODE(chain_item, "chain_item"_id)
358 ast_ptr<ChainItems_t> chain;
359AST_END(chain_item)
360
361AST_NODE(DotChainItem, "DotChainItem"_id) 340AST_NODE(DotChainItem, "DotChainItem"_id)
362 ast_ptr<Name_t> name; 341 ast_ptr<Name_t> name;
363AST_END(DotChainItem) 342AST_END(DotChainItem)
@@ -367,67 +346,46 @@ AST_NODE(ColonChainItem, "ColonChainItem"_id)
367 bool switchToDot = false; 346 bool switchToDot = false;
368AST_END(ColonChainItem) 347AST_END(ColonChainItem)
369 348
370AST_NODE(chain_dot_chain, "chain_dot_chain"_id)
371 ast_ptr<DotChainItem_t> caller;
372 ast_ptr<ChainItems_t, true> chain;
373AST_END(chain_dot_chain)
374
375class ColonChain_t;
376class Invoke_t;
377class Slice_t;
378
379AST_NODE(ChainItem, "ChainItem"_id)
380 ast_ptr<ast_node> item; // Invoke_t | DotChainItem_t | Slice_t | [Exp_t]
381AST_END(ChainItem)
382
383AST_NODE(ChainItems, "ChainItems"_id)
384 ast_ptr<Seperator_t> sep;
385 ast_list<ChainItem_t> simpleChain;
386 ast_ptr<ColonChain_t, true> colonChain;
387AST_END(ChainItems)
388
389AST_NODE(invoke_chain, "invoke_chain"_id)
390 ast_ptr<Invoke_t> invoke;
391 ast_ptr<ChainItems_t, true> chain;
392AST_END(invoke_chain)
393
394AST_NODE(ColonChain, "ColonChain"_id)
395 ast_ptr<ColonChainItem_t> colonChain;
396 ast_ptr<invoke_chain_t, true> invokeChain;
397AST_END(ColonChain)
398
399AST_LEAF(default_value, "default_value"_id)
400AST_END(default_value)
401
402AST_NODE(Slice, "Slice"_id) 349AST_NODE(Slice, "Slice"_id)
403 ast_ptr<ast_node> startValue; // Exp_t | default_value_t 350 ast_ptr<ast_node> startValue; // Exp_t | default_value_t
404 ast_ptr<ast_node> stopValue; // Exp_t | default_value_t 351 ast_ptr<ast_node> stopValue; // Exp_t | default_value_t
405 ast_ptr<ast_node> stepValue; // Exp_t | default_value_t 352 ast_ptr<ast_node> stepValue; // Exp_t | default_value_t
406AST_END(Slice) 353AST_END(Slice)
407 354
355AST_NODE(Parens, "Parens"_id)
356 ast_ptr<Exp_t> expr;
357AST_END(Parens)
358
408AST_NODE(Invoke, "Invoke"_id) 359AST_NODE(Invoke, "Invoke"_id)
409 ast_ptr<ast_node> argument; // FnArgs_t | SingleString_t | DoubleString_t | LuaString_t 360 ast_ptr<Seperator_t> sep;
361 ast_sel_list<Exp_t, SingleString_t, DoubleString_t, LuaString_t> args;
410AST_END(Invoke) 362AST_END(Invoke)
411 363
412class KeyValue_t; 364AST_NODE(Chain, "Chain"_id)
365 ast_ptr<Seperator_t> sep;
366 ast_sel_list<Callable_t, Invoke_t, DotChainItem_t, ColonChainItem_t, Slice_t, Exp_t> items;
367AST_END(Chain)
413 368
414AST_NODE(TableValue, "TableValue"_id) 369AST_NODE(Value, "Value"_id)
415 ast_ptr<ast_node> value; // KeyValue_t | Exp_t 370 ast_ptr<ast_node> item; // SimpleValue_t | simple_table_t | ChainValue_t | String_t
416AST_END(TableValue) 371AST_END(Value)
372
373AST_LEAF(default_value, "default_value"_id)
374AST_END(default_value)
417 375
418AST_NODE(TableLit, "TableLit"_id) 376AST_NODE(TableLit, "TableLit"_id)
419 ast_ptr<Seperator_t> sep; 377 ast_ptr<Seperator_t> sep;
420 ast_list<TableValue_t> values; 378 ast_sel_list<variable_pair_t, normal_pair_t, Exp_t> values;
421AST_END(TableLit) 379AST_END(TableLit)
422 380
423AST_NODE(TableBlock, "TableBlock"_id) 381AST_NODE(TableBlock, "TableBlock"_id)
424 ast_ptr<Seperator_t> sep; 382 ast_ptr<Seperator_t> sep;
425 ast_list<KeyValue_t> values; 383 ast_sel_list<variable_pair_t, normal_pair_t> values;
426AST_END(TableBlock) 384AST_END(TableBlock)
427 385
428AST_NODE(class_member_list, "class_member_list"_id) 386AST_NODE(class_member_list, "class_member_list"_id)
429 ast_ptr<Seperator_t> sep; 387 ast_ptr<Seperator_t> sep;
430 ast_list<KeyValue_t> values; 388 ast_sel_list<variable_pair_t, normal_pair_t> values;
431AST_END(class_member_list) 389AST_END(class_member_list)
432 390
433AST_NODE(ClassLine, "ClassLine"_id) 391AST_NODE(ClassLine, "ClassLine"_id)
@@ -457,19 +415,6 @@ AST_NODE(Export, "Export"_id)
457 ast_ptr<ast_node> item; // ClassDecl_t | export_op_t | export_values_t 415 ast_ptr<ast_node> item; // ClassDecl_t | export_op_t | export_values_t
458AST_END(Export) 416AST_END(Export)
459 417
460AST_NODE(variable_pair, "variable_pair"_id)
461 ast_ptr<Variable_t> name;
462AST_END(variable_pair)
463
464AST_NODE(normal_pair, "normal_pair"_id)
465 ast_ptr<ast_node> key; // KeyName_t | [Exp_t] | DoubleString_t | SingleString_t
466 ast_ptr<ast_node> value; // Exp_t | TableBlock_t
467AST_END(normal_pair)
468
469AST_NODE(KeyValue, "KeyValue"_id)
470 ast_ptr<ast_node> item; // variable_pair_t | normal_pair_t
471AST_END(KeyValue)
472
473AST_NODE(FnArgDef, "FnArgDef"_id) 418AST_NODE(FnArgDef, "FnArgDef"_id)
474 ast_ptr<ast_node> name; // Variable_t | SelfName_t 419 ast_ptr<ast_node> name; // Variable_t | SelfName_t
475 ast_ptr<Exp_t, true> defaultValue; 420 ast_ptr<Exp_t, true> defaultValue;
@@ -508,20 +453,9 @@ AST_NODE(AssignableNameList, "AssignableNameList"_id)
508 ast_list<NameOrDestructure_t> items; 453 ast_list<NameOrDestructure_t> items;
509AST_END(AssignableNameList) 454AST_END(AssignableNameList)
510 455
511AST_NODE(ArgBlock, "ArgBlock"_id)
512 ast_ptr<Seperator_t> sep;
513 ast_list<Exp_t> arguments;
514AST_END(ArgBlock)
515
516AST_NODE(invoke_args_with_table, "invoke_args_with_table"_id)
517 ast_ptr<ArgBlock_t, true> argBlock;
518 ast_ptr<TableBlock_t, true> tableBlock;
519AST_END(invoke_args_with_table)
520
521AST_NODE(InvokeArgs, "InvokeArgs"_id) 456AST_NODE(InvokeArgs, "InvokeArgs"_id)
522 ast_ptr<ExpList_t, true> argsList; 457 ast_ptr<Seperator_t> sep;
523 ast_ptr<invoke_args_with_table_t, true> argsTableBlock; 458 ast_sel_list<Exp_t, TableBlock_t> args;
524 ast_ptr<TableBlock_t, true> tableBlock;
525AST_END(InvokeArgs) 459AST_END(InvokeArgs)
526 460
527AST_LEAF(const_value, "const_value"_id) 461AST_LEAF(const_value, "const_value"_id)
diff --git a/MoonParser/moon_parser.cpp b/MoonParser/moon_parser.cpp
index ec8df38..8917e58 100644
--- a/MoonParser/moon_parser.cpp
+++ b/MoonParser/moon_parser.cpp
@@ -312,30 +312,24 @@ rule Parens = sym('(') >> *SpaceBreak >> Exp >> *SpaceBreak >> sym(')');
312rule Callable = Space >> Variable | SelfName | VarArg | Parens; 312rule Callable = Space >> Variable | SelfName | VarArg | Parens;
313rule FnArgsExpList = Exp >> *((Break | sym(',')) >> White >> Exp); 313rule FnArgsExpList = Exp >> *((Break | sym(',')) >> White >> Exp);
314 314
315rule FnArgs = Seperator >> 315rule FnArgs = (symx('(') >> *SpaceBreak >> -FnArgsExpList >> *SpaceBreak >> sym(')')) |
316( 316 (sym('!') >> not_(expr('=')));
317 (
318 symx('(') >> *SpaceBreak >> -FnArgsExpList >> *SpaceBreak >> sym(')')
319 ) | (
320 sym('!') >> not_(expr('='))
321 )
322);
323 317
324extern rule ChainItems, DotChainItem, ColonChain; 318extern rule ChainItems, DotChainItem, ColonChain;
325 319
326rule chain_call = (Callable | String) >> ChainItems; 320rule chain_call = (Callable | String) >> ChainItems;
327rule chain_item = not_(set(".\\")) >> ChainItems; 321rule chain_item = and_(set(".\\")) >> ChainItems;
328rule chain_dot_chain = DotChainItem >> -ChainItems; 322rule chain_dot_chain = DotChainItem >> -ChainItems;
329 323
330rule Chain = 324rule Chain = Seperator >> (
331 chain_call | 325 chain_call |
332 chain_item | 326 chain_item |
333 Space >> (chain_dot_chain | ColonChain); 327 Space >> (chain_dot_chain | ColonChain));
334 328
335extern rule ChainItem; 329extern rule ChainItem;
336 330
337rule chain_with_colon = +ChainItem >> -ColonChain; 331rule chain_with_colon = +ChainItem >> -ColonChain;
338rule ChainItems = Seperator >> (chain_with_colon | ColonChain); 332rule ChainItems = chain_with_colon | ColonChain;
339 333
340extern rule Invoke, Slice; 334extern rule Invoke, Slice;
341 335
@@ -355,11 +349,11 @@ rule Slice =
355 (sym(',') >> Exp | default_value) >> 349 (sym(',') >> Exp | default_value) >>
356 sym(']'); 350 sym(']');
357 351
358rule Invoke = 352rule Invoke = Seperator >> (
359 FnArgs | 353 FnArgs |
360 SingleString | 354 SingleString |
361 DoubleString | 355 DoubleString |
362 and_(expr('[')) >> LuaString; 356 and_(expr('[')) >> LuaString);
363 357
364extern rule TableValueList, TableLitLine; 358extern rule TableValueList, TableLitLine;
365 359
@@ -448,7 +442,7 @@ rule ExpList = Seperator >> Exp >> *(sym(',') >> Exp);
448rule ExpListLow = Seperator >> Exp >> *((sym(',') | sym(';')) >> Exp); 442rule ExpListLow = Seperator >> Exp >> *((sym(',') | sym(';')) >> Exp);
449 443
450rule ArgLine = CheckIndent >> Exp >> *(sym(',') >> Exp); 444rule ArgLine = CheckIndent >> Exp >> *(sym(',') >> Exp);
451rule ArgBlock = Seperator >> ArgLine >> *(sym(',') >> SpaceBreak >> ArgLine) >> PopIndent; 445rule ArgBlock = ArgLine >> *(sym(',') >> SpaceBreak >> ArgLine) >> PopIndent;
452 446
453rule invoke_args_with_table = 447rule invoke_args_with_table =
454 sym(',') >> 448 sym(',') >>
@@ -458,9 +452,9 @@ rule invoke_args_with_table =
458 ); 452 );
459 453
460rule InvokeArgs = 454rule InvokeArgs =
461 not_(expr('-')) >> 455 not_(expr('-')) >> Seperator >>
462 ( 456 (
463 ExpList >> -(invoke_args_with_table | TableBlock) | 457 Exp >> *(sym(',') >> Exp) >> -(invoke_args_with_table | TableBlock) |
464 TableBlock 458 TableBlock
465 ); 459 );
466 460