Get closures working!

In the process, I had to change the grammar. There is now a strong
separation between statements and expressions. The code was just wrong
before when it popped locals at the end of a block scope because there
could be temporaries on the stack if the block was in expression
position. This fixes that.

Still need to implement closing over `this`.
This commit is contained in:
Bob Nystrom
2013-12-04 07:43:50 -08:00
parent c14b115c02
commit 157944aa27
32 changed files with 1063 additions and 416 deletions
+17 -17
View File
@@ -1,21 +1,21 @@
class Foo {
method { 0 }
method(a) { a }
method(a, b) { a + b }
method(a, b, c) { a + b + c }
method(a, b, c, d) { a + b + c + d }
method(a, b, c, d, e) { a + b + c + d + e }
method(a, b, c, d, e, f) { a + b + c + d + e + f }
method(a, b, c, d, e, f, g) { a + b + c + d + e + f + g }
method(a, b, c, d, e, f, g, h) { a + b + c + d + e + f + g + h }
method(a, b, c, d, e, f, g, h, i) { a + b + c + d + e + f + g + h + i }
method(a, b, c, d, e, f, g, h, i, j) { a + b + c + d + e + f + g + h + i + j }
method(a, b, c, d, e, f, g, h, i, j, k) { a + b + c + d + e + f + g + h + i + j + k}
method(a, b, c, d, e, f, g, h, i, j, k, l) { a + b + c + d + e + f + g + h + i + j + k + l}
method(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}
method(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}
method(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}
method(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}
method { return 0 }
method(a) { return a }
method(a, b) { return a + b }
method(a, b, c) { return a + b + c }
method(a, b, c, d) { return a + b + c + d }
method(a, b, c, d, e) { return a + b + c + d + e }
method(a, b, c, d, e, f) { return a + b + c + d + e + f }
method(a, b, c, d, e, f, g) { return a + b + c + d + e + f + g }
method(a, b, c, d, e, f, g, h) { return a + b + c + d + e + f + g + h }
method(a, b, c, d, e, f, g, h, i) { return a + b + c + d + e + f + g + h + i }
method(a, b, c, d, e, f, g, h, i, j) { return a + b + c + d + e + f + g + h + i + j }
method(a, b, c, d, e, f, g, h, i, j, k) { return a + b + c + d + e + f + g + h + i + j + k}
method(a, b, c, d, e, f, g, h, i, j, k, l) { return a + b + c + d + e + f + g + h + i + j + k + l}
method(a, b, c, d, e, f, g, h, i, j, k, l, m) { return a + b + c + d + e + f + g + h + i + j + k + l + m}
method(a, b, c, d, e, f, g, h, i, j, k, l, m, n) { return a + b + c + d + e + f + g + h + i + j + k + l + m + n}
method(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) { return a + b + c + d + e + f + g + h + i + j + k + l + m + n + o}
method(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) { return a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p}
}
var foo = Foo.new
+13 -13
View File
@@ -1,18 +1,18 @@
class Foo {
+ other { "infix + " + other }
- other { "infix - " + other }
* other { "infix * " + other }
/ other { "infix / " + other }
% other { "infix % " + other }
< other { "infix < " + other }
> other { "infix > " + other }
<= other { "infix <= " + other }
>= other { "infix >= " + other }
== other { "infix == " + other }
!= other { "infix != " + other }
+ other { return "infix + " + other }
- other { return "infix - " + other }
* other { return "infix * " + other }
/ other { return "infix / " + other }
% other { return "infix % " + other }
< other { return "infix < " + other }
> other { return "infix > " + other }
<= other { return "infix <= " + other }
>= other { return "infix >= " + other }
== other { return "infix == " + other }
!= other { return "infix != " + other }
! { "prefix !" }
- { "prefix -" }
! { return "prefix !" }
- { return "prefix -" }
}
var foo = Foo.new
+4 -4
View File
@@ -1,9 +1,9 @@
class Foo {
bar { "on instance" }
static bar { "on metaclass" }
bar { return "on instance" }
static bar { return "on metaclass" }
bar(arg) { "on instance " + arg }
static bar(arg) { "on metaclass " + arg }
bar(arg) { return "on instance " + arg }
static bar(arg) { return "on metaclass " + arg }
}
io.write("on metaclass " + "arg") // expect: on metaclass arg