top | item 6572349

Show HN: My Turbo Pascal compiler in JavaScript

101 points| lkesteloot | 12 years ago |teamten.com | reply

81 comments

order
[+] ret|12 years ago|reply
Your project is great, Pascal is really underrated language.

I looked at parser (hand written are best), this part spotted my eyes https://github.com/lkesteloot/turbopascal/blob/master/Parser... - IMHO order of checking whatever node is identifier or not have to be reversed. Now, such simple program:

program Test;

procedure foo; begin end;

begin foo := 10; end.

produces error 'Error: can't cast from 70 to 76 ("10", line 8)' - whatever this means. :)

[+] lkesteloot|12 years ago|reply
Well the error message is confusing (it's trying to cast from an integer (type 70) to a procedure (type 76)), but it's correct to fail to compile. But yeah I'm not sure that my logic there is altogether correct. I re-wrote it a few times as I found new cases that weren't correctly handled. Specifically, I'm not sure whether the type of the identifier should guide the parsing. It's similar to the "x*y" ambiguity in parsing C (multiplication or declaration of pointer variable y).
[+] pjmlp|12 years ago|reply
Very nice indeed.

It was a pity that C took over the PC world as it did, Turbo Pascal could do everything better than it, including being safe by default, modules, OO.

Why Borland, why!

[+] kabdib|12 years ago|reply
I was at Apple when they were transitioning from Pascal (which had driven much of the Lisa and Macintosh) to C (and some C++).

With the exception of a few curmudgeons, C was the way to go for most people. You lost nested procedures, but the stuff you got back was immense. Pascal's big problem was that the extensions required to make it a "real" systems programming language (arbitrary memory access, I/O that didn't suck, etc.) were not standardized; good luck porting anything. Pascal strings were effectively broken (with a proliferation of Str255 / Str64 / Str32 types that were effectively incompatible unless you cheated).

C, while it still lacked a standard, had everything you needed out of the box, and the direction for a standard was clear.

Pascal became a legacy language and died at Apple in the early 90s.

[+] pmelendez|12 years ago|reply
I never liked Pascal verbosity back in school but I loved C right away... Maybe it is just a matter of taste?
[+] p0nce|12 years ago|reply
I suspect braces vs begin/end pair played a big role in this, other than that I think you are correct.
[+] analog31|12 years ago|reply
Very nice! Now I've got to see if my old floppies are still readable.

While in college, I got myself an oddball MS-DOS computer. My dad read an article in the business pages about this guy who had the audacity to market a complete software product for 39 bucks. Dad didn't know anything about computers, except that there was growing interest in programming, so he got Turbo Pascal version 1 for me as an Xmas present. That was the original version, and I stuck with Turbo Pascal through successive revisions until 1994 when I discovered HyperCard on an Apple Mac.

In my view, among the strengths of TP, its manuals shouldn't be overlooked. Each version came with a relatively slim yet complete manual that a person could actually read. Of course it helped that the target platform was relatively simple too.

[+] lkesteloot|12 years ago|reply
I agree, the manuals were great. (Though I bought them again on eBay for this project and they actually contained a lot of errors!) But also the floppy came with sample programs, and I learned most of Pascal from those.
[+] optymizer|12 years ago|reply
Pascal, my childhood love. I spent so many days in TP 7.1 coding games with the graph library!

At some point I made a GUI for DOS, similar to Windows Explorer (with a desktop, icons, start menu, context menus and obligatory clock! :) ).

I called it Winnie. Then my 386 died and I lost the code :/

Thanks for the memories!

[+] soapdog|12 years ago|reply
OMG!!! That was my favorite TP. I learned to program using Turbo Pascal.

Congratulations on your work and thanks for the nostalgia!

[+] FYI|12 years ago|reply
Likewise!

I started in Basic then moved to Turbo Pascal so I could compile my code & distribute it - mostly on 5.25" floppies as cover disks on magazines.

Later by uploading them to BBSes via a 9600bps modem (yes 9.6Kbps was super quick in those days)!

[+] pjbrunet|12 years ago|reply
Seriously cool. But I can't imagine ever using it ;-) Half the fun of TP was all the libraries, like to interface Souldblaster, modems, etc. All that stuff is long gone.
[+] JasonFruit|12 years ago|reply
Very slick. I'm impressed at the compilation speed, given that it's in JavaScript and I'm on a slow machine. Pascal always was good for that — and you've made me miss it.
[+] sina|12 years ago|reply
Great work, well done! Pascal was the second language I learned, after Basic.

I'm interested in studying the code and possibly replicating it for the purpose of learning about how compilers work. Could you give a general overview of the components, or steps you took to implement them? Thanks!

[+] lkesteloot|12 years ago|reply
Thanks! The code is on GitHub: https://github.com/lkesteloot/turbopascal

It's a two-pass compiler. The first pass runs the code through the Stream, Lexer, and Parser classes to generate a parse tree (with type information). The second pass generates bytecode using the Compiler class. The compiled bytecode is then handed to Machine to execute. Start in IDE.js in the _run() function. Also go into turbo.css and comment out the "display:none" line in the .debug-output block. That'll let you see the parse tree and generated bytecode.

[+] ChuckMcM|12 years ago|reply
Nice, presumably you take the x86 emulator from yesterday, and boot it with a copy of the TurboPascal disk?

Took me a good 30 minutes to remember how to write hello world in PASCAL :-) And I admit I forgot about the period on the End statement.

[+] pjmorris|12 years ago|reply
Boy, does that bring back memories. Turbo Pascal was just plain wonderful software. As for Pascal, I never got over the array length being part of the array's type.
[+] MilesTeg|12 years ago|reply
I remember you could define an array to be indexed by an enumerated type which was nice. I haven't seen that done elsewhere.
[+] lkesteloot|12 years ago|reply
Isn't that crazy? You can't write a generic integer array sort routine!
[+] fit2rule|12 years ago|reply
Nice! I still have an Atari Portfolio in my kit, running Turbo C 1.0 .. those were the days! Everything you needed to rule the world in a single .EXE. Or .. was it a .COM? 64k should've been enough for everyone .. ;)
[+] ooooak|12 years ago|reply
/* set overflow to auto */ #screen { overflow: auto; }
[+] paulrademacher|12 years ago|reply
The examples compile super fast. How long did it use to take?
[+] vidarh|12 years ago|reply
Not much time...

Pascal (and most of Niklaus Wirth's languages) was designed to be easy to compile. The grammars are very small (typically less than a page of text; Turbo Pascal used to include the full BNF grammer in the manual), and his languages are designed to be possible to compile in one pass.

Also "traditional" Wirth-style compilers usually output code directly rather than build an abstract syntax tree and a lot of compilers of the era were heavily influenced by his compiler textbook, which included the full source of a compiler.. They also tend to output object files or final executables directly rather than go via an assembler.

All of that combine to make it very easy to make the compilers very small, and very fast even on very slow machines. For an example of simplicty, take a look at the source code to an educational Oberon-0 (subset of his Oberon language) compiler, from one of his books: http://www.inf.ethz.ch/personal/wirth/books/CompilerConstruc... (it generates code for a virtual RISC machine, and one of the files includes an interpreter for the generated code)

The whole book is available as a PDF here: http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf ; Wirth's material on compiler contruction evolved over the years from an early book about writing a PL/0 compiler, through Pascal and Modula (2) and finally Oberon, but always focusing on simplicity.

For a time I wanted to go to ETH Zurich to study computer science there because of Wirth. I'm a bit of a Wirth fanboy (which is ironic, since my favourite language these days is Ruby - a language that is pretty much the anti-thesis of Wirth's focus on simplicity of implementation).

As as a final digression, lets play six (well, 3) degrees of Pascal to Javascript: One of Wirth's PhD students was Michael Franz who got his PhD with a one of my favourite dissertations, on dynamic architecture independent code generation for Oberon. Dr Michael Franz co-invented the trace-trees technique that went into Mozilla's TraceMonkey version of their javascript JIT with Andreas Gal. Andreas Gal now works at Mozilla with Brendan Eich, the creator of Javascript.

[+] kristiandupont|12 years ago|reply
..Turbo Pascal was sort of shocking, since it basically did everything that IBM Pascal did, only it ran in about 33K of memory including the text editor. This was nothing short of astonishing. Even more astonishing was the fact that you could compile a small program in less than one second. It's as if a company you had never heard of introduced a clone of the Buick LeSabre which could go 1,000,000 MPH and drive around the world on so little gasoline than an ant could drink it without getting sick.

http://www.joelonsoftware.com/articles/fog0000000023.html

I miss Joel's writing!

[+] lkesteloot|12 years ago|reply
I remember compilation being nearly instantaneous.
[+] efutch|12 years ago|reply
Nice, no Compile option though? Is it an interpreter? I tried a Hello, World, and the (Input,Output) after the program name choked it. Nice job!
[+] lkesteloot|12 years ago|reply
The "Compile" option was, I think, for when you wanted to generate a .COM or .EXE. So I don't support that. The "Run" command does compile and run, though.
[+] lkesteloot|12 years ago|reply
Oh interesting, none of my programs had (input,output) so my parser doesn't support that. It was optional in TP and I don't think did anything.
[+] pan69|12 years ago|reply
This is cool. I remember having this on DOS in the late 80's. Never really got into Pascal though.
[+] kirkthejerk|12 years ago|reply
Yay, I don't have to use ctrl-K codes like the original TP3 editor!