Implementing a REPL is relatively straightforward for an interpreted language; the interpreter maintains the program state as normal, and evaluates each separate statement as normal, just using the same evaluation context for a whole interactive session.

The same cannot be said for compiled languages, since a compiler does not generally have a mechanism for maintaining program state across executions of different compilation units. How can a REPL be implemented using a compiler, without writing an interpreter separately for the language?

  • 1
    $\begingroup$ An example of this is Cint - the C++ interpreter developed at CERN. Although there is a fair amount of interpretation going on just in a debugger like gdb. This was replaced by cling - root.cern/cling $\endgroup$ Commented May 29, 2023 at 23:50
  • $\begingroup$ Sometimes the language's semantics make a REPL expensive or infeasible, as with Go. A complete answer may not yield a universal construction. $\endgroup$
    – Corbin
    Commented Nov 8, 2023 at 17:10
  • $\begingroup$ @Corbin If you can expand on that (i.e. what about Go makes it infeasible to write a REPL), I'd welcome it as an answer. $\endgroup$
    – kaya3
    Commented Nov 8, 2023 at 18:02

1 Answer 1


Scala 2 does the following (partial source):

  • Parse every line of input, detecting which external names are referred to and adding imports from previously compiled lines
  • Compile the input + imports
  • Save every exported name in the known ones, potentially overriding old things with the same name (things that referred to the old ones still use the old ones, as they were already compiled)
  • If the line had a result, save it to a resN variable, where N is an incrementing number than starts at 0 on every REPL start

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .