The interpreter pattern is used for situations where one thing needs to be interpreted in order to do or produce something else. What is being interpreted could be, for example, the keywords of a programming language to produce compiled code, the words in a sentence to determine meaning, or a series of numbers to control a robot's motion.
Python is an example of an interpreted scripting language. The commands and expressions typed in are interpreted and converted to tokens (numbers). These tokens are then interpreted to perform a specific set of actions. String all those tokens together and a functional program is the result.
In the same way, C# goes through a similar path of interpretation to produce abstract symbols (tokens) that are converted into an intermediate language that is then interpreted by the .Net (or Mono) runtime.
Programming languages such as C or C++ are interpreted to produce abstract symbols (tokens) that are converted directly into machine language (a discrete series of bytes or numbers) – which the CPU essentially interprets to produce some kind of action.
At a much higher level of abstraction, a domain-specific language could be used to make it simple to create business logic for an application. This domain- specific language typically consists of simple commands and parameters that, when executed in sequence, accomplish some task specific to the application. The process of executing the domain-specific language requires an interpreter. SQL (Structured Query Language) is an example of a domain-specific language, as it is dedicated to reading or writing data to a structured database.
In short, what is interpreted is a set of tokens that represent actions. The tokens can be arranged in an abstract syntax tree to provide more complex relationships between the tokens. Where these tokens come from is outside the interpreter's control.
What form the tokens take and what form the actions take after the tokens are interpreted is up to the application and the interpreter.
Note: Another example of an interpreter (which includes a parser) is shown in the Null Object Pattern example.
C++ | C# | Python | C |
---|---|---|---|
Interpreter_Class class | Interpreter_Class class | Interpreter_Class class | Interpreter_Interpret() function |
The example provided here takes an array of integers and converts each into a text word or punctuation mark separated by a space from the previous token, forming a complete sentence. In essence, interpreting the list of tokens reverses the effects of a parser.
C++
C#
Python
C
RUST
(Apologies. Doxygen does not understand Rust syntax and therefore cannot colorize the code.)