Read-eval-print loop

Read-eval-print loop

A read-eval-print loop (REPL), also known as an interactive toplevel, is a simple, interactive computer programming environment. The term is most usually used to refer to a Lisp interactive environment, but can be applied to command line shells and similar environments for Smalltalk, Python, Haskell, APL, BASIC, J, Scheme, TCL, and other languages as well.

In a REPL, the user may enter expressions, 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:

:The read function accepts a single expression from the user, and parses it into a data structure in memory. For instance, the user may enter the s-expression (+ 1 2 3), which is parsed into a linked list containing four data elements.: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 6.: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 6 does not need much formatting to print.

The REPL is commonly misnamed an "interpreter". This is an erroneous usage, since many programming languages that use compilation (including bytecode compilation) have REPLs, such as Common Lisp and Python.

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 opaque data 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; in Common Lisp, the # form. The REPL of CLIM 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.

Advantages

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, 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.

Implementation

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 syntax tree created by read. Another possibility is to compile the syntax tree into machine code and execute it.

Major language environments and associated REPLs

* Python has a built-in REPL and a more enhanced version in IPython.
* 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.
* 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.
* At least two Haskell implementations provide REPL functionality. The Glasgow Haskell Compiler provides ghci and the Hugs interpreter is often used by beginners as a Haskell REPL.
* PHP does not have a built-in REPL, but has the third-party [http://www.phpsh.org/ phpsh] .
* Mozilla's internal JavaScript engine can be interactively manipulated through [http://hyperstruct.net/projects/mozrepl MozRepl] .
*Windows PowerShell, part of Microsoft Windows, provides a REPL.
* Scala (a language on the JVM) provides a REPL; start it at the command line by typing "scala" (as opposed to scalac, the compiler).
* Perl provides a REPL via the module [http://search.cpan.org/~mstrout/Devel-REPL-1.002001/lib/Devel/REPL.pm Devel::REPL] .
* F# has a REPL in the form of a command called fsi (F# interactive).
* Matlab, Mathematica and other computer algebra systems are mainly accessed through REPL which allow for the evaluation of mathematical expressions and invocation of system commands.

External links

* Paul Graham has written a [http://www.paulgraham.com/rootsoflisp.html description of a REPL implementation] in Common Lisp.
* [http://neugierig.org/software/c-repl/ c-repl] an REPL for C


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • read-eval-print loop — noun An interactive programming language interpreter. It is easier to experiment in langauges that provide a read eval print loop …   Wiktionary

  • Eval — In some programming languages, eval is a function which eval uates a string as though it were an expression and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the… …   Wikipedia

  • Lisp (programming language) — Infobox programming language name = Lisp paradigm = multi paradigm: functional, procedural, reflective generation = 3GL year = 1958 designer = John McCarthy developer = Steve Russell, Timothy P. Hart, and Mike Levin latest release version =… …   Wikipedia

  • Common Lisp — Paradigm(s) Multi paradigm: procedural, functional, object oriented, meta, reflective, generic Appeared in 1984, 1994 for ANSI Common Lisp Developer ANSI X3J13 committee Typing discipline …   Wikipedia

  • Factor (programming language) — Infobox programming language name = Factor paradigm = stack based year = 2003 developer = Slava Pestov latest release version = Continuous Builds [http://factorcode.org/binaries.fhtml] typing = strong, dynamic influenced by = Joy, Forth, Lisp,… …   Wikipedia

  • REPL — (Read eval print loop) простая интерактивная среда программирования. Данный термин наиболее часто используется применительно к интерактивной среде языка программирования Lisp, однако может использоваться и применительно к интерактивным средам… …   Википедия

  • Command-line interface — Screenshot of a sample Bash session. GNOME Terminal 3, Fedora 15 …   Wikipedia

  • Command-line interpreter — A command line interpreter (also command line shell, command language interpreter) is a computer program that reads lines of text entered by a user and interprets them in the context of a given operating system or programming language. Command… …   Wikipedia

  • Standard ML — Infobox programming language name = Standard ML logo = paradigm = multi paradigm: functional, imperative year = designer = typing = strong, static, inferred dialects = Alice, Dependent ML implementations = MLton, MLWorks, Moscow ML, Poly/ML,… …   Wikipedia

  • Ruby (Programmiersprache) — Ruby Offizielles Logo Basisdaten Paradigmen: multiparadigmatisch Erscheinungsjahr: 1995 …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”