Skip to content

Commit fced0c4

Browse files
committed
Added import global syntax.
1 parent 4177d23 commit fced0c4

11 files changed

Lines changed: 527 additions & 140 deletions

File tree

doc/docs/doc/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,43 @@ tb =
928928

929929
The import statement is a syntax sugar for requiring a module or help extracting items from an imported module. The imported items are const by default.
930930

931+
#### Import Global
932+
933+
You can place `import global` at the top of a block to automatically import all names that have not been explicitly declared or assigned in the current scope as globals. These implicit imports are treated as local consts that reference the corresponding globals at the position of the statement.
934+
935+
Names that are explicitly declared as globals in the same scope will not be imported, so you can safely assign to them.
936+
937+
```moonscript
938+
do
939+
import global
940+
print "hello"
941+
math.random 3
942+
-- print = nil -- error: imported globals are const
943+
944+
do
945+
-- explicit global variable will not be imported
946+
import global
947+
global FLAG
948+
print FLAG
949+
FLAG = 123
950+
```
951+
<YueDisplay>
952+
<pre>
953+
do
954+
import global
955+
print "hello"
956+
math.random 3
957+
-- print = nil -- error: imported globals are const
958+
959+
do
960+
-- explicit global variable will not be imported
961+
import global
962+
global FLAG
963+
print FLAG
964+
FLAG = 123
965+
</pre>
966+
</YueDisplay>
967+
931968
```moonscript
932969
-- used as table destructuring
933970
do

doc/docs/zh/doc/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,43 @@ tb =
926926

927927
导入语句是一个语法糖,用于需要引入一个模块或者从已导入的模块中提取子项目。从模块导入的变量默认为不可修改的常量。
928928

929+
#### 导入全局变量
930+
931+
在代码块顶部写 `import global`,会将当前作用域中尚未显式声明或赋值过的变量名,自动导入为本地常量,并在该语句的位置绑定到同名的全局变量。
932+
933+
但是在同一作用域中被显式声明为全局的变量不会被自动导入,因此可以继续进行赋值操作。
934+
935+
```moonscript
936+
do
937+
import global
938+
print "hello"
939+
math.random 3
940+
-- print = nil -- 报错:自动导入的全局变量为常量
941+
942+
do
943+
-- 被显式声明为全局的变量不会被自动导入
944+
import global
945+
global FLAG
946+
print FLAG
947+
FLAG = 123
948+
```
949+
<YueDisplay>
950+
<pre>
951+
do
952+
import global
953+
print "hello"
954+
math.random 3
955+
-- print = nil -- 报错:自动导入的全局变量是常量
956+
957+
do
958+
-- 被显式声明为全局的变量不会被自动导入
959+
import global
960+
global FLAG
961+
print FLAG
962+
FLAG = 123
963+
</pre>
964+
</YueDisplay>
965+
929966
```moonscript
930967
-- 用作表解构
931968
do

spec/inputs/import_global.yue

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
do
3+
import global
4+
print "hello"
5+
math.random 10
6+
7+
do
8+
import global
9+
value = 1
10+
value += 2
11+
print value
12+
13+
do
14+
local print = (msg) ->
15+
return msg
16+
do
17+
import global
18+
print "local"
19+
math.random 1
20+
21+
do
22+
import global
23+
local tostring = (v) -> "local"
24+
tostring "value"
25+
print tostring 123
26+
27+
do
28+
func = (x, y) ->
29+
import global
30+
return type x, tostring y, print
31+
func 1, 2
32+
33+
do
34+
import global
35+
try
36+
func "hello #{world}"
37+
catch err
38+
print err
39+
40+
do
41+
import global
42+
global FLAG
43+
print FLAG
44+
FLAG = 123
45+
46+
do
47+
import global
48+
global Foo = 10
49+
print Foo
50+
Foo += 2
51+
52+
do
53+
import global
54+
global Bar, Baz
55+
Bar = 1
56+
Baz = 2
57+
print Bar, Baz
58+
59+
do
60+
import global
61+
global *
62+
x = 3434
63+
if y then
64+
x = 10
65+
66+
do
67+
import global
68+
global ^
69+
foobar = "all #{lowercase}"
70+
FooBar = "pascal case"
71+
FOOBAR = "all #{Uppercase}"
72+
73+
do
74+
import global
75+
global const class A
76+
global const Flag = 1
77+
global const const, x, y = "const", 1, 2
78+
global const math, table
79+
print math, table
80+
81+
do
82+
import global
83+
with X
84+
\func 1, 2, 3
85+
.tag = "abc"
86+

spec/outputs/codes_from_doc.lua

Lines changed: 82 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,17 @@ local tb = {
408408
}
409409
}
410410
}
411+
do
412+
local math = math
413+
local print = print
414+
print("hello")
415+
math.random(3)
416+
end
417+
do
418+
local print = print
419+
print(FLAG)
420+
FLAG = 123
421+
end
411422
do
412423
local insert, concat = table.insert, table.concat
413424
local C, Ct, Cmt
@@ -737,36 +748,6 @@ end
737748
local first = select(1, ...)
738749
return print(ok, count, first)
739750
end)(fn(true))
740-
local f
741-
f = function(...)
742-
local t = {
743-
n = select("#", ...),
744-
...
745-
}
746-
print("argument count:", t.n)
747-
print("table length:", #t)
748-
for i = 1, t.n do
749-
print(t[i])
750-
end
751-
end
752-
f(1, 2, 3)
753-
f("a", "b", "c", "d")
754-
f()
755-
local process
756-
process = function(...)
757-
local args = {
758-
n = select("#", ...),
759-
...
760-
}
761-
local sum = 0
762-
for i = 1, args.n do
763-
if args[i] ~= nil and type(args[i]) == "number" then
764-
sum = sum + args[i]
765-
end
766-
end
767-
return sum
768-
end
769-
process(1, nil, 3, nil, 5)
770751
Rx.Observable.fromRange(1, 8):filter(function(x)
771752
return x % 2 == 0
772753
end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
@@ -1000,6 +981,36 @@ local arg1 = {
1000981
a = 0
1001982
}
1002983
f2(arg1, arg2)
984+
local f
985+
f = function(...)
986+
local t = {
987+
n = select("#", ...),
988+
...
989+
}
990+
print("argument count:", t.n)
991+
print("table length:", #t)
992+
for i = 1, t.n do
993+
print(t[i])
994+
end
995+
end
996+
f(1, 2, 3)
997+
f("a", "b", "c", "d")
998+
f()
999+
local process
1000+
process = function(...)
1001+
local args = {
1002+
n = select("#", ...),
1003+
...
1004+
}
1005+
local sum = 0
1006+
for i = 1, args.n do
1007+
if args[i] ~= nil and type(args[i]) == "number" then
1008+
sum = sum + args[i]
1009+
end
1010+
end
1011+
return sum
1012+
end
1013+
process(1, nil, 3, nil, 5)
10031014
f(function()
10041015
return print("hello")
10051016
end)
@@ -2916,6 +2927,17 @@ local tb = {
29162927
}
29172928
}
29182929
}
2930+
do
2931+
local math = math
2932+
local print = print
2933+
print("hello")
2934+
math.random(3)
2935+
end
2936+
do
2937+
local print = print
2938+
print(FLAG)
2939+
FLAG = 123
2940+
end
29192941
do
29202942
local insert, concat = table.insert, table.concat
29212943
local C, Ct, Cmt
@@ -3245,36 +3267,6 @@ end
32453267
local first = select(1, ...)
32463268
return print(ok, count, first)
32473269
end)(fn(true))
3248-
local f
3249-
f = function(...)
3250-
local t = {
3251-
n = select("#", ...),
3252-
...
3253-
}
3254-
print("argument count:", t.n)
3255-
print("table length:", #t)
3256-
for i = 1, t.n do
3257-
print(t[i])
3258-
end
3259-
end
3260-
f(1, 2, 3)
3261-
f("a", "b", "c", "d")
3262-
f()
3263-
local process
3264-
process = function(...)
3265-
local args = {
3266-
n = select("#", ...),
3267-
...
3268-
}
3269-
local sum = 0
3270-
for i = 1, args.n do
3271-
if args[i] ~= nil and type(args[i]) == "number" then
3272-
sum = sum + args[i]
3273-
end
3274-
end
3275-
return sum
3276-
end
3277-
process(1, nil, 3, nil, 5)
32783270
Rx.Observable.fromRange(1, 8):filter(function(x)
32793271
return x % 2 == 0
32803272
end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
@@ -3538,6 +3530,36 @@ findFirstEven = function(list)
35383530
end
35393531
return nil
35403532
end
3533+
local f
3534+
f = function(...)
3535+
local t = {
3536+
n = select("#", ...),
3537+
...
3538+
}
3539+
print("argument count:", t.n)
3540+
print("table length:", #t)
3541+
for i = 1, t.n do
3542+
print(t[i])
3543+
end
3544+
end
3545+
f(1, 2, 3)
3546+
f("a", "b", "c", "d")
3547+
f()
3548+
local process
3549+
process = function(...)
3550+
local args = {
3551+
n = select("#", ...),
3552+
...
3553+
}
3554+
local sum = 0
3555+
for i = 1, args.n do
3556+
if args[i] ~= nil and type(args[i]) == "number" then
3557+
sum = sum + args[i]
3558+
end
3559+
end
3560+
return sum
3561+
end
3562+
process(1, nil, 3, nil, 5)
35413563
f(function()
35423564
return print("hello")
35433565
end)

0 commit comments

Comments
 (0)