mdhender

super secret hq

REPL in Forth

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.

do {
  // read a word from the input source
  //
  word = NextWord(input);

  // evaluate the word
  //
  if (IsInDictionary(word)) {
      result = Evaluate(word);
  } else {
      result = StackPush(word);
  }

  // print the result
  //
  ShowResult(result);

  // and loop back to the top
  //
} while (1);

An advantage of the REPL when working interactively is that building and debugging a block of code is very easy. There’s no need to wait for a compiler to start up. In many environments, the REPL runs straight out of the editor, making it very easy to tinker with code and get immediate feedback on the results.

A hidden advantage is that the workflow of the REPL encourages the user to build small blocks that are easy to test. The small blocks are then combined into a working program.

Comments