refactor: replace Foo.new constructor syntax with new Foo and simplify superclass constructor calls
Remove the `this new` constructor declaration syntax in favor of a `new` keyword that creates instances directly. Superclass constructors are now invoked via bare `super(args)` instead of `super.new(args)`, eliminating the semantic weirdness of calling a constructor on a partially-constructed instance. Update the compiler to treat `new` as a keyword token, replace the `isMethod` flag with `methodName`/`methodLength` fields, and change the VM's `NEW` opcode to pop the class from the stack before creating the instance. Add a default `new` native method on Object that returns `this`. Update all benchmarks and tests to use the new syntax.
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
// Ported from the Python version.
|
||||
|
||||
class Tree {
|
||||
this new(item, depth) {
|
||||
new(item, depth) {
|
||||
_item = item
|
||||
if (depth > 0) {
|
||||
var item2 = item + item
|
||||
depth = depth - 1
|
||||
_left = Tree.new(item2 - 1, depth)
|
||||
_right = Tree.new(item2, depth)
|
||||
_left = new Tree(item2 - 1, depth)
|
||||
_right = new Tree(item2, depth)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,9 +27,9 @@ var stretchDepth = maxDepth + 1
|
||||
var start = OS.clock
|
||||
|
||||
io.write("stretch tree of depth " + stretchDepth.toString + " check: " +
|
||||
Tree.new(0, stretchDepth).check.toString)
|
||||
new Tree(0, stretchDepth).check.toString)
|
||||
|
||||
var longLivedTree = Tree.new(0, maxDepth)
|
||||
var longLivedTree = new Tree(0, maxDepth)
|
||||
|
||||
// iterations = 2 ** maxDepth
|
||||
var iterations = 1
|
||||
@@ -44,7 +44,7 @@ while (depth < stretchDepth) {
|
||||
var check = 0
|
||||
var i = 1
|
||||
while (i < iterations + 1) {
|
||||
check = check + Tree.new(i, depth).check + Tree.new(-i, depth).check
|
||||
check = check + new Tree(i, depth).check + new Tree(-i, depth).check
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class Toggle {
|
||||
this new(startState) {
|
||||
new(startState) {
|
||||
_state = startState
|
||||
}
|
||||
|
||||
@@ -11,7 +11,8 @@ class Toggle {
|
||||
}
|
||||
|
||||
class NthToggle is Toggle {
|
||||
this new(startState, maxCounter) super.new(startState) {
|
||||
new(startState, maxCounter) {
|
||||
super(startState)
|
||||
_countMax = maxCounter
|
||||
_count = 0
|
||||
}
|
||||
@@ -31,7 +32,7 @@ var start = OS.clock
|
||||
var n = 1000000
|
||||
var i = 0
|
||||
var val = true
|
||||
var toggle = Toggle.new(val)
|
||||
var toggle = new Toggle(val)
|
||||
|
||||
while (i < n) {
|
||||
val = toggle.activate.value
|
||||
@@ -41,7 +42,7 @@ while (i < n) {
|
||||
io.write(toggle.value)
|
||||
|
||||
val = true
|
||||
var ntoggle = NthToggle.new(val, 3)
|
||||
var ntoggle = new NthToggle(val, 3)
|
||||
|
||||
i = 0
|
||||
while (i < n) {
|
||||
|
||||
Reference in New Issue
Block a user