From 5440c952f2c73da6fa6a752d8628eaf25015c070 Mon Sep 17 00:00:00 2001
From: Li Jin <dragon-fly@qq.com>
Date: Thu, 17 Sep 2020 08:47:29 +0800
Subject: make simple table and table block appear in the end of function
 arguments merged.

---
 spec/inputs/syntax.moon     |  4 ++--
 spec/inputs/tables.moon     |  2 +-
 src/MoonP/moon_compiler.cpp | 33 ++++++++++++++++++++++++++++++++-
 3 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/spec/inputs/syntax.moon b/spec/inputs/syntax.moon
index 99daac5..32d480e 100644
--- a/spec/inputs/syntax.moon
+++ b/spec/inputs/syntax.moon
@@ -240,7 +240,7 @@ hello "comma",
  something: hello_world
  frick: you
 
--- creates two tables
+-- creates one table
 another hello, one,
 	two, three, four, yeah: man
 	okay: yeah
@@ -252,7 +252,7 @@ another hello, one, two, three, four,
 
 another hello, one, two, three, four, yeah: man
 	okay: yeah
- 
+
 -- 
 a += 3 - 5
 a *= 3 + 5
diff --git a/spec/inputs/tables.moon b/spec/inputs/tables.moon
index 079be35..6375660 100644
--- a/spec/inputs/tables.moon
+++ b/spec/inputs/tables.moon
@@ -125,7 +125,7 @@ kam = {
 		one_thing =>
 }
 
--- TODO: both of these have undesirable output
+-- both of these have desirable output
 keepit going: true,
 	okay: "yeah",
 	workd: "okay"
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp
index 406b412..112cae7 100644
--- a/src/MoonP/moon_compiler.cpp
+++ b/src/MoonP/moon_compiler.cpp
@@ -49,7 +49,7 @@ inline std::string s(std::string_view sv) {
 }
 
 const std::string_view version() {
-	return "0.4.12"sv;
+	return "0.4.14"sv;
 }
 
 // name of table stored in lua registry
@@ -3603,6 +3603,37 @@ private:
 	}
 
 	void transformInvokeArgs(InvokeArgs_t* invokeArgs, str_list& out) {
+			if (invokeArgs->args.size() > 1) {
+			/* merge all the key-value pairs into one table
+			 from arguments in the end */
+			auto lastArg = invokeArgs->args.back();
+			_ast_list* lastTable = nullptr;
+			if (auto tableBlock = ast_cast<TableBlock_t>(lastArg)) {
+				lastTable = &tableBlock->values;
+			} else if (auto value = singleValueFrom(lastArg)) {
+				if (auto simpleTable = ast_cast<simple_table_t>(value->item)) {
+					lastTable = &simpleTable->pairs;
+				}
+			}
+			if (lastTable) {
+				ast_ptr<false, ast_node> ref(lastArg);
+				invokeArgs->args.pop_back();
+				while (!invokeArgs->args.empty()) {
+					if (Value_t* value = singleValueFrom(invokeArgs->args.back())) {
+						if (auto tb = value->item.as<simple_table_t>()) {
+							const auto& ps = tb->pairs.objects();
+							for (auto it = ps.rbegin(); it != ps.rend(); ++it) {
+								lastTable->push_front(*it);
+							}
+							invokeArgs->args.pop_back();
+							continue;
+						}
+					}
+					break;
+				}
+				invokeArgs->args.push_back(lastArg);
+			}
+		}
 		str_list temp;
 		for (auto arg : invokeArgs->args.objects()) {
 			switch (arg->getId()) {
-- 
cgit v1.2.3-55-g6feb