When Python Becomes Javascript

My most recent checkin note:

* Disable python scoping (curly brace and semicolon time)
* Support for // comments
* Keyword “func” -> “function”
* for loops now require parens
* Created yapArrayInject()
* built-in calls to init(), count(), and get() all pass the object as first param

A few “minor” things make for quite a large shift in the look and feel of the language. It now feels a lot like Javascript, only instead of using “this”, it passes the object as the first parameter ala Python, but only when you specifically request it like Lua (using colon). It ends up looking something like:

// ----------------------------------------------------------
// Creating the range() iterator

var range = object();
range.init = function(self, start, end)
{
    self.start = start;
    self.end = end;
    return self;
};

range.count = function(self)
{
    return 1 + (self.end - self.start);
};

range.get = function(self, i)
{
    return self.start + i;
};

// ----------------------------------------------------------
// Iterator usage

print("Range:");
for(v in range(1, 10))
{
    print(" %d" % (v));
}
print("\n");

// ----------------------------------------------------------
// Array iteration

var a = array();
push(a, 5);
push(a, 6);
push(a, 7);
push(a, 8);
print("Array:");
for(w in a)
{
    print(" %d" % (w));
}
print("\n");

// ----------------------------------------------------------
// Member Function Execution

var obj = range(1, 100);
print("Object count: %d\n" % ( obj:count() ));

It looks wildly different!

I’m not in love with it yet. The interpreter currently requires that if you implement init() for an object, it has to return itself. I thought it’d be neat to allow for generator objects, but in practice it is going to seem like a boilerplate nuisance. I used to have a “magical” workaround, but boilerplate might be better than magic. I also want to implement some intrinsic iterators that allow for things like ipairs() and/or members() for objects.

p.s. I had to switch the syntax highlighter from “python” to “javascript” for it to colorize it well.

Posted in Blog