Make constructors just methods.

* Eliminate "new" reserved word.
* Allow "this" before a method definition to define a constructor.
* Only create a default constructor for classes that don't define one.
This commit is contained in:
Bob Nystrom
2015-07-10 09:18:22 -07:00
parent 0ddaa2517c
commit 5fb6186d7d
221 changed files with 864 additions and 654 deletions
+2 -2
View File
@@ -3,13 +3,13 @@ var g = null
{
var local = "local"
f = new Fn {
f = Fn.new {
IO.print(local)
local = "after f"
IO.print(local)
}
g = new Fn {
g = Fn.new {
IO.print(local)
local = "after g"
IO.print(local)
@@ -1,7 +1,7 @@
var f = null
new Fn {|param|
f = new Fn {
Fn.new {|param|
f = Fn.new {
IO.print(param)
}
}.call("param")
@@ -3,10 +3,10 @@
// would crash because it walked to the end of the upvalue list (correct), but
// then didn't handle not finding the variable.
new Fn {
Fn.new {
var a = "a"
var b = "b"
new Fn {
Fn.new {
IO.print(b) // expect: b
IO.print(a) // expect: a
}.call()
@@ -2,11 +2,11 @@ var F = null
class Foo {
method(param) {
F = new Fn {
F = Fn.new {
IO.print(param)
}
}
}
(new Foo).method("param")
Foo.new().method("param")
F.call() // expect: param
@@ -2,7 +2,7 @@ var f = null
{
var local = "local"
f = new Fn {
f = Fn.new {
IO.print(local)
}
}
@@ -1,3 +1,4 @@
// TODO: Is this right? Shouldn't it resolve to this.local?
var foo = null
{
@@ -8,7 +9,7 @@ var foo = null
}
}
foo = new Foo
foo = Foo.new()
}
foo.method // expect: local
+4 -4
View File
@@ -1,12 +1,12 @@
var f = null
new Fn {
Fn.new {
var a = "a"
new Fn {
Fn.new {
var b = "b"
new Fn {
Fn.new {
var c = "c"
f = new Fn {
f = Fn.new {
IO.print(a)
IO.print(b)
IO.print(c)
@@ -1,6 +1,6 @@
{
var local = "local"
new Fn {
Fn.new {
IO.print(local) // expect: local
}.call()
}
@@ -1,3 +1,4 @@
// TODO: Is this right? Shouldn't it resolve to this.local?
{
var local = "local"
class Foo {
@@ -6,5 +7,5 @@
}
}
(new Foo).method // expect: local
Foo.new().method // expect: local
}
@@ -2,7 +2,7 @@ var f = null
{
var a = "a"
f = new Fn {
f = Fn.new {
IO.print(a)
IO.print(a)
}
@@ -3,7 +3,7 @@
{
var a = "a"
f = new Fn { IO.print(a) }
f = Fn.new { IO.print(a) }
}
{
@@ -1,6 +1,6 @@
{
var foo = "closure"
new Fn {
Fn.new {
{
IO.print(foo) // expect: closure
var foo = "shadow"