<p>Implementing a <a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop" rel="noreferrer">REPL</a> 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.</p>
<p>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?</p>

<compilers><implementation><repl>
How to implement a REPL for a compiled language?
CC BY-SA 4.0
2023-05-29T23:50:07.493Z
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 - https://root.cern/cling/
Bruce Adams
CC BY-SA 4.0
2023-11-08T17:10:21.613Z
Sometimes the language's semantics make a REPL expensive or infeasible, as with Go. A complete answer may not yield a universal construction.
Corbin
CC BY-SA 4.0
2023-11-08T18:02:10.917Z
@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.
kaya3
<p>Scala 2 does the following (partial <a href="https://docs3.scala-lang.org/overviews/repl/overview.html" rel="noreferrer">source</a>):</p>
<ul>
<li>Parse every line of input, detecting which external names are referred to and adding imports from previously compiled lines</li>
<li>Compile the input + imports</li>
<li>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)</li>
<li>If the line had a result, save it to a <code>resN</code> variable, where <code>N</code> is an incrementing number than starts at <code>0</code> on every REPL start</li>
</ul>

CC BY-SA 4.0
2023-05-29T20:09:03.623Z
2023-05-29T20:09:03.623Z
RubenVerg
# How to implement a REPL for a compiled language?
Posted by: username_0 on 2023-05-29
Implementing a [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) 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?
**Tags:** compilers implementation repl
> **Comments:**
> **username_1**: 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 - https://root.cern/cling/  
> **username_2**: Sometimes the language's semantics make a REPL expensive or infeasible, as with Go. A complete answer may not yield a universal construction.  
> **username_0**: @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.  
---
## 1 answers
### Answer by username_3 on 2023-05-29. Score: 7 | Accepted answer
Scala 2 does the following (partial [source](https://docs3.scala-lang.org/overviews/repl/overview.html)):

* 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
---
