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:
@@ -1,13 +1,13 @@
|
||||
// Ported from the Python version.
|
||||
|
||||
class Tree {
|
||||
new(item, depth) {
|
||||
this new(item, depth) {
|
||||
_item = item
|
||||
if (depth > 0) {
|
||||
var item2 = item + item
|
||||
depth = depth - 1
|
||||
_left = new Tree(item2 - 1, depth)
|
||||
_right = new Tree(item2, depth)
|
||||
_left = Tree.new(item2 - 1, depth)
|
||||
_right = Tree.new(item2, depth)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,9 +27,9 @@ var stretchDepth = maxDepth + 1
|
||||
var start = IO.clock
|
||||
|
||||
IO.print("stretch tree of depth ", stretchDepth, " check: ",
|
||||
new Tree(0, stretchDepth).check)
|
||||
Tree.new(0, stretchDepth).check)
|
||||
|
||||
var longLivedTree = new Tree(0, maxDepth)
|
||||
var longLivedTree = Tree.new(0, maxDepth)
|
||||
|
||||
// iterations = 2 ** maxDepth
|
||||
var iterations = 1
|
||||
@@ -41,7 +41,7 @@ var depth = minDepth
|
||||
while (depth < stretchDepth) {
|
||||
var check = 0
|
||||
for (i in 1..iterations) {
|
||||
check = check + new Tree(i, depth).check + new Tree(-i, depth).check
|
||||
check = check + Tree.new(i, depth).check + Tree.new(-i, depth).check
|
||||
}
|
||||
|
||||
IO.print((iterations * 2), " trees of depth ", depth, " check: ", check)
|
||||
|
||||
@@ -50,7 +50,7 @@ var ORDERED = null
|
||||
// disrupting current constraints. Strengths cannot be created outside
|
||||
// this class, so == can be used for value comparison.
|
||||
class Strength {
|
||||
new(value, name) {
|
||||
this new(value, name) {
|
||||
_value = value
|
||||
_name = name
|
||||
}
|
||||
@@ -67,13 +67,13 @@ class Strength {
|
||||
}
|
||||
|
||||
// Compile time computed constants.
|
||||
REQUIRED = new Strength(0, "required")
|
||||
STRONG_REFERRED = new Strength(1, "strongPreferred")
|
||||
PREFERRED = new Strength(2, "preferred")
|
||||
STRONG_DEFAULT = new Strength(3, "strongDefault")
|
||||
NORMAL = new Strength(4, "normal")
|
||||
WEAK_DEFAULT = new Strength(5, "weakDefault")
|
||||
WEAKEST = new Strength(6, "weakest")
|
||||
REQUIRED = Strength.new(0, "required")
|
||||
STRONG_REFERRED = Strength.new(1, "strongPreferred")
|
||||
PREFERRED = Strength.new(2, "preferred")
|
||||
STRONG_DEFAULT = Strength.new(3, "strongDefault")
|
||||
NORMAL = Strength.new(4, "normal")
|
||||
WEAK_DEFAULT = Strength.new(5, "weakDefault")
|
||||
WEAKEST = Strength.new(6, "weakest")
|
||||
|
||||
ORDERED = [
|
||||
WEAKEST, WEAK_DEFAULT, NORMAL, STRONG_DEFAULT, PREFERRED, STRONG_REFERRED
|
||||
@@ -82,7 +82,7 @@ ORDERED = [
|
||||
var ThePlanner
|
||||
|
||||
class Constraint {
|
||||
new(strength) {
|
||||
this new(strength) {
|
||||
_strength = strength
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ class Constraint {
|
||||
|
||||
// Abstract superclass for constraints having a single possible output variable.
|
||||
class UnaryConstraint is Constraint {
|
||||
new(myOutput, strength) {
|
||||
this new(myOutput, strength) {
|
||||
super(strength)
|
||||
_satisfied = false
|
||||
_myOutput = myOutput
|
||||
@@ -187,7 +187,7 @@ class UnaryConstraint is Constraint {
|
||||
// change their output during plan execution. This is called "stay
|
||||
// optimization".
|
||||
class StayConstraint is UnaryConstraint {
|
||||
new(variable, strength) {
|
||||
this new(variable, strength) {
|
||||
super(variable, strength)
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ class StayConstraint is UnaryConstraint {
|
||||
// A unary input constraint used to mark a variable that the client
|
||||
// wishes to change.
|
||||
class EditConstraint is UnaryConstraint {
|
||||
EditConstraint(variable, strength) {
|
||||
this new(variable, strength) {
|
||||
super(variable, strength)
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ var BACKWARD = 0
|
||||
// Abstract superclass for constraints having two possible output
|
||||
// variables.
|
||||
class BinaryConstraint is Constraint {
|
||||
new(v1, v2, strength) {
|
||||
this new(v1, v2, strength) {
|
||||
super(strength)
|
||||
_v1 = v1
|
||||
_v2 = v2
|
||||
@@ -328,7 +328,7 @@ class BinaryConstraint is Constraint {
|
||||
// this relationship but the scale factor and offset are considered
|
||||
// read-only.
|
||||
class ScaleConstraint is BinaryConstraint {
|
||||
new(src, scale, offset, dest, strength) {
|
||||
this new(src, scale, offset, dest, strength) {
|
||||
_scale = scale
|
||||
_offset = offset
|
||||
super(src, dest, strength)
|
||||
@@ -376,7 +376,7 @@ class ScaleConstraint is BinaryConstraint {
|
||||
|
||||
// Constrains two variables to have the same value.
|
||||
class EqualityConstraint is BinaryConstraint {
|
||||
new(v1, v2, strength) {
|
||||
this new(v1, v2, strength) {
|
||||
super(v1, v2, strength)
|
||||
}
|
||||
|
||||
@@ -391,7 +391,7 @@ class EqualityConstraint is BinaryConstraint {
|
||||
// various parameters of interest to the DeltaBlue incremental
|
||||
// constraint solver.
|
||||
class Variable {
|
||||
new(name, value) {
|
||||
this new(name, value) {
|
||||
_constraints = []
|
||||
_determinedBy = null
|
||||
_mark = 0
|
||||
@@ -430,7 +430,7 @@ class Variable {
|
||||
// to resatisfy all currently satisfiable constraints in the face of
|
||||
// one or more changing inputs.
|
||||
class Plan {
|
||||
new {
|
||||
this new() {
|
||||
_list = []
|
||||
}
|
||||
|
||||
@@ -448,7 +448,7 @@ class Plan {
|
||||
}
|
||||
|
||||
class Planner {
|
||||
new {
|
||||
this new() {
|
||||
_currentMark = 0
|
||||
}
|
||||
|
||||
@@ -521,7 +521,7 @@ class Planner {
|
||||
// Assume: [sources] are all satisfied.
|
||||
makePlan(sources) {
|
||||
var mark = newMark
|
||||
var plan = new Plan
|
||||
var plan = Plan.new()
|
||||
var todo = sources
|
||||
while (todo.count > 0) {
|
||||
var constraint = todo.removeAt(-1)
|
||||
@@ -622,23 +622,23 @@ var total = 0
|
||||
// constraint so it cannot be accomodated. The cost in this case is,
|
||||
// of course, very low. Typical situations lie somewhere between these
|
||||
// two extremes.
|
||||
var chainTest = new Fn {|n|
|
||||
ThePlanner = new Planner
|
||||
var chainTest = Fn.new {|n|
|
||||
ThePlanner = Planner.new()
|
||||
var prev = null
|
||||
var first = null
|
||||
var last = null
|
||||
|
||||
// Build chain of n equality constraints.
|
||||
for (i in 0..n) {
|
||||
var v = new Variable("v", 0)
|
||||
if (prev != null) new EqualityConstraint(prev, v, REQUIRED)
|
||||
var v = Variable.new("v", 0)
|
||||
if (prev != null) EqualityConstraint.new(prev, v, REQUIRED)
|
||||
if (i == 0) first = v
|
||||
if (i == n) last = v
|
||||
prev = v
|
||||
}
|
||||
|
||||
new StayConstraint(last, STRONG_DEFAULT)
|
||||
var edit = new EditConstraint(first, PREFERRED)
|
||||
StayConstraint.new(last, STRONG_DEFAULT)
|
||||
var edit = EditConstraint.new(first, PREFERRED)
|
||||
var plan = ThePlanner.extractPlanFromConstraints([edit])
|
||||
for (i in 0...100) {
|
||||
first.value = i
|
||||
@@ -647,8 +647,8 @@ var chainTest = new Fn {|n|
|
||||
}
|
||||
}
|
||||
|
||||
var change = new Fn {|v, newValue|
|
||||
var edit = new EditConstraint(v, PREFERRED)
|
||||
var change = Fn.new {|v, newValue|
|
||||
var edit = EditConstraint.new(v, PREFERRED)
|
||||
var plan = ThePlanner.extractPlanFromConstraints([edit])
|
||||
for (i in 0...10) {
|
||||
v.value = newValue
|
||||
@@ -662,20 +662,20 @@ var change = new Fn {|v, newValue|
|
||||
// other by a simple linear transformation (scale and offset). The
|
||||
// time is measured to change a variable on either side of the
|
||||
// mapping and to change the scale and offset factors.
|
||||
var projectionTest = new Fn {|n|
|
||||
ThePlanner = new Planner
|
||||
var scale = new Variable("scale", 10)
|
||||
var offset = new Variable("offset", 1000)
|
||||
var projectionTest = Fn.new {|n|
|
||||
ThePlanner = Planner.new()
|
||||
var scale = Variable.new("scale", 10)
|
||||
var offset = Variable.new("offset", 1000)
|
||||
var src = null
|
||||
var dst = null
|
||||
|
||||
var dests = []
|
||||
for (i in 0...n) {
|
||||
src = new Variable("src", i)
|
||||
dst = new Variable("dst", i)
|
||||
src = Variable.new("src", i)
|
||||
dst = Variable.new("dst", i)
|
||||
dests.add(dst)
|
||||
new StayConstraint(src, NORMAL)
|
||||
new ScaleConstraint(src, scale, offset, dst, REQUIRED)
|
||||
StayConstraint.new(src, NORMAL)
|
||||
ScaleConstraint.new(src, scale, offset, dst, REQUIRED)
|
||||
}
|
||||
|
||||
change.call(src, 17)
|
||||
|
||||
@@ -5,7 +5,7 @@ var sum = 0
|
||||
var start = IO.clock
|
||||
|
||||
for (i in 0...100000) {
|
||||
fibers.add(new Fiber {
|
||||
fibers.add(Fiber.new {
|
||||
sum = sum + i
|
||||
if (i < 99999) fibers[i + 1].call()
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class Toggle {
|
||||
new(startState) {
|
||||
this new(startState) {
|
||||
_state = startState
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ class Toggle {
|
||||
}
|
||||
|
||||
class NthToggle is Toggle {
|
||||
new(startState, maxCounter) {
|
||||
this new(startState, maxCounter) {
|
||||
super(startState)
|
||||
_countMax = maxCounter
|
||||
_count = 0
|
||||
@@ -31,7 +31,7 @@ class NthToggle is Toggle {
|
||||
var start = IO.clock
|
||||
var n = 100000
|
||||
var val = true
|
||||
var toggle = new Toggle(val)
|
||||
var toggle = Toggle.new(val)
|
||||
|
||||
for (i in 0...n) {
|
||||
val = toggle.activate.value
|
||||
@@ -49,7 +49,7 @@ for (i in 0...n) {
|
||||
IO.print(toggle.value)
|
||||
|
||||
val = true
|
||||
var ntoggle = new NthToggle(val, 3)
|
||||
var ntoggle = NthToggle.new(val, 3)
|
||||
|
||||
for (i in 0...n) {
|
||||
val = ntoggle.activate.value
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
Fiber.abort("Error message.")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var fiber
|
||||
|
||||
fiber = new Fiber {
|
||||
fiber = Fiber.new {
|
||||
fiber.call() // expect runtime error: Fiber has already been called.
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
var a
|
||||
var b
|
||||
|
||||
a = new Fiber {
|
||||
a = Fiber.new {
|
||||
b.call() // expect runtime error: Fiber has already been called.
|
||||
}
|
||||
|
||||
b = new Fiber {
|
||||
b = Fiber.new {
|
||||
a.call()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber")
|
||||
return "result"
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("call")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var fiber
|
||||
|
||||
fiber = new Fiber {
|
||||
fiber = Fiber.new {
|
||||
fiber.call(2) // expect runtime error: Fiber has already been called.
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
var a
|
||||
var b
|
||||
|
||||
a = new Fiber {
|
||||
a = Fiber.new {
|
||||
b.call(3) // expect runtime error: Fiber has already been called.
|
||||
}
|
||||
|
||||
b = new Fiber {
|
||||
b = Fiber.new {
|
||||
a.call(2)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("call")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
"s".unknown
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("1")
|
||||
Fiber.yield()
|
||||
IO.print("2")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
"s".unknown
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
var fiber = new Fiber("not fn") // expect runtime error: Argument must be a function.
|
||||
var fiber = Fiber.new("not fn") // expect runtime error: Argument must be a function.
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
var b = new Fiber {
|
||||
var b = Fiber.new {
|
||||
IO.print("fiber b")
|
||||
}
|
||||
|
||||
var a = new Fiber {
|
||||
var a = Fiber.new {
|
||||
IO.print("begin fiber a")
|
||||
b.call()
|
||||
IO.print("end fiber a")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var fiber
|
||||
|
||||
fiber = new Fiber {
|
||||
fiber = Fiber.new {
|
||||
IO.print(1) // expect: 1
|
||||
fiber.run()
|
||||
IO.print(2) // expect: 2
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
var a
|
||||
var b
|
||||
|
||||
a = new Fiber {
|
||||
a = Fiber.new {
|
||||
IO.print(2)
|
||||
b.run()
|
||||
IO.print("nope")
|
||||
}
|
||||
|
||||
b = new Fiber {
|
||||
b = Fiber.new {
|
||||
IO.print(1)
|
||||
a.run()
|
||||
IO.print(3)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
var a = new Fiber {
|
||||
var a = Fiber.new {
|
||||
IO.print("run")
|
||||
}
|
||||
|
||||
// Run a through an intermediate fiber since it will get discarded and we need
|
||||
// to return to the main one after a completes.
|
||||
var b = new Fiber {
|
||||
var b = Fiber.new {
|
||||
a.run()
|
||||
IO.print("nope")
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var fiber
|
||||
|
||||
fiber = new Fiber {
|
||||
fiber = Fiber.new {
|
||||
IO.print(1) // expect: 1
|
||||
fiber.run("ignored")
|
||||
IO.print(2) // expect: 2
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
var a
|
||||
var b
|
||||
|
||||
a = new Fiber {
|
||||
a = Fiber.new {
|
||||
IO.print(2)
|
||||
b.run("ignored")
|
||||
IO.print("nope")
|
||||
}
|
||||
|
||||
b = new Fiber {
|
||||
b = Fiber.new {
|
||||
IO.print(1)
|
||||
a.run("ignored")
|
||||
IO.print(3)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("before")
|
||||
true.unknownMethod
|
||||
IO.print("after")
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var fiber
|
||||
|
||||
fiber = new Fiber {
|
||||
fiber = Fiber.new {
|
||||
fiber.try() // expect runtime error: Fiber has already been called.
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
var a
|
||||
var b
|
||||
|
||||
a = new Fiber {
|
||||
a = Fiber.new {
|
||||
b.try() // expect runtime error: Fiber has already been called.
|
||||
}
|
||||
|
||||
b = new Fiber {
|
||||
b = Fiber.new {
|
||||
a.call()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("try")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {}
|
||||
var fiber = Fiber.new {}
|
||||
IO.print(fiber is Fiber) // expect: true
|
||||
IO.print(fiber is Object) // expect: true
|
||||
IO.print(fiber is Bool) // expect: false
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber 1")
|
||||
Fiber.yield()
|
||||
IO.print("fiber 2")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber 1")
|
||||
var result = Fiber.yield()
|
||||
IO.print(result)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber")
|
||||
var result = Fiber.yield()
|
||||
IO.print(result)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
var a = new Fiber {
|
||||
var a = Fiber.new {
|
||||
IO.print("before") // expect: before
|
||||
Fiber.yield()
|
||||
}
|
||||
|
||||
// Run a chain of fibers. Since none of them are called, they all get discarded
|
||||
// and there is no remaining caller.
|
||||
var b = new Fiber { a.run() }
|
||||
var c = new Fiber { b.run() }
|
||||
var b = Fiber.new { a.run() }
|
||||
var c = Fiber.new { b.run() }
|
||||
c.run()
|
||||
IO.print("not reached")
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {
|
||||
var fiber = Fiber.new {
|
||||
IO.print("fiber 1")
|
||||
Fiber.yield("yield 1")
|
||||
IO.print("fiber 2")
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
var a = new Fiber {
|
||||
var a = Fiber.new {
|
||||
IO.print("before") // expect: before
|
||||
Fiber.yield(1)
|
||||
}
|
||||
|
||||
// Run a chain of fibers. Since none of them are called, they all get discarded
|
||||
// and there is no remaining caller.
|
||||
var b = new Fiber { a.run() }
|
||||
var c = new Fiber { b.run() }
|
||||
var b = Fiber.new { a.run() }
|
||||
var c = Fiber.new { b.run() }
|
||||
c.run()
|
||||
IO.print("not reached")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
IO.print(new Fn {}.arity) // expect: 0
|
||||
IO.print(new Fn {|a| a}.arity) // expect: 1
|
||||
IO.print(new Fn {|a, b| a}.arity) // expect: 2
|
||||
IO.print(new Fn {|a, b, c| a}.arity) // expect: 3
|
||||
IO.print(new Fn {|a, b, c, d| a}.arity) // expect: 4
|
||||
IO.print(Fn.new {}.arity) // expect: 0
|
||||
IO.print(Fn.new {|a| a}.arity) // expect: 1
|
||||
IO.print(Fn.new {|a, b| a}.arity) // expect: 2
|
||||
IO.print(Fn.new {|a, b, c| a}.arity) // expect: 3
|
||||
IO.print(Fn.new {|a, b, c, d| a}.arity) // expect: 4
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
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) }
|
||||
var f0 = Fn.new { IO.print("zero") }
|
||||
var f1 = Fn.new {|a| IO.print("one ", a) }
|
||||
var f2 = Fn.new {|a, b| IO.print("two ", a, " ", b) }
|
||||
var f3 = Fn.new {|a, b, c| IO.print("three ", a, " ", b, " ", c) }
|
||||
|
||||
f0.call("a") // expect: zero
|
||||
f0.call("a", "b") // expect: zero
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
var f2 = new Fn {|a, b| IO.print(a, b) }
|
||||
var f2 = Fn.new {|a, b| IO.print(a, b) }
|
||||
f2.call("a") // expect runtime error: Function expects more arguments.
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
// Not structurally equal.
|
||||
IO.print(new Fn { 123 } == new Fn { 123 }) // expect: false
|
||||
IO.print(new Fn { 123 } != new Fn { 123 }) // expect: true
|
||||
IO.print(Fn.new { 123 } == Fn.new { 123 }) // expect: false
|
||||
IO.print(Fn.new { 123 } != Fn.new { 123 }) // expect: true
|
||||
|
||||
// Not equal to other types.
|
||||
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
|
||||
IO.print(Fn.new { 123 } == 1) // expect: false
|
||||
IO.print(Fn.new { 123 } == false) // expect: false
|
||||
IO.print(Fn.new { 123 } == "fn 123") // expect: false
|
||||
IO.print(Fn.new { 123 } != 1) // expect: true
|
||||
IO.print(Fn.new { 123 } != false) // expect: true
|
||||
IO.print(Fn.new { 123 } != "fn 123") // expect: true
|
||||
|
||||
// Equal by identity.
|
||||
var f = new Fn { 123 }
|
||||
var f = Fn.new { 123 }
|
||||
IO.print(f == f) // expect: true
|
||||
IO.print(f != f) // expect: false
|
||||
|
||||
@@ -1 +1 @@
|
||||
new Fn(3) // expect runtime error: Argument must be a function.
|
||||
Fn.new(3) // expect runtime error: Argument must be a function.
|
||||
|
||||
@@ -1 +1 @@
|
||||
IO.print(new Fn {}) // expect: <fn>
|
||||
IO.print(Fn.new {}) // expect: <fn>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
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
|
||||
IO.print(Fn.new { 0 } is Fn) // expect: true
|
||||
IO.print(Fn.new { 0 } is Object) // expect: true
|
||||
IO.print(Fn.new { 0 } is String) // expect: false
|
||||
IO.print(Fn.new { 0 }.type == Fn) // expect: true
|
||||
|
||||
@@ -18,6 +18,6 @@ class Foo {
|
||||
toString { "Foo.toString" }
|
||||
}
|
||||
|
||||
IO.print([1, new Foo, 2].join(", ")) // expect: 1, Foo.toString, 2
|
||||
IO.print([1, Foo.new(), 2].join(", ")) // expect: 1, Foo.toString, 2
|
||||
|
||||
// TODO: Handle lists that contain themselves.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var list = new List
|
||||
var list = List.new()
|
||||
|
||||
IO.print(list.count) // expect: 0
|
||||
IO.print(list) // expect: []
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
var a = [1, 4, 2, 1, 5]
|
||||
var b = ["W", "o", "r", "l", "d"]
|
||||
var max = new Fn {|a, b| a > b ? a : b }
|
||||
var sum = new Fn {|a, b| a + b }
|
||||
var max = Fn.new {|a, b| a > b ? a : b }
|
||||
var sum = Fn.new {|a, b| a + b }
|
||||
|
||||
IO.print(a.reduce(max)) // expect: 5
|
||||
IO.print(a.reduce(10, max)) // expect: 10
|
||||
|
||||
@@ -12,6 +12,6 @@ class Foo {
|
||||
toString { "Foo.toString" }
|
||||
}
|
||||
|
||||
IO.print([1, new Foo, 2]) // expect: [1, Foo.toString, 2]
|
||||
IO.print([1, Foo.new(), 2]) // expect: [1, Foo.toString, 2]
|
||||
|
||||
// TODO: Handle lists that contain themselves.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var fiber = new Fiber {}
|
||||
var fiber = Fiber.new {}
|
||||
|
||||
var map = {
|
||||
null: "null value",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var map = new Map
|
||||
var map = Map.new()
|
||||
|
||||
IO.print(map.count) // expect: 0
|
||||
IO.print(map) // expect: {}
|
||||
|
||||
@@ -12,7 +12,7 @@ class Foo {
|
||||
toString { "Foo.toString" }
|
||||
}
|
||||
|
||||
IO.print({1: new Foo}) // expect: {1: Foo.toString}
|
||||
IO.print({1: Foo.new()}) // expect: {1: Foo.toString}
|
||||
|
||||
// Since iteration order is unspecified, we don't know what order the results
|
||||
// will be.
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
class Foo {}
|
||||
IO.print(!(new Foo)) // expect: false
|
||||
IO.print(!Foo.new()) // expect: false
|
||||
|
||||
@@ -26,15 +26,15 @@ IO.print(Object.same(Bool, Bool)) // expect: true
|
||||
// Other types compare by identity.
|
||||
class Foo {}
|
||||
|
||||
var foo = new Foo
|
||||
var foo = Foo.new()
|
||||
IO.print(Object.same(foo, foo)) // expect: true
|
||||
IO.print(Object.same(foo, new Foo)) // expect: false
|
||||
IO.print(Object.same(foo, Foo.new())) // expect: false
|
||||
|
||||
// Ignores == operators.
|
||||
class Bar {
|
||||
==(other) { true }
|
||||
}
|
||||
|
||||
var bar = new Bar
|
||||
var bar = Bar.new()
|
||||
IO.print(Object.same(bar, bar)) // expect: true
|
||||
IO.print(Object.same(bar, new Bar)) // expect: false
|
||||
IO.print(Object.same(bar, Bar.new())) // expect: false
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
class Foo {}
|
||||
IO.print((new Foo).toString == "instance of Foo") // expect: true
|
||||
IO.print(Foo.new().toString == "instance of Foo") // expect: true
|
||||
|
||||
@@ -8,4 +8,4 @@ class TestSequence is Sequence {
|
||||
iteratorValue(iterator) { iterator }
|
||||
}
|
||||
|
||||
IO.print((new TestSequence).count) // expect: 10
|
||||
IO.print(TestSequence.new().count) // expect: 10
|
||||
|
||||
@@ -7,4 +7,4 @@ class InfiniteSequence is Sequence {
|
||||
}
|
||||
|
||||
// Should not try to iterate the whole sequence.
|
||||
IO.print((new InfiniteSequence).isEmpty) // expect: false
|
||||
IO.print(InfiniteSequence.new().isEmpty) // expect: false
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Infinite iterator demonstrating that Sequence.map is not eager
|
||||
class FibIterator {
|
||||
new {
|
||||
this new() {
|
||||
_current = 0
|
||||
_next = 1
|
||||
}
|
||||
@@ -16,7 +16,7 @@ class FibIterator {
|
||||
|
||||
class Fib is Sequence {
|
||||
iterate(iterator) {
|
||||
if (iterator == null) return new FibIterator
|
||||
if (iterator == null) return FibIterator.new()
|
||||
iterator.iterate
|
||||
return iterator
|
||||
}
|
||||
@@ -24,7 +24,7 @@ class Fib is Sequence {
|
||||
iteratorValue(iterator) { iterator.value }
|
||||
}
|
||||
|
||||
var squareFib = (new Fib).map {|fib| fib * fib }
|
||||
var squareFib = Fib.new().map {|fib| fib * fib }
|
||||
var iterator = null
|
||||
|
||||
IO.print(squareFib is Sequence) // expect: true
|
||||
|
||||
@@ -8,4 +8,4 @@ class TestSequence is Sequence {
|
||||
iteratorValue(iterator) { iterator }
|
||||
}
|
||||
|
||||
IO.print((new TestSequence).toList) // expect: [1, 2, 3]
|
||||
IO.print(TestSequence.new().toList) // expect: [1, 2, 3]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Infinite iterator demonstrating that Sequence.where is not eager
|
||||
class FibIterator {
|
||||
new {
|
||||
this new() {
|
||||
_current = 0
|
||||
_next = 1
|
||||
}
|
||||
@@ -16,7 +16,7 @@ class FibIterator {
|
||||
|
||||
class Fib is Sequence {
|
||||
iterate(iterator) {
|
||||
if (iterator == null) return new FibIterator
|
||||
if (iterator == null) return FibIterator.new()
|
||||
iterator.iterate
|
||||
return iterator
|
||||
}
|
||||
@@ -24,7 +24,7 @@ class Fib is Sequence {
|
||||
iteratorValue(iterator) { iterator.value }
|
||||
}
|
||||
|
||||
var largeFibs = (new Fib).where {|fib| fib > 100 }
|
||||
var largeFibs = Fib.new().where {|fib| fib > 100 }
|
||||
var iterator = null
|
||||
|
||||
IO.print(largeFibs is Sequence) // expect: true
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ class Foo {
|
||||
}
|
||||
|
||||
// Calls toString on argument.
|
||||
IO.print(new Foo) // expect: Foo.toString
|
||||
IO.print(Foo.new()) // expect: Foo.toString
|
||||
|
||||
// With one argument, returns the argument.
|
||||
IO.print(IO.print(1) == 1) // expect: 1
|
||||
|
||||
@@ -2,4 +2,4 @@ class BadToString {
|
||||
toString { 3 }
|
||||
}
|
||||
|
||||
IO.print(new BadToString) // expect: [invalid toString]
|
||||
IO.print(BadToString.new()) // expect: [invalid toString]
|
||||
|
||||
@@ -2,5 +2,5 @@ class BadToString {
|
||||
toString { 3 }
|
||||
}
|
||||
|
||||
IO.write(new BadToString) // expect: [invalid toString]
|
||||
IO.write(BadToString.new()) // expect: [invalid toString]
|
||||
IO.print
|
||||
@@ -1,4 +1,4 @@
|
||||
new Fn {
|
||||
Fn.new {
|
||||
var a = "before"
|
||||
IO.print(a) // expect: before
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
var f
|
||||
for (i in [1, 2, 3]) {
|
||||
var j = 4
|
||||
f = new Fn { IO.print(i + j) }
|
||||
f = Fn.new { IO.print(i + j) }
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
var f
|
||||
while (true) {
|
||||
var i = "i"
|
||||
f = new Fn { IO.print(i) }
|
||||
f = Fn.new { IO.print(i) }
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var done = false
|
||||
while (!done) {
|
||||
new Fn {
|
||||
Fn.new {
|
||||
break // expect error
|
||||
}
|
||||
done = true
|
||||
|
||||
@@ -12,4 +12,4 @@ class foo {
|
||||
}
|
||||
|
||||
foo.callFoo // expect: static foo method
|
||||
(new foo).callFoo // expect: instance foo method
|
||||
foo.new().callFoo // expect: instance foo method
|
||||
|
||||
@@ -11,4 +11,4 @@ class Foo {
|
||||
}
|
||||
|
||||
Foo.sayName // expect: Foo!
|
||||
(new Foo).sayName // expect: Foo!
|
||||
Foo.new().sayName // expect: Foo!
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class Foo {}
|
||||
|
||||
var foo = new Foo
|
||||
var foo = Foo.new()
|
||||
IO.print(foo is Foo) // expect: true
|
||||
|
||||
// TODO: Test precedence and grammar of what follows "new".
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -15,7 +15,7 @@ IO.print(true || false ? 1 : 2) // expect: 1
|
||||
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(Foo.new() ? 1 : 2) // expect: 1
|
||||
|
||||
var a = 0
|
||||
IO.print(a = 3 ? 1 : 2) // expect: 1
|
||||
@@ -32,7 +32,7 @@ IO.print(true ? 1 || false : 2) // expect: 1
|
||||
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
|
||||
IO.print(true ? Foo.new() : 2) // expect: instance of Foo
|
||||
|
||||
IO.print(true ? a = 5 : 2) // expect: 5
|
||||
IO.print(a) // expect: 5
|
||||
@@ -48,7 +48,7 @@ IO.print(false ? 1 : 2 || false) // expect: 2
|
||||
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 : Foo.new()) // expect: instance of Foo
|
||||
|
||||
// Associativity.
|
||||
IO.print(true ? 2 : true ? 4 : 5) // expect: 2
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
this +(value) { // expect error
|
||||
IO.print("ok")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
this -(value) { // expect error
|
||||
IO.print("ok")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
this name=(value) { // expect error
|
||||
IO.print("ok")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class Foo {
|
||||
static this new() {} // expect error
|
||||
this static new() {} // expect error
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
this [value] { // expect error
|
||||
IO.print("ok")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
this ! { // expect error
|
||||
IO.print("ok")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
this new() {
|
||||
IO.print("ok")
|
||||
}
|
||||
}
|
||||
|
||||
var foo = Foo.new() // expect: ok
|
||||
foo.new() // expect runtime error: Foo does not implement 'new()'.
|
||||
@@ -2,7 +2,7 @@ class Foo {
|
||||
toString { "Foo" }
|
||||
}
|
||||
|
||||
// Classes inherit the argument-less "new" one by default.
|
||||
var foo = new Foo
|
||||
// Classes get an argument-less "new()" by default.
|
||||
var foo = Foo.new()
|
||||
IO.print(foo is Foo) // expect: true
|
||||
IO.print(foo.toString) // expect: Foo
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class Foo {
|
||||
this new() {
|
||||
IO.print("Foo.new()")
|
||||
}
|
||||
}
|
||||
|
||||
class Bar is Foo {}
|
||||
|
||||
Bar.new() // expect: Foo.new()
|
||||
@@ -2,6 +2,8 @@ class Foo {
|
||||
+(other) { "Foo " + other }
|
||||
}
|
||||
|
||||
IO.print(new Foo + "value") // expect: Foo value
|
||||
IO.print(Foo.new() + "value") // expect: Foo value
|
||||
|
||||
// TODO: Other expressions following a constructor, like new Foo.bar("arg").
|
||||
// TODO: Delete this test?
|
||||
// TODO: Other constructor tests, like named constructors, etc.
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// TODO: Change this.
|
||||
|
||||
class Foo {
|
||||
this named() { _field = "named" }
|
||||
this other() { _field = "other" }
|
||||
|
||||
toString { _field }
|
||||
}
|
||||
|
||||
IO.print(Foo.named()) // expect: named
|
||||
IO.print(Foo.other()) // expect: other
|
||||
|
||||
// Returns the new instance.
|
||||
var foo = Foo.named()
|
||||
IO.print(foo is Foo) // expect: true
|
||||
IO.print(foo.toString) // expect: named
|
||||
@@ -1,19 +0,0 @@
|
||||
class Foo {
|
||||
new { IO.print("none") }
|
||||
new() { IO.print("zero") }
|
||||
new(a) { IO.print(a) }
|
||||
new(a, b) { IO.print(a + b) }
|
||||
|
||||
toString { "Foo" }
|
||||
}
|
||||
|
||||
// Can overload by arity.
|
||||
new Foo // expect: none
|
||||
new Foo() // expect: zero
|
||||
new Foo("one") // expect: one
|
||||
new Foo("one", "two") // expect: onetwo
|
||||
|
||||
// Returns the new instance.
|
||||
var foo = new Foo // expect: none
|
||||
IO.print(foo is Foo) // expect: true
|
||||
IO.print(foo.toString) // expect: Foo
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
this real() {}
|
||||
}
|
||||
|
||||
// Classes do not get an argument-less "new()" if they define a constructor.
|
||||
var foo = Foo.new() // expect runtime error: Foo metaclass does not implement 'new()'.
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
this new { // expect error
|
||||
IO.print("ok")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
this base() {}
|
||||
}
|
||||
|
||||
class Bar is Foo {}
|
||||
|
||||
Bar.base() // expect runtime error: Bar metaclass does not implement 'base()'.
|
||||
@@ -0,0 +1,13 @@
|
||||
// Tests that Object implements new(). The only way to call that is through a
|
||||
// super() call in a subclass, so this does that.
|
||||
|
||||
class Foo {
|
||||
this new() {
|
||||
super() // Should not cause a no method error.
|
||||
IO.print("ok")
|
||||
}
|
||||
}
|
||||
|
||||
Foo.new() // expect: ok
|
||||
|
||||
// TODO: Test that can't invoke initializer on existing instance.
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user