Quantcast
Channel: [In]effective Theory
Viewing all articles
Browse latest Browse all 53

How Little Things Change: a Pascal-to-Javascript Converter

$
0
0

I've just "finished" writing a pascal-to-javascript converter, with the intent of using it to port 400,000 lines of physics simulations (from CUPS) over to browsers and therefore mobile devices. Current issues:

  • It only does syntactic conversion, so anybody using it would have to write any external pascal functions used.
  • It doesn't handle types quite correctly, which may necessitate some hand-hacking of the final project.
  • No pretty-printing is done on the result, so it would be a good idea to run the closure compiler on the result. (I use closure --formatting PRETTY_PRINT.)

Other than the above, it seems to work (at least on the 3000-line sample of pascal code I've been testing it on). It also seems to be adequately fast, converting 3000 in a bit less than 50 milliseconds.

What amazed me about this was how obscenely easy it was. Pascal is a pre-C language designed in 1968 - JavaScript first appeared in 1995. Pascal is compiled, statically typed, and general purpose - JavaScript is interpreted, untyped (to a nearly obscene extent), and until a couple years ago, was exclusively reserved for in-browser applications. The two should vary wildly.

They don't.

Admittedly, I was doing it the easy way - converting from Pascal to JavaScript would be nearly impossible without writing a JavaScript interpreter, thanks to JavaScript's anything-goes "type system". But once you look past the type system and the syntax, what's left feels extraordinarily similar. Let's look at the most common constructs used in an average JavaScript program.

  • Assignment
  • Function calls
  • Conditionals: if-then and if-then-else
  • Loops: while and do/while
  • Objects with properties.

Now isn't that strange: Pascal has all of these! Going the other way, Pascal adds one construct: WITH. Usage looks like this:

a.b := 5;
WITH a DO BEGIN
  DoSomething(b);
END;

As it turns out, JavaScript has this too - its use is discouraged (since it mangles scope), but it's there, even if it's never used.

a.b = 5;
with (a) {
  doSomething(b);
}

Everything anybody does under ordinary with almost any commonly-used language can be done - with equal ease and comparable (if not identical) syntax - in any other commonly-used language. In 40 years, the nature of programming really hasn't changed that much. This isn't just some coincidental similarity between JavaScript and Pascal - it should be almost as easy to write converters to C and Java (which I intend to do shortly).

Naturally, this is not the first time this observation has been made - but I suspect many programmers out there don't grasp how similar most languages really are to each other. Let me put it this way: I don't know Pascal. The correspondence between Pascal and JavaScript is so tight that I wrote a converter from one to the other in a matter of days, without knowing the former nor particularly liking the latter.

Because they're the same.

Depending what colors you fly, this is either an indication that the old ways of programming are good enough, or that we still have a long way to go before reaching an "ideal" state. I tend to side with the latter point of view (being a Haskell fan and whatnot), but I'll say this for the former: of the many procedural/object-oriented languages out there, no one is better than any other (excepting the occasional freak, like COBOL or APL). C++ really doesn't provide any major advantage over C, nor Java over Algol. Some of them provide slightly better tools for different tasks (FORTRAN's continued popularity in the scientific community being a prime example), but to say X is better than Y because it provides nice abstractions, or is more elegant, is, for the most part, complete rot. Yes, JavaScript provides many very powerful abstractions. But few programmers actually use them - most seem to avoid them like the plague, preferring to essentially write Java or C code.


Viewing all articles
Browse latest Browse all 53

Latest Images

Trending Articles





Latest Images