package main
func main(){
a := 1
f := func(){
b := a // freevalue
println(b)
a = 2 // side-effect
}
f()
println(a) // 2
}
查找一个外部作用域中的变量
package: main library
@init
type: () ->
entry-0:
extern type:
main
type: () -> null
entry-0:
<any> t28 = undefined-println
<null> t26 = call <() -> null> AnonymousFunc-2 () binding[<number> 1] member[]
<number> t27 = side-effect <number> 2 [a] by <null> t26
<any> t29 = call <any> t28 (<number> t27) binding[] member[]
extern type:
AnonymousFunc-2
freeValue: a:(20)a, println:(21)println
sideEffects: a
type: () -> null
entry-0:
jump -> b-1
b-1: <- entry-0
<any> t22 = call <any> println (<number> a) binding[] member[]
jump -> b-2
b-2: <- b-1
extern type:
package main
func main(){
a := 1
a = 2
println(a) // 2
}
package main
func main() {
a := 1
f1 := func() {
a = 2
}
f2 := func() {
f1() // f2继承f1的side-effect
}
f2()
println(a) // side-effect(a,2)
}
package main
func main() {
a := 1 // f1将绑定a,绑定值由闭包定义的位置决定与调用位置无关
f1 := func() {
a = 2
}
{
a := 3
f1()
println(a) // 3
}
println(a) // side-effect(a,2)
}
package: main library
@init
type: () ->
entry-0:
extern type:
main
type: () -> null
entry-0:
<null> t36 = call <() -> null> AnonymousFunc-3 () binding[<number> 1, <() -> null> AnonymousFunc-2] member[]
<any> t37 = side-effect <number> 2 [a] by <null> t36
<number, error> t39 = call <func(...interface {}) (int, error)> println (<any> t37) binding[] member[]
extern type:
AnonymousFunc-2
freeValue: a:(21)a
sideEffects: a
type: () -> null
entry-0:
jump -> b-1
b-1: <- entry-0
jump -> b-2
b-2: <- b-1
extern type:
AnonymousFunc-3
freeValue: f1:(29)f1, a:(32)a
sideEffects: a // 继承自AnonymousFunc-2
type: () -> null
entry-0:
jump -> b-1
b-1: <- entry-0
<null> t30 = call <() -> null> f1 () binding[<number> a] member[]
<any> t33 = side-effect <number> 2 [a] by <null> t30
jump -> b-2
b-2: <- b-1
extern type:
error:
package: main library
@init
type: () ->
entry-0:
extern type:
main
sideEffects: a
type: () -> null
entry-0:
jump -> b-1
b-1: <- entry-0
<null> t27 = call <() -> null> AnonymousFunc-2 () binding[<number> 3] member[]
<any> t28 = side-effect <number> 2 [a] by <null> t27 // 生成side-effect但暂时不会使用
<number, error> t30 = call <func(...interface {}) (int, error)> println (<number> 3) binding[] member[]
jump -> b-2
b-2: <- b-1
<number, error> t33 = call <func(...interface {}) (int, error)> println (<any> t28) binding[] member[]
extern type:
AnonymousFunc-2
freeValue: a:(21)a
sideEffects: a
type: () -> null
entry-0:
jump -> b-1
b-1: <- entry-0
jump -> b-2
b-2: <- b-1
extern type:
error:
由于 side-effect 不在当前作用域中,因此第一个 println 查找到常量'3',第二个 println 属于 entry-0 的子作用域,可以查找到 side-effect
package main
func main(){
a := 0
f := func() {
if true {
a = 2
}else{
}
println(a) // phi(freevalue,2)
}
a = 1
f()
println(a) // phi(1,2)
a = 2
f()
println(a) // phi(2,2)
}
在这个样例中的 f 可能生成 side-effect 也可能不生成,因此应该生成一个 phi(freevalue,2)。而 freevalue 只是一个占位符,它将在该闭包被调用时替换为绑定变量在当前作用域中的值。
目前该功能正在实现中,敬请期待后续版本。
END
YAK官方资源
Yak 语言官方教程:
https://yaklang.com/docs/intro/
Yakit 视频教程:
https://space.bilibili.com/437503777
Github下载地址:
https://github.com/yaklang/yakit
Yakit官网下载地址:
https://yaklang.com/
Yakit安装文档:
https://yaklang.com/products/download_and_install
Yakit使用文档:
https://yaklang.com/products/intro/
常见问题速查:
https://yaklang.com/products/FAQ