feat: migrate all function literals from fn syntax to new Fn block syntax

Replace all occurrences of the old `fn` function literal syntax with the new `new Fn {|params| body}` block syntax across benchmark, test, and source files. Update the Wren compiler to handle the pipe operator as a block argument delimiter by removing `TOKEN_PIPE` from the newline-swallowing token list and adding explicit newline matching in `infixOp`. Adjust test expectations in `conditional/precedence.wren` to remove `fn`-based expressions and update `test/assignment/local.wren` to use the new syntax.
This commit is contained in:
Bob Nystrom
2014-04-03 02:41:53 +00:00
parent 81f20caf33
commit e20da2f070
61 changed files with 124 additions and 127 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
fn {
new Fn {
var a = "before"
IO.print(a) // expect: before
+1 -1
View File
@@ -1,7 +1,7 @@
var f
for (i in [1, 2, 3]) {
var j = 4
f = fn IO.print(i + j)
f = new Fn { IO.print(i + j) }
break
}
+1 -1
View File
@@ -1,7 +1,7 @@
var f
while (true) {
var i = "i"
f = fn IO.print(i)
f = new Fn { IO.print(i) }
break
}
+1 -1
View File
@@ -1,6 +1,6 @@
var done = false
while (!done) {
fn {
new Fn {
break // expect error
}
done = true
+2 -2
View File
@@ -3,13 +3,13 @@ var g = null
{
var local = "local"
f = fn {
f = new Fn {
IO.print(local)
local = "after f"
IO.print(local)
}
g = fn {
g = new Fn {
IO.print(local)
local = "after g"
IO.print(local)
@@ -1,7 +1,7 @@
var f = null
fn(param) {
f = fn {
new Fn {|param|
f = new Fn {
IO.print(param)
}
}.call("param")
@@ -2,7 +2,7 @@ var f = null
class Foo {
method(param) {
f = fn {
f = new Fn {
IO.print(param)
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ var f = null
{
var local = "local"
f = fn {
f = new Fn {
IO.print(local)
}
}
+4 -4
View File
@@ -1,12 +1,12 @@
var f = null
fn {
new Fn {
var a = "a"
fn {
new Fn {
var b = "b"
fn {
new Fn {
var c = "c"
f = fn {
f = new Fn {
IO.print(a)
IO.print(b)
IO.print(c)
+1 -1
View File
@@ -1,6 +1,6 @@
{
var local = "local"
fn {
new Fn {
IO.print(local) // expect: local
}.call
}
@@ -2,7 +2,7 @@ var f = null
{
var a = "a"
f = fn {
f = new Fn {
IO.print(a)
IO.print(a)
}
+1 -1
View File
@@ -3,7 +3,7 @@
{
var a = "a"
f = fn IO.print(a)
f = new Fn { IO.print(a) }
}
{
+1 -1
View File
@@ -1,6 +1,6 @@
var global = "global"
// TODO: Forward reference to global declared after use.
fn {
new Fn {
IO.print(global) // expect: global
}.call
-4
View File
@@ -16,7 +16,6 @@ IO.print(!false ? 1 : 2) // expect: 1
IO.print(~0 ? 1 : 2) // expect: 1
IO.print(3 is Num ? 1 : 2) // expect: 1
IO.print(new Foo ? 1 : 2) // expect: 1
IO.print(fn true ? 1 : 2) // expect: <fn>
var a = 0
IO.print(a = 3 ? 1 : 2) // expect: 1
@@ -34,8 +33,6 @@ IO.print(true ? !true : 2) // expect: false
IO.print(true ? ~0 : 2) // expect: 4294967295
IO.print(true ? 3 is Bool : 2) // expect: false
IO.print(true ? new Foo : 2) // expect: instance of Foo
// TODO: Is this what we want?
IO.print(true ? fn 1 : 2) // expect: <fn>
IO.print(true ? a = 5 : 2) // expect: 5
IO.print(a) // expect: 5
@@ -52,7 +49,6 @@ IO.print(false ? 1 : !false) // expect: true
IO.print(false ? 1 : ~0) // expect: 4294967295
IO.print(false ? 1 : 3 is Num) // expect: true
IO.print(false ? 1 : new Foo) // expect: instance of Foo
IO.print(false ? 1 : fn 2) // expect: <fn>
// Associativity.
IO.print(true ? 2 : true ? 4 : 5) // expect: 2
+3 -3
View File
@@ -3,16 +3,16 @@ var closure
{
var a = "before"
fiber = Fiber.create(fn {
fiber = Fiber.create {
IO.print(a)
Fiber.yield
a = "after"
Fiber.yield
IO.print(a)
a = "final"
})
}
closure = fn {
closure = new Fn {
IO.print(a)
}
}
+2 -2
View File
@@ -2,11 +2,11 @@ class Foo {
new { _field = "Foo field" }
closeOverGet {
return fn { return _field }
return new Fn { _field }
}
closeOverSet {
return fn { _field = "new value" }
return new Fn { _field = "new value" }
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
class Foo {
static bar {
fn _field = "wat" // expect error
new Fn { _field = "wat" } // expect error
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
var list = []
for (i in [1, 2, 3]) {
list.add(fn IO.print(i))
list.add(new Fn { IO.print(i) })
}
for (f in list) f.call
+1 -1
View File
@@ -2,7 +2,7 @@ var list = []
for (i in [1, 2, 3]) {
var j = i + 1
list.add(fn IO.print(j))
list.add(new Fn { IO.print(j) })
}
for (f in list) f.call
+1 -1
View File
@@ -1,4 +1,4 @@
var f = fn {
var f = new Fn {
IO.print("evaluate sequence")
return [1, 2, 3]
}
+2 -2
View File
@@ -1,6 +1,6 @@
var f = fn {
var f = new Fn {
for (i in [1, 2, 3]) {
return fn IO.print(i)
return new Fn { IO.print(i) }
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
var f = fn {
var f = new Fn {
for (i in [1, 2, 3]) {
return i
}
+4 -4
View File
@@ -1,7 +1,7 @@
var f0 = fn IO.print("zero")
var f1 = fn(a) IO.print("one " + a)
var f2 = fn(a, b) IO.print("two " + a + " " + b)
var f3 = fn(a, b, c) IO.print("three " + a + " " + b + " " + c)
var f0 = new Fn { IO.print("zero") }
var f1 = new Fn {|a| IO.print("one " + a) }
var f2 = new Fn {|a, b| IO.print("two " + a + " " + b) }
var f3 = new Fn {|a, b, c| IO.print("three " + a + " " + b + " " + c) }
f0.call("a") // expect: zero
f0.call("a", "b") // expect: zero
+1 -1
View File
@@ -1,2 +1,2 @@
var f2 = fn(a, b) IO.print(a + b)
var f2 = new Fn {|a, b| IO.print(a + b) }
f2.call("a") // expect runtime error: Function expects more arguments.
+1 -1
View File
@@ -1,2 +1,2 @@
var f = fn {}
var f = new Fn {}
IO.print(f.call) // expect: null
+9 -9
View File
@@ -1,16 +1,16 @@
// Not structurally equal.
IO.print((fn 123) == (fn 123)) // expect: false
IO.print((fn 123) != (fn 123)) // expect: true
IO.print(new Fn { 123 } == new Fn { 123 }) // expect: false
IO.print(new Fn { 123 } != new Fn { 123 }) // expect: true
// Not equal to other types.
IO.print((fn 123) == 1) // expect: false
IO.print((fn 123) == false) // expect: false
IO.print((fn 123) == "fn 123") // expect: false
IO.print((fn 123) != 1) // expect: true
IO.print((fn 123) != false) // expect: true
IO.print((fn 123) != "fn 123") // expect: true
IO.print(new Fn { 123 } == 1) // expect: false
IO.print(new Fn { 123 } == false) // expect: false
IO.print(new Fn { 123 } == "fn 123") // expect: false
IO.print(new Fn { 123 } != 1) // expect: true
IO.print(new Fn { 123 } != false) // expect: true
IO.print(new Fn { 123 } != "fn 123") // expect: true
// Equal by identity.
var f = fn 123
var f = new Fn { 123 }
IO.print(f == f) // expect: true
IO.print(f != f) // expect: false
+17 -17
View File
@@ -1,50 +1,50 @@
var f0 = fn 0
var f0 = new Fn { 0 }
IO.print(f0.call) // expect: 0
var f1 = fn(a) a
var f1 = new Fn {|a| a }
IO.print(f1.call(1)) // expect: 1
var f2 = fn(a, b) a + b
var f2 = new Fn {|a, b| a + b }
IO.print(f2.call(1, 2)) // expect: 3
var f3 = fn(a, b, c) a + b + c
var f3 = new Fn {|a, b, c| a + b + c }
IO.print(f3.call(1, 2, 3)) // expect: 6
var f4 = fn(a, b, c, d) a + b + c + d
var f4 = new Fn {|a, b, c, d| a + b + c + d }
IO.print(f4.call(1, 2, 3, 4)) // expect: 10
var f5 = fn(a, b, c, d, e) a + b + c + d + e
var f5 = new Fn {|a, b, c, d, e| a + b + c + d + e }
IO.print(f5.call(1, 2, 3, 4, 5)) // expect: 15
var f6 = fn(a, b, c, d, e, f) a + b + c + d + e + f
var f6 = new Fn {|a, b, c, d, e, f| a + b + c + d + e + f }
IO.print(f6.call(1, 2, 3, 4, 5, 6)) // expect: 21
var f7 = fn(a, b, c, d, e, f, g) a + b + c + d + e + f + g
var f7 = new Fn {|a, b, c, d, e, f, g| a + b + c + d + e + f + g }
IO.print(f7.call(1, 2, 3, 4, 5, 6, 7)) // expect: 28
var f8 = fn(a, b, c, d, e, f, g, h) a + b + c + d + e + f + g + h
var f8 = new Fn {|a, b, c, d, e, f, g, h| a + b + c + d + e + f + g + h }
IO.print(f8.call(1, 2, 3, 4, 5, 6, 7, 8)) // expect: 36
var f9 = fn(a, b, c, d, e, f, g, h, i) a + b + c + d + e + f + g + h + i
var f9 = new Fn {|a, b, c, d, e, f, g, h, i| a + b + c + d + e + f + g + h + i }
IO.print(f9.call(1, 2, 3, 4, 5, 6, 7, 8, 9)) // expect: 45
var f10 = fn(a, b, c, d, e, f, g, h, i, j) a + b + c + d + e + f + g + h + i + j
var f10 = new Fn {|a, b, c, d, e, f, g, h, i, j| a + b + c + d + e + f + g + h + i + j }
IO.print(f10.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) // expect: 55
var f11 = fn(a, b, c, d, e, f, g, h, i, j, k) a + b + c + d + e + f + g + h + i + j + k
var f11 = new Fn {|a, b, c, d, e, f, g, h, i, j, k| a + b + c + d + e + f + g + h + i + j + k }
IO.print(f11.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)) // expect: 66
var f12 = fn(a, b, c, d, e, f, g, h, i, j, k, l) a + b + c + d + e + f + g + h + i + j + k + l
var f12 = new Fn {|a, b, c, d, e, f, g, h, i, j, k, l| a + b + c + d + e + f + g + h + i + j + k + l }
IO.print(f12.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)) // expect: 78
var f13 = fn(a, b, c, d, e, f, g, h, i, j, k, l, m) a + b + c + d + e + f + g + h + i + j + k + l + m
var f13 = new Fn {|a, b, c, d, e, f, g, h, i, j, k, l, m| a + b + c + d + e + f + g + h + i + j + k + l + m }
IO.print(f13.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)) // expect: 91
var f14 = fn(a, b, c, d, e, f, g, h, i, j, k, l, m, n) a + b + c + d + e + f + g + h + i + j + k + l + m + n
var f14 = new Fn {|a, b, c, d, e, f, g, h, i, j, k, l, m, n| a + b + c + d + e + f + g + h + i + j + k + l + m + n }
IO.print(f14.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)) // expect: 105
var f15 = fn(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) a + b + c + d + e + f + g + h + i + j + k + l + m + n + o
var f15 = new Fn {|a, b, c, d, e, f, g, h, i, j, k, l, m, n, o| a + b + c + d + e + f + g + h + i + j + k + l + m + n + o }
IO.print(f15.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)) // expect: 120
var f16 = fn(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p
var f16 = new Fn {|a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p| a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p }
IO.print(f16.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)) // expect: 136
+5 -7
View File
@@ -1,25 +1,23 @@
// Single expression body.
(fn IO.print("ok")).call // expect: ok
// TODO: Precedence of fn body.
new Fn { IO.print("ok") }.call // expect: ok
// Curly body.
fn {
new Fn {
IO.print("ok") // expect: ok
}.call
// No trailing newline.
fn {
new Fn {
IO.print("ok") }.call // expect: ok
// Multiple expressions.
fn {
new Fn {
IO.print("1") // expect: 1
IO.print("2") // expect: 2
}.call
// Extra newlines.
fn {
new Fn {
IO.print("1") // expect: 1
+1 -1
View File
@@ -1 +1 @@
IO.print(fn {}) // expect: <fn>
IO.print(new Fn {}) // expect: <fn>
+1 -1
View File
@@ -1 +1 @@
var f = fn(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q) { "!" } // expect error
var f = new Fn {|a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q| "!" } // expect error
+4 -4
View File
@@ -1,4 +1,4 @@
IO.print((fn 0) is Fn) // expect: true
IO.print((fn 0) is Object) // expect: true
IO.print((fn 0) is String) // expect: false
IO.print((fn 0).type == Fn) // expect: true
IO.print(new Fn { 0 } is Fn) // expect: true
IO.print(new Fn { 0 } is Object) // expect: true
IO.print(new Fn { 0 } is String) // expect: false
IO.print(new Fn { 0 }.type == Fn) // expect: true
@@ -2,11 +2,11 @@ class Foo {
new { _field = "Foo field" }
closeOverFooGet {
return fn { return fn { return _field } }
return new Fn { new Fn { _field } }
}
closeOverFooSet {
return fn { return fn { _field = "new foo value" } }
return new Fn { new Fn { _field = "new foo value" } }
}
}
@@ -17,11 +17,11 @@ class Bar is Foo {
}
closeOverBarGet {
return fn { return fn { return _field } }
return new Fn { new Fn { _field } }
}
closeOverBarSet {
return fn { return fn { _field = "new bar value" } }
return new Fn { new Fn { _field = "new bar value" } }
}
}
+4 -4
View File
@@ -1,6 +1,6 @@
IO.print(Num is Class) // expect: true
IO.print(true is Bool) // expect: true
IO.print((fn 1) is Fn) // expect: true
IO.print(new Fn { 1 } is Fn) // expect: true
IO.print(123 is Num) // expect: true
IO.print(null is Null) // expect: true
IO.print("s" is String) // expect: true
@@ -8,7 +8,7 @@ IO.print("s" is String) // expect: true
IO.print(Num is Bool) // expect: false
IO.print(null is Class) // expect: false
IO.print(true is Fn) // expect: false
IO.print((fn 1) is Num) // expect: false
IO.print(new Fn { 1 } is Num) // expect: false
IO.print("s" is Null) // expect: false
IO.print(123 is String) // expect: false
@@ -16,7 +16,7 @@ IO.print(123 is String) // expect: false
IO.print(Num is Object) // expect: true
IO.print(null is Object) // expect: true
IO.print(true is Object) // expect: true
IO.print((fn 1) is Object) // expect: true
IO.print(new Fn { 1 } is Object) // expect: true
IO.print("s" is Object) // expect: true
IO.print(123 is Object) // expect: true
@@ -24,7 +24,7 @@ IO.print(123 is Object) // expect: true
IO.print(Num is Class) // expect: true
IO.print(null is Class) // expect: false
IO.print(true is Class) // expect: false
IO.print((fn 1) is Class) // expect: false
IO.print(new Fn { 1 } is Class) // expect: false
IO.print("s" is Class) // expect: false
IO.print(123 is Class) // expect: false
+1 -1
View File
@@ -1,4 +1,4 @@
fn {
new Fn {
IO.print(1) // expect: 1
IO.print(2) // expect: 2
IO.print(3) // expect: 3
+1 -1
View File
@@ -1,4 +1,4 @@
var f = fn {
var f = new Fn {
1; 2; 3; 4; 5; 6; 7; 8;
9; 10; 11; 12; 13; 14; 15; 16;
17; 18; 19; 20; 21; 22; 23; 24;
+1 -1
View File
@@ -1,3 +1,3 @@
var a = [1, 2, 3]
var b = a.map(fn (x) x + 1)
var b = a.map {|x| x + 1 }
IO.print(b) // expect: [2, 3, 4]
+2 -2
View File
@@ -1,6 +1,6 @@
var a = [1, 2, 3]
var b = a.where(fn (x) x > 1)
var b = a.where {|x| x > 1 }
IO.print(b) // expect: [2, 3]
var c = a.where(fn (x) x > 10)
var c = a.where {|x| x > 10 }
IO.print(c) // expect: []
+1 -1
View File
@@ -1,3 +1,3 @@
var a = 1..3
var b = a.map(fn (x) x + 1)
var b = a.map {|x| x + 1 }
IO.print(b) // expect: [2, 3, 4]
+2 -2
View File
@@ -1,6 +1,6 @@
var a = 1..3
var b = a.where(fn (x) x > 1)
var b = a.where {|x| x > 1 }
IO.print(b) // expect: [2, 3]
var c = a.where(fn (x) x > 10)
var c = a.where {|x| x > 10 }
IO.print(c) // expect: []
+1 -1
View File
@@ -1,3 +1,3 @@
IO.print(fn {
IO.print(new Fn {
if (false) "no" else return "ok"
}.call) // expect: ok
+1 -1
View File
@@ -1,3 +1,3 @@
IO.print(fn {
IO.print(new Fn {
if (true) return "ok"
}.call) // expect: ok
+1 -1
View File
@@ -1,3 +1,3 @@
IO.print(fn {
IO.print(new Fn {
while (true) return "ok"
}.call) // expect: ok
+1 -1
View File
@@ -1,4 +1,4 @@
var f = fn {
var f = new Fn {
return "ok"
IO.print("bad")
}
+1 -1
View File
@@ -1,4 +1,4 @@
var f = fn {
var f = new Fn {
if (true) { return }
IO.print("bad")
}
+1 -1
View File
@@ -1,4 +1,4 @@
var f = fn {
var f = new Fn {
return
IO.print("bad")
}
+2 -2
View File
@@ -2,11 +2,11 @@ class Foo {
static initialize { __field = "Foo field" }
static closeOverGet {
return fn { return __field }
return new Fn { __field }
}
static closeOverSet {
return fn { __field = "new value" }
return new Fn { __field = "new value" }
}
}
+1 -3
View File
@@ -4,9 +4,7 @@ class Base {
class Derived is Base {
getClosure {
return fn {
return super.toString
}
return new Fn { super.toString }
}
toString { return "Derived" }
+1 -1
View File
@@ -1,3 +1,3 @@
fn {
new Fn {
super.foo // expect error
}
+1 -3
View File
@@ -1,8 +1,6 @@
class Foo {
getClosure {
return fn {
return toString
}
return new Fn { toString }
}
toString { return "Foo" }
+1 -1
View File
@@ -2,7 +2,7 @@ class Outer {
method {
IO.print(this) // expect: Outer
fn {
new Fn {
IO.print(this) // expect: Outer
class Inner {
+3 -3
View File
@@ -1,8 +1,8 @@
class Foo {
getClosure {
return fn {
return fn {
return fn {
return new Fn {
return new Fn {
return new Fn {
return toString
}
}
+1 -1
View File
@@ -1,3 +1,3 @@
fn {
new Fn {
this // expect error
}
@@ -1,3 +1,3 @@
fn(a) {
new Fn {|a|
var a = "oops" // expect error
}
+1 -1
View File
@@ -1,3 +1,3 @@
fn {
new Fn {
IO.print(notDefined) // expect error
}.call
+1 -1
View File
@@ -3,7 +3,7 @@ var list = []
var i = 1
while (i < 4) {
var j = i + 1
list.add(fn IO.print(j))
list.add(new Fn { IO.print(j) })
i = i + 1
}
+2 -2
View File
@@ -1,7 +1,7 @@
var f = fn {
var f = new Fn {
while (true) {
var i = "i"
return fn IO.print(i)
return new Fn { IO.print(i) }
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
var f = fn {
var f = new Fn {
while (true) {
var i = "i"
return i