The Hy REPL¶
Hy’s REPL (read-eval-print loop) [1] functionality is implemented in the hy.cmdline.HyREPL
class. The HyREPL
extends the Python Standard Library’s code.InteractiveConsole
[2] class. For more information about starting the REPL from the command line, see Command Line Interface. A REPL can also be instantiated programatically, by calling hy.cmdline.run_repl
- see Launching a Hy REPL from Python.
From a high level, a single cycle of the REPL consists of the following steps:
- tokenize and parse input with
hy.lex.hy_parse
, generating Hy AST [3]; - compile Hy AST to Python AST with
hy.compiler.hy_compile
[4]; - execute the Python code with
eval
; and - print output, formatted with
output_fn
.
REPL Built-ins¶
Recent Evalution Results¶
The results of the three most recent evaluations can be obtained by entering *1
(most recent), *2
, and *3
. For example:
=> "first"
'first'
=> "second"
'second'
=> "third"
'third'
=> f"{*1},{*2},{*3}"
'third,second,first'
Note
The result of evaluating *i
itself becomes the next most recent result, pushing *1
to *2
, *2
to *3
, and *3
off the cache.
Most Recent Exception¶
Once an exception has been thrown in an interactive session, the most recent exception can be obtained by entering *e
. For example:
=> *e
Traceback (most recent call last):
File "stdin-8d630e81640adf6e2670bb457a8234263247e875", line 1, in <module>
*e
NameError: name 'hyx_XasteriskXe' is not defined
=> (/ 1 0)
Traceback (most recent call last):
File "stdin-7b3ace8766f1e1cfb3ae7c01a1a61cebed24f482", line 1, in <module>
(/ 1 0)
ZeroDivisionError: division by zero
=> *e
ZeroDivisionError('division by zero')
=> (type *e)
<class 'ZeroDivisionError'>
[1] | https://en.wikipedia.org/wiki/Read-eval-print_loop |
[2] | https://docs.python.org/3/library/code.html |
[3] | Steps 1 and 2: Tokenizing and Parsing |
[4] | Step 3: Hy Compilation to Python AST |