summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-02-15 09:55:18 +0800
committerLi Jin <dragon-fly@qq.com>2022-02-15 09:55:18 +0800
commitf4e2286da2b0212b0a7ac76821203bd398cfac5b (patch)
tree74e009d36b1cd67426ba729c8cb073f414f86cbf
parentddb0b9deb720368a425d00bce0c0352469b55911 (diff)
downloadyuescript-f4e2286da2b0212b0a7ac76821203bd398cfac5b.tar.gz
yuescript-f4e2286da2b0212b0a7ac76821203bd398cfac5b.tar.bz2
yuescript-f4e2286da2b0212b0a7ac76821203bd398cfac5b.zip
fix a global variable check case.
-rw-r--r--spec/outputs/backcall.lua19
-rw-r--r--spec/outputs/destructure.lua7
-rw-r--r--spec/outputs/existential.lua161
-rw-r--r--spec/outputs/multiline_chain.lua7
-rw-r--r--spec/outputs/pipe.lua50
-rw-r--r--spec/outputs/vararg.lua10
-rwxr-xr-xsrc/yuescript/yue_compiler.cpp22
7 files changed, 183 insertions, 93 deletions
diff --git a/spec/outputs/backcall.lua b/spec/outputs/backcall.lua
index 9f7ec29..3a4c5e9 100644
--- a/spec/outputs/backcall.lua
+++ b/spec/outputs/backcall.lua
@@ -31,16 +31,19 @@ do
31 end)()) 31 end)())
32end 32end
33do 33do
34 if http ~= nil then 34 do
35 http.get("ajaxtest", function(data) 35 local _obj_0 = http
36 body[".result"]:html(data) 36 if _obj_0 ~= nil then
37 return http.post("ajaxprocess", data, function(processed) 37 _obj_0.get("ajaxtest", function(data)
38 body[".result"]:append(processed) 38 body[".result"]:html(data)
39 return setTimeout(1000, function() 39 return http.post("ajaxprocess", data, function(processed)
40 return print("done") 40 body[".result"]:append(processed)
41 return setTimeout(1000, function()
42 return print("done")
43 end)
41 end) 44 end)
42 end) 45 end)
43 end) 46 end
44 end 47 end
45end 48end
46do 49do
diff --git a/spec/outputs/destructure.lua b/spec/outputs/destructure.lua
index 9ca13c8..1862b85 100644
--- a/spec/outputs/destructure.lua
+++ b/spec/outputs/destructure.lua
@@ -298,8 +298,11 @@ do
298 end 298 end
299 local _obj_1 = getmetatable(tb).func 299 local _obj_1 = getmetatable(tb).func
300 if _obj_1 == nil then 300 if _obj_1 == nil then
301 if item ~= nil then 301 do
302 _obj_1 = item.defVal 302 local _obj_2 = item
303 if _obj_2 ~= nil then
304 _obj_1 = _obj_2.defVal
305 end
303 end 306 end
304 end 307 end
305 a.b(function() 308 a.b(function()
diff --git a/spec/outputs/existential.lua b/spec/outputs/existential.lua
index b594218..b241b3b 100644
--- a/spec/outputs/existential.lua
+++ b/spec/outputs/existential.lua
@@ -1,18 +1,28 @@
1if f1 ~= nil then 1do
2 f1() 2 local _obj_0 = f1
3 if _obj_0 ~= nil then
4 _obj_0()
5 end
3end 6end
4if f2 ~= nil then 7do
5 f2("arg0", 123) 8 local _obj_0 = f2
9 if _obj_0 ~= nil then
10 _obj_0("arg0", 123)
11 end
6end 12end
7local x 13local x
8if tab ~= nil then 14do
9 x = tab.value 15 local _obj_0 = tab
16 if _obj_0 ~= nil then
17 x = _obj_0.value
18 end
10end 19end
11print((function() 20print((function()
12 if abc ~= nil then 21 local _obj_0 = abc
13 local _obj_0 = abc["hello world"] 22 if _obj_0 ~= nil then
14 if _obj_0 ~= nil then 23 local _obj_1 = _obj_0["hello world"]
15 return _obj_0.xyz 24 if _obj_1 ~= nil then
25 return _obj_1.xyz
16 end 26 end
17 return nil 27 return nil
18 end 28 end
@@ -21,16 +31,24 @@ end)())
21if print and (x ~= nil) then 31if print and (x ~= nil) then
22 print(x) 32 print(x)
23end 33end
24if self ~= nil then 34do
25 self:func(998) 35 local _obj_0 = self
36 if _obj_0 ~= nil then
37 _obj_0:func(998)
38 end
26end 39end
27do 40do
28 local _with_0 41 local _with_0
29 if abc ~= nil then 42 do
30 local _obj_0 = abc() 43 local _obj_0 = abc
31 local _obj_1 = _obj_0.func 44 if _obj_0 ~= nil then
32 if _obj_1 ~= nil then 45 do
33 _with_0 = _obj_1(_obj_0) 46 local _obj_1 = _obj_0()
47 local _obj_2 = _obj_1.func
48 if _obj_2 ~= nil then
49 _with_0 = _obj_2(_obj_1)
50 end
51 end
34 end 52 end
35 end 53 end
36 if (function() 54 if (function()
@@ -45,19 +63,29 @@ do
45end 63end
46do 64do
47 local _des_0 65 local _des_0
48 if a ~= nil then 66 do
49 local _obj_0 = a["if"] 67 local _obj_0 = a
50 if _obj_0 ~= nil then 68 if _obj_0 ~= nil then
51 local _obj_1 = _obj_0["then"] 69 do
52 if _obj_1 ~= nil then 70 local _obj_1 = _obj_0["if"]
53 local _obj_2 = _obj_1(_obj_0, 123) 71 if _obj_1 ~= nil then
54 if _obj_2 ~= nil then 72 do
55 _des_0 = _obj_2((function() 73 local _obj_2 = _obj_1["then"]
56 if self ~= nil then 74 if _obj_2 ~= nil then
57 return self["function"](self, 998) 75 do
76 local _obj_3 = _obj_2(_obj_1, 123)
77 if _obj_3 ~= nil then
78 _des_0 = _obj_3((function()
79 local _obj_4 = self
80 if _obj_4 ~= nil then
81 return _obj_4["function"](_obj_4, 998)
82 end
83 return nil
84 end)())
85 end
86 end
58 end 87 end
59 return nil 88 end
60 end)())
61 end 89 end
62 end 90 end
63 end 91 end
@@ -97,41 +125,54 @@ if (window ~= nil) then
97 local environment = 'browser (probably)' 125 local environment = 'browser (probably)'
98end 126end
99local zip 127local zip
100local _obj_0 = lottery.drawWinner 128do
101if _obj_0 ~= nil then 129 local _obj_0 = lottery.drawWinner
102 local _obj_1 = _obj_0().address 130 if _obj_0 ~= nil then
103 if _obj_1 ~= nil then 131 do
104 zip = _obj_1.zipcode 132 local _obj_1 = _obj_0().address
133 if _obj_1 ~= nil then
134 zip = _obj_1.zipcode
135 end
136 end
105 end 137 end
106end 138end
107local len = (function() 139local len = (function()
108 if utf8 ~= nil then 140 local _obj_0 = utf8
109 return utf8.len 141 if _obj_0 ~= nil then
142 return _obj_0.len
110 end 143 end
111 return nil 144 return nil
112end)() or (function() 145end)() or (function()
113 if string ~= nil then 146 local _obj_0 = string
114 return string.len 147 if _obj_0 ~= nil then
148 return _obj_0.len
115 end 149 end
116 return nil 150 return nil
117end)() or function(o) 151end)() or function(o)
118 return #o 152 return #o
119end 153end
120local a 154local a
121if tb1 ~= nil then 155do
122 local _obj_1 = tb1["end"] 156 local _obj_0 = tb1
123 if _obj_1 ~= nil then 157 if _obj_0 ~= nil then
124 a = _obj_1(tb1, 123 + (function() 158 do
125 if tb2 ~= nil then 159 local _obj_1 = _obj_0["end"]
126 return tb2["then"](tb2, 456) 160 if _obj_1 ~= nil then
161 a = _obj_1(_obj_0, 123 + (function()
162 local _obj_2 = tb2
163 if _obj_2 ~= nil then
164 return _obj_2["then"](_obj_2, 456)
165 end
166 return nil
167 end)())
127 end 168 end
128 return nil 169 end
129 end)())
130 end 170 end
131end 171end
132local b = ((function() 172local b = ((function()
133 if tb1 ~= nil then 173 local _obj_0 = tb1
134 local _base_0 = tb1 174 if _obj_0 ~= nil then
175 local _base_0 = _obj_0
135 local _fn_0 = _base_0["end"] 176 local _fn_0 = _base_0["end"]
136 return _fn_0 and function(...) 177 return _fn_0 and function(...)
137 return _fn_0(_base_0, ...) 178 return _fn_0(_base_0, ...)
@@ -139,8 +180,9 @@ local b = ((function()
139 end 180 end
140 return nil 181 return nil
141end)() ~= nil) or (function() 182end)() ~= nil) or (function()
142 if tb2 ~= nil then 183 local _obj_0 = tb2
143 local _base_0 = tb2 184 if _obj_0 ~= nil then
185 local _base_0 = _obj_0
144 local _fn_0 = _base_0["then"] 186 local _fn_0 = _base_0["then"]
145 return _fn_0 and function(...) 187 return _fn_0 and function(...)
146 return _fn_0(_base_0, ...) 188 return _fn_0(_base_0, ...)
@@ -155,17 +197,24 @@ do
155 _with_0:close() 197 _with_0:close()
156 end 198 end
157end 199end
158if tb ~= nil then 200do
159 local _obj_1 = getmetatable(tb).__a 201 local _obj_0 = tb
160 if _obj_1 ~= nil then 202 if _obj_0 ~= nil then
161 _obj_1(123) 203 do
204 local _obj_1 = getmetatable(_obj_0).__a
205 if _obj_1 ~= nil then
206 _obj_1(123)
207 end
208 end
162 end 209 end
163end 210end
164do 211do
165 local _with_0 212 local _with_0
166 local _obj_1 = getmetatable(tb) 213 do
167 if _obj_1 ~= nil then 214 local _obj_0 = getmetatable(tb)
168 _with_0 = getmetatable(_obj_1).__index 215 if _obj_0 ~= nil then
216 _with_0 = getmetatable(_obj_0).__index
217 end
169 end 218 end
170 if _with_0 ~= nil then 219 if _with_0 ~= nil then
171 _with_0.a = 1 220 _with_0.a = 1
diff --git a/spec/outputs/multiline_chain.lua b/spec/outputs/multiline_chain.lua
index ecd2ca8..5edad88 100644
--- a/spec/outputs/multiline_chain.lua
+++ b/spec/outputs/multiline_chain.lua
@@ -21,8 +21,11 @@ local tbb = {
21 } 21 }
22} 22}
23local tb 23local tb
24if f2 ~= nil then 24do
25 tb = f2("abc", f1({ }).a) 25 local _obj_0 = f2
26 if _obj_0 ~= nil then
27 tb = _obj_0("abc", f1({ }).a)
28 end
26end 29end
27local f 30local f
28f = function() 31f = function()
diff --git a/spec/outputs/pipe.lua b/spec/outputs/pipe.lua
index bf517ae..345f8b0 100644
--- a/spec/outputs/pipe.lua
+++ b/spec/outputs/pipe.lua
@@ -32,31 +32,45 @@ do
32end 32end
33do 33do
34 local _with_0 34 local _with_0
35 if create ~= nil then 35 do
36 _with_0 = create(b, "new") 36 local _obj_0 = create
37 if _obj_0 ~= nil then
38 _with_0 = _obj_0(b, "new")
39 end
37 end 40 end
38 _with_0.value = 123 41 _with_0.value = 123
39 print(_with_0:work()) 42 print(_with_0:work())
40end 43end
41if f ~= nil then 44do
42 f(123) 45 local _obj_0 = f
46 if _obj_0 ~= nil then
47 _obj_0(123)
48 end
43end 49end
44if f2 ~= nil then 50do
45 f2((function() 51 local _obj_0 = f2
46 if f1 ~= nil then 52 if _obj_0 ~= nil then
47 return f1("abc") 53 _obj_0((function()
48 end 54 local _obj_1 = f1
49 return nil 55 if _obj_1 ~= nil then
50 end)()) 56 return _obj_1("abc")
57 end
58 return nil
59 end)())
60 end
51end 61end
52local c 62local c
53if f2 ~= nil then 63do
54 c = f2((function() 64 local _obj_0 = f2
55 if f1 ~= nil then 65 if _obj_0 ~= nil then
56 return f1("abc") 66 c = _obj_0((function()
57 end 67 local _obj_1 = f1
58 return nil 68 if _obj_1 ~= nil then
59 end)()) 69 return _obj_1("abc")
70 end
71 return nil
72 end)())
73 end
60end 74end
61local f 75local f
62f = function() 76f = function()
diff --git a/spec/outputs/vararg.lua b/spec/outputs/vararg.lua
index e799a94..052fce3 100644
--- a/spec/outputs/vararg.lua
+++ b/spec/outputs/vararg.lua
@@ -189,14 +189,16 @@ join = function(...)
189 end 189 end
190 end)(...)) 190 end)(...))
191 f_eop((function() 191 f_eop((function()
192 if func ~= nil then 192 local _obj_0 = func
193 return func() 193 if _obj_0 ~= nil then
194 return _obj_0()
194 end 195 end
195 return nil 196 return nil
196 end)()) 197 end)())
197 f_eop((function(...) 198 f_eop((function(...)
198 if func ~= nil then 199 local _obj_0 = func
199 return func(...) 200 if _obj_0 ~= nil then
201 return _obj_0(...)
200 end 202 end
201 return nil 203 return nil
202 end)(...)) 204 end)(...))
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index 6bc9fe8..6c8e167 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.9.10"sv; 63const std::string_view version = "0.9.11"sv;
64const std::string_view extension = "yue"sv; 64const std::string_view extension = "yue"sv;
65 65
66class YueCompilerImpl { 66class YueCompilerImpl {
@@ -3539,7 +3539,19 @@ private:
3539 } 3539 }
3540 BLOCK_END 3540 BLOCK_END
3541 auto objVar = singleVariableFrom(partOne); 3541 auto objVar = singleVariableFrom(partOne);
3542 if (objVar.empty()) { 3542 bool isScoped = false;
3543 if (objVar.empty() || !isLocal(objVar)) {
3544 switch (usage) {
3545 case ExpUsage::Common:
3546 case ExpUsage::Assignment:
3547 isScoped = true;
3548 break;
3549 default: break;
3550 }
3551 if (isScoped) {
3552 temp.push_back(indent() + "do"s + nll(x));
3553 pushScope();
3554 }
3543 objVar = getUnusedName("_obj_"sv); 3555 objVar = getUnusedName("_obj_"sv);
3544 if (auto colonItem = ast_cast<ColonChainItem_t>(partOne->items.back())) { 3556 if (auto colonItem = ast_cast<ColonChainItem_t>(partOne->items.back())) {
3545 auto chainValue = x->new_ptr<ChainValue_t>(); 3557 auto chainValue = x->new_ptr<ChainValue_t>();
@@ -3549,7 +3561,7 @@ private:
3549 if (_withVars.empty()) { 3561 if (_withVars.empty()) {
3550 throw std::logic_error(_info.errorMessage("short dot/colon syntax must be called within a with block"sv, x)); 3562 throw std::logic_error(_info.errorMessage("short dot/colon syntax must be called within a with block"sv, x));
3551 } 3563 }
3552 chainValue->items.push_back(toAst<Callable_t>(_withVars.top(), x)); 3564 chainValue->items.push_back(toAst<Callable_t>(_withVars.top(), x));
3553 } 3565 }
3554 auto newObj = singleVariableFrom(chainValue); 3566 auto newObj = singleVariableFrom(chainValue);
3555 if (!newObj.empty()) { 3567 if (!newObj.empty()) {
@@ -3650,6 +3662,10 @@ private:
3650 default: 3662 default:
3651 break; 3663 break;
3652 } 3664 }
3665 if (isScoped) {
3666 popScope();
3667 temp.push_back(indent() + "end"s + nlr(x));
3668 }
3653 out.push_back(join(temp)); 3669 out.push_back(join(temp));
3654 return true; 3670 return true;
3655 } 3671 }