:bloogeR is mE

PLT with "Crafting Interpreters" + An awesome CLI Java debugger on Linux!

June 18, 2021, 1:59 p.m.

Abstract

These past few days, I have fallen into the world of programming language theory (PLT) and along the way found a super useful GDB-like command-line java debugger on Linux!

Preface

After finishing building this blog a few days ago, I wanted to start a new project, but not any ol' project... something that I had never touched before!

And so, after scouring the web for millenia, I found the ultimate peak of computer science -- interpreters!

Well, maybe not the peak, but definitely the peak of what I have done so far.

 

In the summer of '19, my buddy and I organized a super intensive eight-week book-reading of the legendary Structures and Interpretation of Computer Programs.

We slogged through it, but eventually, through hard work, sweat, and the power of popcorn reading, we made it out as enlightened lambda wizards.

However, one great regret I have about that read-through was the last two chapters of the book:

We were worn and weary from the last three chapters that we had just overcome and to read just the first few subsections of the penultimate chapter, we squeezed enough juice out of our brains to fry a motherboard.

At that point, we unanimously agreed that we had already gotten enough out of the book and that the last two chapters were more suitable for when we were more prepared.

After skimming the last two chapters, the book club concluded and we went our separate ways. I'm not sure about the others, but for me, those last two chapters had always stuck with me...

One year -- a hellish college application process, COVID-19, and a blog later, and I'm ready.

I'm sure you've guessed it by now, but those two accursed chapters were all about interpreters and compilers!

And now I'm going to face them head-to-head.

Crafting Interpreters

After all that background, I will confess, I found Bob Nystrom's book purely by coincidence.

I was looking for projects and began researching Donald Knuth (the guy has done so much in computer science and mathematics) and the books he's written, wondering if any of them would be worth reading as a project.

Along the way, I found How to Design Programs and learned an author of the book actually created Racket (a LISP dialect) and DrRacket (an IDE for Racket)!

From there, I somehow found out about some demo programs that came with Racket -- PLT games -- which came with my installation of DrRacket.

I played some of the games and became curious about who wrote the code for these games, as well as about what PLT stood for.

And after I searched for "racket PLT games," an eight-year-old Reddit post popped up.

However, it was not about the PLT games that came with Racket, but instead about a now defunct website called plt-games.com (accessible through archive.org), which hosted competitions for the design and implementation of new programming languages.

I took a look at a few of the submissions for one of the competitions and found the languages people created to be incredibly amusing (see Reduct), and wanted to see how these were created in the first place.

Taking a look back at that Reddit post, guess who I found in the comments... none other than Bob Nyrstrom, giving an interested commenter links to implementations of multiple languages in increasing complexity -- all created by himself of course.

I clicked on one of the links, which brought me to a page showing off the language, which happened to be a subdomain of his website, and found his blog (read it!) from there.

I became enamored with his posts and experiences with writing code and books, and came across a post talking about his love for programming languages which became his inspriation to write a book that he would toil for years making and even end up releasing for FREE!

I started reading the book, Crafting Interpreters, and we're where we're at now.

GJDB

Now on to more technical stuff.

The book specifies an object-oriented C-like programming language called Lox which is implemented twice throughout the book:  A high-level implementation in Java, and a low-level bytecode implementation in C.

I'm working through the Java implementation on a Linux CLI and find my solution to a problem does not work as I expect.

I have used GDB before for C debugging on command line before and really like the minimalist flow the tool gives you.

On the other hand, the moment I executed JDB, the Java debugger, it was clunky, awkward.

I looked online for an alternative and found GJDB on a Berkley professor's personal website, which is an improved modification of JDB last updated in 2014.

Guess what, a Java debugger last updated 2014 is manifolds better and more streamlined than one that's still being updated and packaged with the latest Java SDK today.

However, as of current, the latest GJDB release is 6.4.1 and requires a few tweaks before you can get it installed with JDK 11.0.11.

Download and extract it and make the following changes in the following files:

  1. DIR/configure:98 - Remove the if statement checking JDK version starting on line 98.
  2. DIR/src/ucb/gjdb/Env.java:14 - Remove the com.sun.tools.jdi.* import on line 14.

where DIR is the directory you extracted the files.

Also note that you must have emacs installed (apt-get install emacs25 on Ubuntu).

After that, follow the directions in the README that comes with the source and you should be ready to debug any Java program you want!

 

I'll keep you updated as I work through the book if I find anything interesting!

P.S. All linked books are free!