Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More convenient REPL support #31

Merged
merged 4 commits into from
Jun 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name = "LispSyntax"
uuid = "51c06dcf-91d3-5c9e-a52e-02df4e7cbcf5"
version = "0.2.0"
version = "0.2.1"

[deps]
ParserCombinator = "fae87a5f-d1ad-5cf0-8f61-c941e1580b46"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
ReplMaker = "b873ce64-0db9-51f5-a568-4457d8e49576"

[compat]
ParserCombinator = "≥ 2.0.0"
Expand Down
69 changes: 23 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,64 +79,41 @@ Notable Differences

REPL Mode
---------
In order to avoid having to type out `lisp"( ... )"` for each top level expression,
one can use [ReplMaker.jl](https://github.com/MasonProtter/ReplMaker.jl) to make a
REPL mode for LispSyntax.jl
```julia
julia> using LispSyntax, ReplMaker
LispSyntax.jl provides a convenience REPL, alleviating one from having to
type `lisp"( ... )"` for each top level expression. In order to use REPL
mode, simply initialize it:

julia> initrepl(LispSyntax.lisp_eval_helper,
prompt_text="λ> ",
prompt_color=:red,
start_key=")",
mode_name="Lisp Mode")
```julia
julia> using LispSyntax
julia> LispSyntax.init_repl()
REPL mode Lisp Mode initialized. Press ) to enter and backspace to exit.
```
As instructed, if we now press `)` at an empty `julia>` prompt, we enter `Lisp Mode`.
```julia
λ> (defn fib [a] (if (< a 2) a (+ (fib (- a 1)) (fib (- a 2)))))
At this point, type `)`, and you're ready to Lisp:

```clj
jλ> (* 2 (reduce + (: 1 6)))
42
jλ> (defn fib [a]
(if (< a 2)
a
(+ (fib (- a 1)) (fib (- a 2)))))
fib (generic function with 1 method)

λ> (fib 10)
jλ> (fib 10)
55
```
to go back to vanilla julia, simply press the backspace button or `Ctrl-C`

To return to the Julia prompt, simply type the backspace type or
`Ctrl-C`. Once there, you'll still have access to the fuctions you
defined:
```julia
julia> fib
fib (generic function with 1 method)

julia> fib(10)
55
```

If one want to support multi-line s-expressions then you must define
a `valid_input_checker` for the REPL mode as follows:
```julia
julia> using REPL: REPL, LineEdit; using LispSyntax: ParserCombinator

julia> function valid_sexpr(s)
try
LispSyntax.read(String(take!(copy(LineEdit.buffer(s)))))
true
catch err
isa(err, ParserCombinator.ParserException) || rethrow(err)
false
end
end
valid_sexpr (generic function with 1 method)

julia> initrepl(LispSyntax.lisp_eval_helper,
valid_input_checker=valid_sexpr,
prompt_text="λ> ",
prompt_color=:red,
start_key=")",
mode_name="Lisp Mode")
REPL mode Lisp Mode initialized. Press ) to enter and backspace to exit.
You may also create a [customized REPL](docs/repl-mode.md).

λ> (defn fib [a]
(if (< a 2)
a
(+ (fib (- a 1)) (fib (- a 2)))))
fib (generic function with 1 method)
```

TODO
----
Expand Down
60 changes: 60 additions & 0 deletions docs/repl-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Custom REPL Mode

LispSyntax.jl provides a default REPL mode, but if you'd like to
customize your own, you may do so by using
[ReplMaker.jl](https://github.com/MasonProtter/ReplMaker.jl)
directly:
```julia
julia> using LispSyntax, ReplMaker

julia> ReplMaker.initrepl(LispSyntax.lisp_eval_helper,
prompt_text="λ> ",
prompt_color=:red,
start_key=")",
mode_name="Lisp Mode")
REPL mode Lisp Mode initialized. Press ) to enter and backspace to exit.
```
At this point, type `)`, and you're ready to Lisp:

```clj
λ> (* 2 (reduce + (: 1 6)))
42
λ> (defn fib [a] (if (< a 2) a (+ (fib (- a 1)) (fib (- a 2)))))
fib (generic function with 1 method)
λ> (fib 10)
55
```

If you want to support multi-line S-expressions then you must define
a `valid_input_checker` for the REPL mode as follows:
```julia
julia> using REPL: REPL, LineEdit; using LispSyntax: ParserCombinator

julia> function lisp_reader(s)
try
LispSyntax.read(String(take!(copy(LineEdit.buffer(s)))))
true
catch err
isa(err, ParserCombinator.ParserException) || rethrow(err)
false
end
end
valid_sexpr (generic function with 1 method)

julia> initrepl(LispSyntax.lisp_eval_helper,
valid_input_checker=lisp_reader,
prompt_text="λ> ",
prompt_color=:red,
start_key=")",
mode_name="Lisp Mode")
REPL mode Lisp Mode initialized. Press ) to enter and backspace to exit.
```
```clj
λ> (defn fib [a]
(if (< a 2)
a
(+ (fib (- a 1)) (fib (- a 2)))))
fib (generic function with 1 method)
```

Note that this is provided by default with `LispSyntax.init_repl`.
2 changes: 2 additions & 0 deletions src/LispSyntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,6 @@ function include_lisp(mod::Module, io::IO)
res
end

include("repl.jl")

end # module
24 changes: 24 additions & 0 deletions src/repl.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using ParserCombinator
using REPL: REPL, LineEdit
using ReplMaker

function lisp_reader(s)
try
read(String(take!(copy(LineEdit.buffer(s)))))
true
catch err
isa(err, ParserCombinator.ParserException) || rethrow(err)
false
end
end

function init_repl(; prompt_text="jλ> ", prompt_color = :red, start_key = ")", sticky=true)
ReplMaker.initrepl(lisp_eval_helper,
repl = Base.active_repl,
valid_input_checker = lisp_reader,
prompt_text = prompt_text,
prompt_color = prompt_color,
start_key = start_key,
sticky_mode=sticky,
mode_name = "Lisp Mode")
end