1
+ import importlib
1
2
# noinspection PyUnresolvedReferences
2
3
import readline # noqa: F401
3
4
import traceback
7
8
8
9
import basilisp .compiler as compiler
9
10
import basilisp .lang .runtime as runtime
11
+ import basilisp .lang .symbol as sym
10
12
import basilisp .main as basilisp
11
13
import basilisp .reader as reader
14
+ from basilisp .util import Maybe
12
15
13
16
14
17
@click .group ()
@@ -33,10 +36,27 @@ def eval_str(s: str, ctx: compiler.CompilerContext, module: types.ModuleType):
33
36
return last
34
37
35
38
39
+ def bootstrap_repl (which_ns : str ) -> types .ModuleType :
40
+ """Bootstrap the REPL with a few useful vars and returned the
41
+ bootstrapped module so it's functions can be used by the REPL
42
+ command."""
43
+ repl_ns = runtime .Namespace .get_or_create (sym .symbol ('basilisp.repl' ))
44
+ ns = runtime .Namespace .get_or_create (sym .symbol (which_ns ))
45
+ repl_module = importlib .import_module ('basilisp.repl' )
46
+ ns .add_alias (sym .symbol ('basilisp.repl' ), repl_ns )
47
+ for name in ['*1' , '*2' , '*3' , '*e' , 'doc' , 'pydoc' , 'source' ]:
48
+ ns .intern (sym .symbol (name ),
49
+ Maybe (runtime .Var .find (sym .symbol (name , ns = 'basilisp.repl' ))).or_else_raise (
50
+ lambda : runtime .RuntimeException (
51
+ f"Var basilisp.repl/{ name } not found!" ))) # pylint: disable=cell-var-from-loop
52
+ return repl_module
53
+
54
+
36
55
@cli .command (short_help = 'start the Basilisp REPL' )
37
56
@click .option ('--default-ns' , default = runtime ._REPL_DEFAULT_NS , help = 'default namespace to use for the REPL' )
38
57
def repl (default_ns ):
39
58
basilisp .init ()
59
+ repl_module = bootstrap_repl (default_ns )
40
60
ctx = compiler .CompilerContext ()
41
61
ns_var = runtime .set_current_ns (default_ns )
42
62
while True :
@@ -53,16 +73,21 @@ def repl(default_ns):
53
73
continue
54
74
55
75
try :
56
- print (compiler .lrepr (eval_str (lsrc , ctx , ns .module )))
76
+ result = eval_str (lsrc , ctx , ns .module )
77
+ print (compiler .lrepr (result ))
78
+ repl_module .mark_repl_result (result )
57
79
except reader .SyntaxError as e :
58
80
traceback .print_exception (reader .SyntaxError , e , e .__traceback__ )
81
+ repl_module .mark_exception (e )
59
82
continue
60
83
except compiler .CompilerException as e :
61
84
traceback .print_exception (compiler .CompilerException , e ,
62
85
e .__traceback__ )
86
+ repl_module .mark_exception (e )
63
87
continue
64
88
except Exception as e :
65
89
traceback .print_exception (Exception , e , e .__traceback__ )
90
+ repl_module .mark_exception (e )
66
91
continue
67
92
68
93
0 commit comments