Textbooks
- Crafting Interpreters by Robert Nystrom.
- Programming in Scala by Odersky, Spoon, Venners, and Sommers, 5th edition, published by Artima Press. (Code and Other Resources)
In the first half of the course, we will look at a toy implementation of a programming language, called Lox. We will go through parts I and II of Crafting Interpreters (the java interpreter). The textbook is free in its HTML form, which is excellent. You can also buy a PDF or hard copy.
The first half of the course will also introduce Scala, which is an industrial-strength language. We will look at functional programming in Lox and Scala, with an emphasis on understanding higher-order functions and closures.
In the second half of the course, we will look at additional language features in Lox, Scala, Javascript, and other languages. In particular, we will look at how Lox and Scala integrate object-oriented features. The Scala text we use is excellent -- it describes the language from the point of view of a programming language theorist.
Software
- Java. I recommend version 21, which has long term support (LTS), but later versions are fine.
- VSCode. You don't technically need this, but you do need some editor. And VSCode does everything we need. You need to install some extensions:
-
Homebrew. If you are on a mac, you
will need Homebrew to install Maven and Scala. If you are
Windows you can skip this step.
- Be sure to do the "Next Steps" printed on the terminal after executing the brew install script.
- Here is a step-by-step guide.
-
Scala. We
will use Scala as an example language. To install it, just
follow the instructions at the top of the download page.
- For now, we will simply run the Scala interpreter from the command line.
-
After installing, (re)start a terminal window and type
scala
. You should get a message with the Scala version, followed by the promptscala>
. At this prompt you can type any Scala expression, such as1 + 5
. After you press the return/enter key, Scala will read the expression, evaluate it, and print the result. The is a Read-Eval-Print loop, or REPL. - Later in the quarter we will be using a Scala tool called sbt. If sbt says server failed to start and Could not create lock then there is another instance of sbt running. Exit all your programs and try to delete the target directory using the finder/windows explorer. If that does not work, restart your computer and try again. Once you can delete the target directory, then you can start VSCode and sbt again.
-
VSCode understands
build.sbt
files, but seems to be a bit slow about picking up tests. If you want to run sbt tests using VSCode buttons, then it seems to be necessary to open a single test file in the VSCode editor. After doing this, VSCode seems to recognize all of the test files properly.
-
Maven. We
will use Maven to build, test, and run a toy language
interpreter, written in java. The toy language is called Lox.
-
On a mac, type
brew install maven
- On windows, you install the zipfile from the maven website and then add the maven bin directory to your path. Here is a step-by-step guide.
-
To compile the Lox interpreter, download the source code from
the course webpage and unzip it; in the terminal, change
directory into the unzipped folder that contains the file
pom.xml
. In that folder, runmvn compile
. Ifmvn compile
says there is no POM in this directory then you are startingmvn
in the wrong directory. -
Run the tests using
mvn test
. -
After compiling, you can run the Lox interpreter with
java -cp target/classes com.craftinginterpreters.lox.Lox
. You must be in the same directory where you successfully executedmvn compile
. -
I like running a combination command on my mac:
mvn -q clean compile && java -cp target/classes com.craftinginterpreters.lox.Lox
To run Lox on a file, I use:mvn -q clean compile && java -cp target/classes com.craftinginterpreters.lox.Lox x.lox
Note: The option-q
suppresses non-error output. On my system, this does not behave as I expect unless I add theclean
command.
-
On a mac, type
General Resources
Please report broken links to the instructor.
Online Editors/Compilers/Interpreters
- General read-eval-print-loop: repl.it (Note that BiswaScheme does not support rational literals)
- General compiler: godbolt.org