(+ 1 2 3)，这句活会被解析成一个包含四个元素的链表。
- 求值函数 负责处理内部的数据结构并对其求值。在Lisp中，求一个以函数名开头的ｓ－表达式意味着对接下来的参数调用那个函数。所以函数"
1 2 3上调用，产生结果
In a REPL, the user enters one or more expressions (rather than an entire compilation unit), which are then evaluated, and the results displayed. The name read–eval–print loop comes from the names of the Lisp primitive functions which implement this functionality:
- 读取 函数会接受一个用户的表达式，并将它处理成内存中的数据结构。举个例子，用户可能输入一个 S-表达式
(+ 1 2 3)，而它会被处理成有四个元素的 链表。
- The eval function takes this internal data structure and evaluates it. In Lisp, evaluating an s-expression beginning with the name of a function means calling that function on the arguments that make up the rest of the expression. So the function
+is called on the arguments
1 2 3, yielding the result
- The print function takes the result yielded by eval, and prints it out to the user. If it is a complex expression, it may be pretty-printed to make it easier to understand. In this example, though, the number
6does not need much formatting to print.
A REPL can become an essential part of learning a new language as it gives quick feedback to the novice. Many tool-suites as well as programming languages use a REPL to allow algorithm exploration and debug, such as MATLAB, ROOT, SciPy and IPython. The doctest module of the Python programming language allows tests to be easily generated from the captured output of its REPL command line shell.
Because the print function outputs in the same textual format that the read function uses for input, most results are printed in a form that could (if it's useful) be copied and pasted back into the REPL. However, it's sometimes necessary to print representations of elements that can't sensibly be read back in, such as a socket handle or a complex class instance. In these cases, there must exist a syntax for unreadable objects. In Python, it's the
<__module__.class instance> notation, and in Common Lisp, the
#<whatever> form. The REPL of CLIM, SLIME, and the Symbolics Lisp Machine can also read back unreadable objects. They record for each output which object was printed. Later when the code is read back, the object will be retrieved from the printed output.
To implement a Lisp REPL, it is necessary only to implement these three functions and an infinite-loop function. (Naturally, the implementation of eval will be complicated, since it must also implement all the primitive functions like car and + and special operators like if.) This done, a basic REPL itself is but a single line of code: (loop (print (eval (read)))).
One possible implementation of eval is as a recursive interpreter that acts on the abstract syntax tree created by read. Another possibility is to compile the syntax tree into machine code and execute it.
Real REPL implementations in Lisp are often much more complicated. Typical functionality provided by a Lisp REPL includes:
- History of inputs and outputs.
- Variables are set for the input expressions and results. These variables are also available in the REPL. For example in Common Lisp * refers to the last result, ** and *** to the results before that.
- Levels of REPLs. In many Lisp systems if an error occurs during the reading, evaluation or printing of an expression, the system is not thrown back to the top-level with an error message. Instead a new REPL, one level deeper, is started in the error context. The user can then inspect the problem, fix it and continue - if possible. If an error occurs in such a debug REPL, another REPL, again a level deeper, is started. Often the REPL offers special debug commands.
- Error handling. The REPL provides restarts. These restarts can be used, when an error occurs to go back to a certain REPL level.
- Mouse sensitive input and output of data objects.
- Input editing and context specific completion over symbols, pathnames, class names and other objects.
- Help and documentation for commands.
- Variables are controlling the reader. For example the variable *read-base* controls in which base numbers are read by default.
- Variables are controlling the printer. Example: maximum length or maximum depth of expressions to print.
- Additional command syntax. Some REPLs have commands that follow not the s-expression syntax, but often work with Lisp data as arguments.
- Graphical REPLs. Some Lisp REPLs (the CLIM Listener is an example) accept also graphical input and output.
Major language environments and associated REPLs[编辑]
- C does not have a built-in REPL, but third-party REPLs exist, like c-repl and ccons.
- C++ does not have a built-in REPL, but third-party REPLs exist, like cint and cling.
- C# does not have built-in REPL but has an implementation in Mono through CsharpRepl. To some extent REPL can also be imitated in LINQPad free interactive utility. The Microsoft Roslyn CTP contains a C# REPL which can be accessed through Visual Studio (View > Other Windows > C# Interactive Window).
- Clojure, a recent Lisp dialect, provides a REPL.
- CoffeeScript provides a REPL with mutli-line input support and syntax-highlighted output (coffee).
- Common Lisp, most implementations have built-in REPLs.
- Emacs Lisp, the Emacs editor features a REPL for elisp called ielm
- Erlang provides a REPL (erl).
- F# has a REPL in the form of a command called fsi (F# interactive).
- Falcon has an interactive mode by running falcon -i.
- Forth was classically implemented as a REPL interpreter which would either define or evaluate "words." These words form the building blocks of a Forth program, which is usually a list of words. The ability to operate as a compiler instead of just as a REPL interpreter was only added more recently.
- Frink has a built-in REPL. The command-line version provides a REPL interface if no input file is given, and the default GUIs provide a REPL or more traditional programming mode.
- Groovy has a built-in REPL (), as well as a graphical console, which can run locally or on the web (http://groovyconsole.appspot.com/).
- Haskell has at least two implementations which provide REPL functionality. The Glasgow Haskell Compiler provides ghci and the Hugs interpreter is often used by beginners as a Haskell REPL.
- Java does not have a built-in REPL, but has a pure Java implementation in BeanShell. It can also be driven via scripting languages ported to the Java platform like Jython or Groovy. There is a Java REPL implementation in DrJava accessible from the Interactions pane and in JGrasp IDE .
- Julia has a REPL launched by the command julia without any parameters.
- Lua has an optional REPL. Several third party REPLs exist, such as ilua and lua-repl.
- MATLAB, Mathematica, Sage (mathematics software) and other computer algebra systems are mainly accessed through REPL which allow for the evaluation of mathematical expressions and invocation of system commands.
- OCaml has a built-in REPL (called a toplevel system in OCaml terminology), via the command ocaml. You can also build a customized REPL with custom code preloaded using the ocamlmktop utility.
- Perl provides a REPL via the module Devel::REPL, also Perl Console.
- PHP's command-line interface has an interactive mode, which you can run by doing php -a (assuming php is your PHP-CLI executable, not PHP-CGI). There is also phpsh and wigwam, which have tab completion and other features, and boris, which claims to be more robust than PHP's native interactive mode.
- Prolog, a logic programming language, by default, boots into a REPL interface.
- Python has a built-in REPL and a more enhanced version in IPython and bpython.
- Q by default starts a REPL interface.
- R statistical programming language, by default, boots into a REPL interface.
- Ruby's standard library contains a module called IRB which stands for Interactive Ruby Shell which provides a REPL. The standard Ruby distribution packages a standalone utility called irb which simply spawns an IRB instance. Even more powerful than irb is PRY, which can be easily installed as a gem.
- Scala provides a REPL; start it at the command line by typing "scala" (as opposed to scalac, the compiler).
- Scheme, most implementations have built-in REPLs.
- Smalltalk, many implementations also have a command line REPL built in. All Smalltalks with a GUI also have the "workspace" built in which is a more advanced form of the REPL where you select code and "do it" or "print it" or "inspect it" to evaluate the code. Files can also be loaded using the stream-fileIn operation.
- Standard ML of New Jersey has a built-in REPL invoked by the "sml" command.
- SuperCollider, a language for sound synthesis, analysis and algorithmic composition.
- Windows PowerShell, part of Microsoft Windows, provides a REPL.
- xBase, most implementations (Foxpro, DBase III) have a "." prompt providing a REPL
Applications with a REPL[编辑]
- Apple's Safari web browser has a command prompt under the Develop menu, Show Error Console window.
- Some window managers have a REPL, for example Sawfish and StumpWM and Fvwm (with FvwmTalk module).
- Sublime Text 2 editor has a Python console, and a plugin (SublimeREPL) for other languages.