mdhender

super secret hq

LISP in Go

Permalink

I’m keen on learning Go. I’ve kicked around the sample programs and understand the syntax. I can write code that compiles but I’m not yet to the point where I feel comfortable programming it. You could say that I know that grammar but not the idioms.

I’m going to change that by writing a good sized program in Go. I love LISP, so that’s a natural choice for me. Luckily for me, Leo Howell wrote a very nice series of articles on building a LISP interpreter in C. I’ve gone through that and think that I understand it well enough to rework it in Go. There’s enough to it that I should learn how to express myself in Go.

I’m keen on learning Go, so I am building Leo’s interpreter with it. It’s a good opportunity since there’s a lot more to it than the typical “Hello, Universe” examples. Luckily, I understand the interpreter well enough that the challenge will be expressing myself in Go. (Well, I think that I understand it; this will prove it.)

As a bonus, I watched Rob Pike’s lecture on lexing with goroutines, and that looks like a lot of fun to try.

I plan to follow Leo’s outline, so feel free to compare against the original.

The Forth Interpreter Loop

Permalink

In the Forth Programmer’s Handbook, Conklin and Rather outline the basics of the Forth interpreter:

while true
  get next lexeme
  lookup lexeme in dictionary
  if word is found
      execute word
      if there is stack underflow
          display "stack empty"
          reset stacks and intepreter
  elsif lexeme is numeric
      convert to binary
      push result onto stack
  else
      display "unknown word"
      reset stacks and intepreter
end-while

Branching In False Forth

Permalink

Most programming languages support conditional logic by using a some form of “if condition then codeBlock1 else codeBlock2 end-if.” The “else codeBlock2” is usually optional.

The “condition” evaluates to either true or false. If the condition evaluates to true, the code in the first block is executed. Otherwise, the code in the second block is executed (assuming that it is present).

Most programming languages support conditional logic by using a some form of “if condition then codeBlock1 else codeBlock2 end-if.” The “else codeBlock2” is usually optional.

REPL in Forth

Permalink

Many interpreters implement a read, evaluate/execute, print loop (also known as “REPL”) when interacting with the user. The REPL fetches code to evaluate, passes it to the interpreter to evaluate and execute, then prints the output to the console.