Programming languages

MathScript

MathScript is a toy programming language designed to look fashionable while having a uniform syntax with few rules to remember. I made it as a reaction to modern programming languages that tend towards ever higher complexity, making it harder for beginners to get started.

On the surface, MathScript looks like a highly simplified Javascript. That's deliberate. Right now it also compiles to Javascript, but it's just a starting point. I can easily imagine other implementations.

For now let's take a look at the language:

// Make an infinite counter with closures.
function makeCounter() {
    var i = -1;
    function next() {
        i += 1;
        return i;
    }
    return next;
}
var count = makeCounter();
print(count());
print(count());
print(count());

A few highlights:

(Ideally, reading past array boundaries should be an error, but MathScript doesn't have error handling yet. Such a feature has to be designed with care to ensure portability.)

Download

October 2015 release (27K)

As MathScript is just an academic curiosity, I never picked a license.

The files:

Roadmap

  1. Create a browser-based wrapper with a nice user interface API.
  2. Write some real scripts to see what works, what doesn't and what's missing.
  3. Ideally, write a utility to wrap a compiled script and a set of built-ins into a self-contained web page.

Differences from Javascript

The most popular programming languages nowadays use the same general syntax as C for expressions and flow control statements. That's good for familiarity, but comes with a problem: C-style syntax is awful. Operators like ++ or ?: can easily make a mess of anyone's code, and don't even get me started on using = for assignment, instead of equality like any layman would rightly expect. As for the overly powerful for instruction, how often do you need its full power? I bet it's much more common to get burned by it instead. Flexibility also means being able to jump off a bridge without meaning to...

The upside from this is: after some thinking, it occurred to me that a nice clean subset of the Javascript syntax can be recognized by a recursive descent parser. And it can! The entire prototype compiler is less than 400 lines of Javascript. It remains to be seen whether this results in a language that's too verbose. But Python isn't, while Java is, for example. If anything, pure C-like syntax seems to make things worse!

So far, key differences are:

Oh my, the list has grown long. Amazing how many compile-time checks you can perform without compromising the language's dynamic nature. You can't prevent all errors, of course, and you shouldn't try; but covering the basics is the least you can do.

Other things aren't set in stone, and may change once I get around to writing better regular expressions, or who knows, maybe a hand-rolled lexer.

Last but not least: no, there are no objects in MathScript. It seems hard to remember nowadays, but we can in fact live without them. Not every language should be suitable for developing giant-scale apps. Sometimes you really do need just scripts.