{prompt←4↑''} (eval ##.repl) stop ⍝ Read-eval-print loop.
[repl] uses quote-quad (⍞) to input an argument for operand function [eval]. The
result is displayed in the session and the loop repeated until [stop] is input.
In particular, if [stop] is '', repl terminates when an empty line is entered.
Ref: http://en.wikipedia.org/wiki/Read-eval-print_loop
Technical note:
[repl] reports 'Eh?' if the operand function generates an error. The error-guard
is isolated in an inner function in order to preserve tail-call optimisation in
the main loop. This is a consequence of error-guards' having dynamic as opposed
to lexical scope. Without such isolation the loop would accumulate stack frames
with successive interactions.
repl←{ ⍝ Read-eval-print loop.
⍺←4↑'' ⍝ default prompt
expr←(≢⍺)↓⍞⊣⍞←⍺ ⍝ read expression
expr≡,⍵: ⍝ matches terminator: quit.
expr∧.=' ':⍺ ∇ ⍵ ⍝ blank line: ignore.
⎕←⍺⍺{ ⍝ print
0::'Eh?' ⍝ catching all errors
⍺⍺ ⍵ ⍝ eval
}expr ⍝ of expr
⍺ ∇ ⍵ ⍝ loop
}
Examples:
⌽ repl'→' ⍝ reverse of input.
hello
olleh
world
dlrow
→
⎕size repl ')' ⍝ size of object
display
3588
repl
684
notes
1363400
)
⍎ repl '' ⍝ mini-session until null input.
2+3
5
⍳3
1 2 3
display∘⍎ repl')' ⍝ boxed output
1 2 + 3 4
┌→──┐
│4 6│
└~──┘
1 2 + ⊂3 4
┌→────────────┐
│ ┌→──┐ ┌→──┐ │
│ │4 5│ │5 6│ │
│ └~──┘ └~──┘ │
└∊────────────┘
}}} ⍝ error
Eh?
)
scripts._defs∘parse repl'→' ⍝ bind left arg for dyadic fuction: →parse←
2+3
A
┌┴─┐
┌┴┐ 3
2 +
∘.+/m n
A
┌──┴──┐
┌─┴──┐ ┌┴┐
┌┴─┐ / m n
∘ ┌┴┐
. +
→
See also: parse esh lisp joy
Back to: contents
Back to: Workspaces