aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-05-16 09:45:04 +0800
committerLi Jin <dragon-fly@qq.com>2022-05-16 09:45:27 +0800
commita9fe6a5c4c17cf7e8305c1d614939ce510fb1103 (patch)
tree404509931668656356cce49493f10e27d1cc32e6
parentc4935826eea8df7a161b2b4ba9262dce66d9d366 (diff)
downloadyuescript-a9fe6a5c4c17cf7e8305c1d614939ce510fb1103.tar.gz
yuescript-a9fe6a5c4c17cf7e8305c1d614939ce510fb1103.tar.bz2
yuescript-a9fe6a5c4c17cf7e8305c1d614939ce510fb1103.zip
fix issue #97 by line decorator.
-rw-r--r--spec/inputs/attrib.yue8
-rw-r--r--spec/outputs/attrib.lua39
-rw-r--r--spec/outputs/loops.lua4
-rwxr-xr-xsrc/yuescript/yue_compiler.cpp64
4 files changed, 111 insertions, 4 deletions
diff --git a/spec/inputs/attrib.yue b/spec/inputs/attrib.yue
index 7e9a42c..500ef0f 100644
--- a/spec/inputs/attrib.yue
+++ b/spec/inputs/attrib.yue
@@ -14,6 +14,14 @@ do
14 close f = with io.open "file.txt" 14 close f = with io.open "file.txt"
15 \write "Hello" 15 \write "Hello"
16 16
17do
18 const a = 1 if true
19 close b = (if x then 1) unless false
20 const c = (switch x
21 when "abc" then 998) if true
22 close d =
23 :value if a ?? b
24
17macro defer = (item)-> "close _ = #{item}" 25macro defer = (item)-> "close _ = #{item}"
18macro defer_f = (func)-> "close _ = setmetatable {},__close:#{func}" 26macro defer_f = (func)-> "close _ = setmetatable {},__close:#{func}"
19 27
diff --git a/spec/outputs/attrib.lua b/spec/outputs/attrib.lua
index d9ac3f2..aed549d 100644
--- a/spec/outputs/attrib.lua
+++ b/spec/outputs/attrib.lua
@@ -25,6 +25,45 @@ do
25 end)() 25 end)()
26end 26end
27do 27do
28 local a <const> = (function()
29 if true then
30 return 1
31 end
32 end)()
33 local b <close> = (function()
34 if not false then
35 return ((function()
36 if x then
37 return 1
38 end
39 end)())
40 end
41 end)()
42 local c <const> = (function()
43 if true then
44 return ((function()
45 local _exp_0 = x
46 if "abc" == _exp_0 then
47 return 998
48 end
49 end)())
50 end
51 end)()
52 local d <close> = (function()
53 if (function()
54 if a ~= nil then
55 return a
56 else
57 return b
58 end
59 end)() then
60 return {
61 value = value
62 }
63 end
64 end)()
65end
66do
28 local _ <close> = (function() 67 local _ <close> = (function()
29 local _with_0 = io.open("file.txt") 68 local _with_0 = io.open("file.txt")
30 _with_0:write("Hello") 69 _with_0:write("Hello")
diff --git a/spec/outputs/loops.lua b/spec/outputs/loops.lua
index bcd559c..7f87b2d 100644
--- a/spec/outputs/loops.lua
+++ b/spec/outputs/loops.lua
@@ -187,7 +187,7 @@ for i = 1, 10 do
187end 187end
188local a = 1 188local a = 1
189repeat 189repeat
190 local _cond_0 190 local _cond_0 = false
191 local _continue_0 = false 191 local _continue_0 = false
192 repeat 192 repeat
193 a = a + 1 193 a = a + 1
@@ -208,7 +208,7 @@ repeat
208until _cond_0 208until _cond_0
209x = 0 209x = 0
210repeat 210repeat
211 local _cond_0 211 local _cond_0 = false
212 local _continue_0 = false 212 local _continue_0 = false
213 repeat 213 repeat
214 x = x + 1 214 x = x + 1
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index 6f641d9..6d90d24 100755
--- a/src/yuescript/yue_compiler.cpp
+++ b/src/yuescript/yue_compiler.cpp
@@ -60,7 +60,7 @@ using namespace parserlib;
60 60
61typedef std::list<std::string> str_list; 61typedef std::list<std::string> str_list;
62 62
63const std::string_view version = "0.10.20"sv; 63const std::string_view version = "0.10.21"sv;
64const std::string_view extension = "yue"sv; 64const std::string_view extension = "yue"sv;
65 65
66class YueCompilerImpl { 66class YueCompilerImpl {
@@ -925,6 +925,66 @@ private:
925 local->defined = true; 925 local->defined = true;
926 transformLocalDef(local, out); 926 transformLocalDef(local, out);
927 } 927 }
928 } else if (auto attrib = statement->content.as<LocalAttrib_t>()) {
929 auto appendix = statement->appendix.get();
930 switch (appendix->item->getId()) {
931 case id<if_line_t>(): {
932 auto if_line = static_cast<if_line_t*>(appendix->item.get());
933 auto ifNode = x->new_ptr<If_t>();
934 ifNode->type.set(if_line->type);
935 ifNode->nodes.push_back(if_line->condition);
936
937 auto expList = x->new_ptr<ExpList_t>();
938 for (auto val : attrib->assign->values.objects()) {
939 switch (val->getId()) {
940 case id<If_t>():
941 case id<Switch_t>():
942 case id<With_t>(): {
943 auto simpleValue = x->new_ptr<SimpleValue_t>();
944 simpleValue->value.set(val);
945 auto value = x->new_ptr<Value_t>();
946 value->item.set(simpleValue);
947 auto exp = newExp(value, x);
948 expList->exprs.push_back(exp);
949 break;
950 }
951 case id<TableBlock_t>(): {
952 auto tableBlock = static_cast<TableBlock_t*>(val);
953 auto tabLit = x->new_ptr<TableLit_t>();
954 tabLit->values.dup(tableBlock->values);
955 auto simpleValue = x->new_ptr<SimpleValue_t>();
956 simpleValue->value.set(tabLit);
957 auto value = x->new_ptr<Value_t>();
958 value->item.set(simpleValue);
959 auto exp = newExp(value, x);
960 expList->exprs.push_back(exp);
961 break;
962 }
963 case id<Exp_t>(): {
964 expList->exprs.push_back(val);
965 break;
966 }
967 default: YUEE("AST node mismatch", val); break;
968 }
969 }
970 auto expListAssign = x->new_ptr<ExpListAssign_t>();
971 expListAssign->expList.set(expList);
972 auto stmt = x->new_ptr<Statement_t>();
973 stmt->content.set(expListAssign);
974 ifNode->nodes.push_back(stmt);
975
976 statement->appendix.set(nullptr);
977 attrib->assign->values.clear();
978 attrib->assign->values.push_back(ifNode);
979 transformStatement(statement, out);
980 return;
981 }
982 case id<CompInner_t>(): {
983 throw std::logic_error(_info.errorMessage("for-loop line decorator is not supported here"sv, appendix->item.get()));
984 break;
985 }
986 default: YUEE("AST node mismatch", appendix->item.get()); break;
987 }
928 } 988 }
929 auto appendix = statement->appendix.get(); 989 auto appendix = statement->appendix.get();
930 switch (appendix->item->getId()) { 990 switch (appendix->item->getId()) {
@@ -5345,7 +5405,7 @@ private:
5345 auto continueVar = getUnusedName("_continue_"sv); 5405 auto continueVar = getUnusedName("_continue_"sv);
5346 forceAddToScope(continueVar); 5406 forceAddToScope(continueVar);
5347 _continueVars.push(continueVar); 5407 _continueVars.push(continueVar);
5348 _buf << indent() << "local "sv << conditionVar << nll(body); 5408 _buf << indent() << "local "sv << conditionVar << " = false"sv << nll(body);
5349 _buf << indent() << "local "sv << continueVar << " = false"sv << nll(body); 5409 _buf << indent() << "local "sv << continueVar << " = false"sv << nll(body);
5350 _buf << indent() << "repeat"sv << nll(body); 5410 _buf << indent() << "repeat"sv << nll(body);
5351 pushScope(); 5411 pushScope();