This is the book I am reading and I started to code the interpreter using JDK19’s preview features.
The book though uses an older version of the JDK.
The preview features introduce record pattern matching but soon I realized languages like Haskell and OCaml
were built with such pattern matching in mind.
So I decided to code part of it using Haskell. And then I started to code the same parts using Racket
which is a far more lofty goal. I think.
Explanation(TODO)
As I mention every time I am still learning Functional Programming principles. So I put together
this code based on data structures generally used for such purposes. The intention is to drive
this test based on structures like these.
Haskell Code
I will refactor this code and probably create a Git repo.
The result is this. The error condition is not tested.
Typed Racket code
I read that Racket has many levels and types of languages and here I am using Typed Racket.
I have to note that this is my first ever Racket code and the intention is to port my haskell to Typed Racket.
So I have explained what I learnt using pieces of code. Neither the Racket code or Haskell code implementes
the entire interpreter or even the lexer. The git repo. will have the entire code if I manage to learn
Racket sufficiently to code the whole interpreter in the book.
Step 1
This compiles without errors. But the code is not tested. I will add more test code while I refactor.
In this case I have a feeling that the code is more verbose than the equivalent Haskell code.
Simple test
But this prints
without the value. So I have to learn how to print a struct.
This is the Racket way of printing the struct with the value.
Step 2
This took longer than I expected as Racket seems to be very different than even Haskell. Many iterations later with help
from Racket experts I was able to understand how to code the function.
The type of this function is only slightly different than the Haskell code and the error conditions
are not tested fully. The Offset of each charater is not tracked but that should not be difficult.