DAVE'S LIFE ON HOLD

Self in Javascript Progress

In the past two days, I've started down two different approaches to representing Self objects in Javascript, and I'm settling into a rather interesting representation:

function ()  return arguments.callee.eval.apply(arguments.callee,arguments);

If you look at that for about 2 moments, it may not make sense until you look at the definition of "eval":

Function.prototype.eval = function(selector) <br>        return this[selector].apply(this,arguments.after(0));<br>

What this means is that eval does a dynamic message send by looking up the property of the function represented by the string "selector", and then invokes that method using the remainder of the arguments as the parameters to the method. To define a method, I'm currently leaning towards a simple routine:

Function.prototype.does = function(selector,definition) <br>        this[selector] = definition.compile();<br>        return this;<br>

Which simply compiles the definition for the given selector and adds it to the function object itself. What this means is that we can invoke a method by doing something like:

object("selector", ....)

Which will invoke in turn the method defined as:

object.does("selector", "| console.log('hello world')");

Now I've ditched the syntax for most function definitions and have started using something that looks a lot more like Self:

' x y | return x + y '

Which the parameters are before the | and the string after the | is the body of the function which we can construct using:

String.prototype.compile = function() return Function.constructor.apply(Function,parts.shift().split(/\\s/).concat(parts.join("|")).filter(function(x) { return x != "" " alt="
var parts = this.split("|");
return Function.constructor.apply(Function,parts.shift().split(/\\s/).concat(parts.join("|")).filter(function(x) { return x != "" " />))
}

And at this point, we can start writing some pretty awesome looking javascript! I've got a utility function named _ which takes an optional global name to which to attach an object:

_('foo')

Will create an object and name it 'foo' in the global namespace so we can refer to it as foo. If we were to write:

_('foo').does('add:', "x | this('value:', x + this('value')); return this('value')").slot("value",10);

we would create an object with a method 'add: x' and a slot 'value <- 10'. We could evaluate it as:

foo("add:",4); // -> 14
foo("add:",6); // -> 20
foo("value"); // -> 20

At this point some bells should be going off as:

foo add: 4.
foo add: 6.
foo value.

Is the equivalent Self. It is going to take a little more work to finish the translator, but a considerable amount of the language can be built using these idioms.