8 ms·
Show HN: My Turbo Pascal compiler in JavaScript
- ChuckMcM 13y agoNice, 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.
- danieldk 13y agoI found more details here, including a link to the source code: http://www.teamten.com/lawrence/projects/turbo_pascal_compiler/ http://www.teamten.com/lawrence/projects/turbo_pascal_compil... Well done!
- ChuckMcM 13y agoThat is awesome, somewhere I've got a pretty printer I developed for TurboPASCAL. I tried to sell all the rights to Borland in exchange for a enough cash to buy a decent oscilloscope (I have a history of 'consulting for toys' :-)and they weren't going for it. In hindsight (much later) I realized I should have set the price really high, let them try a demo with a non-refundable deposit of 10% of the price, and then feigning sadness when they decided not to proceed :-)
- acqq 13y agoSome interesting background of the author: http://www.teamten.com/lawrence/oscar/ http://www.teamten.com/lawrence/oscar/
- deleted 13y ago[deleted]
- pjbrunet 13y agoSeriously 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.
- efutch 13y agoNice, 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 13y agoOh 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.
- lkesteloot 13y agoThe "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.
- dugmartin 13y agoIf I remember right it was COM only until TP4.
- lkesteloot 13y agoYep!
- JasonFruit 13y agoVery 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.
- paulrademacher 13y agoThe examples compile super fast. How long did it use to take?
- lkesteloot 13y agoI remember compilation being nearly instantaneous.
- kristiandupont 13y ago..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 http://www.joelonsoftware.com/articles/fog0000000023.html I miss Joel's writing!
- corysama 13y agoThis article from 2009 came up again recently explaining how Turbo Pascal was so fast back on an 8MHz 8088 http://prog21.dadgum.com/47.html http://prog21.dadgum.com/47.html
- vidarh 13y agoIt was only shocking to people used to complicated behemoths of multi-pass compilers or complicated optimizing compilers with expensive internal representations. Turbo Pascal really mostly "only" does things the way Wirth used to: As simple as possible. Wirth's languages are all designed for single pass compilation with direct code generation, and if you write your compilers that way, they will be fast. It is very, very hard not to make them fast that way. His compiler textsbooks, all the way back to "Compilerbay" in 1977 (using PL/0 as the language) included full source code for compilers following that style, and they're so small the entire compilers fit in a few handful pages. (EDIT: just to make clear: I do admire Turbo Pascal - I used it quite a lot, but main the genius of Turbo Pascal was to pick the Wirth way of doing compilers and integrate it with a simple "IDE" - a large part of the amazing impression of Turbo Pascal was that you did not have to write your code to disk, exit the editor, run a compiler etc. but could do it all in one)
- ret 13y agoYour 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.js#L487 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 13y agoWell 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).
- pan69 13y agoThis is cool. I remember having this on DOS in the late 80's. Never really got into Pascal though.
- pjmorris 13y agoBoy, 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.
- lkesteloot 13y agoIsn't that crazy? You can't write a generic integer array sort routine!
- MilesTeg 13y agoI remember you could define an array to be indexed by an enumerated type which was nice. I haven't seen that done elsewhere.
- pjmlp 13y agoAt least in Ada, Modula-2, Modula-3 it is also possible.
- pjmlp 13y agoVery 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!
- pmelendez 13y agoI never liked Pascal verbosity back in school but I loved C right away... Maybe it is just a matter of taste?
- lucian1900 13y agoPascal is a safer language overall, which I would argue is a good thing. C eskewed many safety features for slightly (but not significantly) more power.
- waps 13y agoHaving written TSR's in Pascal and C I have to disagree. C had significantly more power and was a lot simpler to get working for lower level stuff.
- pjmlp 13y agoOnly if you are comparing to standard Pascal. Turbo Pascal had all the capabilities of C as well. I can produce a one to one table, if you wish. Additionally after version 5.5, it also had support for OO.
- pjmlp 13y agoI never understood the complaints about verbosity in Pascal languages and the escape to C, Perl and friends. Why not use APL then? Programs are to be read, not to be write-only.
- 10098 13y agoVerbose programs are hard to read.
- soapdog 13y agoOMG!!! That was my favorite TP. I learned to program using Turbo Pascal. Congratulations on your work and thanks for the nostalgia!
- FYI 13y agoLikewise! 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 13y agoPretty much the same here. I purchased Borland TP5 in high school which was the first version with "OOP" but I think we were using versions 3 & 4 at school which were also nice. The equivalent of WordPress back then was Telegard which was written in Pascal. The sourcecode got loose which resulted in several spin-off "Telegard hacks" and many of the "elite" BBS where essentially themed versions of that original Telegard. Then I heard Pascal morphed into Delphi? I never made it to Delphi. By 1994 I was into Javascript, Java, Rexx and best of all the opposite sex ;-)
- pjmlp 13y agoI also learned Basic (Spectrum, GW, Quick, Turbo), followed by Assembly (Z80, 68000, x86) and Turbo Pascal. So when I got to learn C, my reaction was like "meh", given that I was already used to Turbo Pascal capabilities in type safety, modules, OO, low level programming (everything that C does), blazing compilation times. So I quickly moved away from C into C++ that allowed me to use stuff I was already used to from Turbo Pascal. Parallel to that I used most of Oberon family of languages.
- cek 13y agoI forwarded this to Anders Hijlsberg [1]. It'd be fun if he commented on this here. I cut my programming teeth on Pascal and Turbo Pascal. This is hugely nostalgic for me! [1]http://en.wikipedia.org/wiki/Anders_Hejlsberg http://en.wikipedia.org/wiki/Anders_Hejlsberg
- mseepgood 13y agoMaybe he will rewrite it in TypeScript?
- optymizer 13y agoPascal, 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!
- kirkthejerk 13y agoYay, I don't have to use ctrl-K codes like the original TP3 editor!
- sina 13y agoGreat 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 13y agoThanks! The code is on GitHub: https://github.com/lkesteloot/turbopascal 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.
- paulrademacher 13y ago> see the parse tree and generated bytecode Make that a feature!
- lkesteloot 13y agoDone! Try the hidden "X" command.
- bowerbird 13y agoseverely cool. -bowerbird
- analog31 13y agoVery 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 13y agoI 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.
- ooooak 13y ago/* set overflow to auto */ #screen { overflow: auto; }
- lkesteloot 13y ago:-)
- fit2rule 13y agoNice! 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 .. ;)