INTERPRETERS

Aarushi Bhate
8 min readJun 30, 2022

--

In computer science, an interpreter is a computer program that executes instructions written in a programming or scripting language directly without first compiling them into a machine language program. Interpreters typically use one of the following strategies to execute a program:

  • Parse the source code and perform its behavior directly.
  • Translate source code into some efficient intermediate representation or object code and immediately execute that;
  • Explicitly execute stored precompiled bytecode made by a compiler and matched with the interpreter Virtual Machine.

Early versions of the Lisp programming language and the BASIC dialects of minicomputers and microcomputers are examples of the first type.

Perl, Raku, Python, MATLAB, and Ruby are examples of the second type, and UCSD Pascal is an example of the third type.

The source program is precompiled and saved as machine-independent code. This code is linked at run time and executed by the interpreter or compiler (for JIT systems). Some systems, such as Smalltalk and the latest versions of BASIC and Java, can also combine the two and three. Various types of interpreters have also been built for many languages ​​traditionally associated with compilation, such as Algol, Fortran, Cobol, C, and C ++.

Interpretation and compilation are the two main means of implementing a programming language, but they are not mutually exclusive because most interpretation systems, such as compilers, also do some translation work. The terms “interpreted language” or “compiled language” mean that the standard implementations of that language are the interpreter and the compiler, respectively. High-level languages ​​are ideally implementation-independent abstractions.

As early as 1952, interpreters were used to facilitate programming within the limits of computers at the time (for example, lack of program memory or native support for floating point numbers). The interpreter was also used at a low level for translation between machine languages, so I was able to write code for a machine that was still under construction and test it on an existing computer. The first high-level language interpreted was Lisp. Lisp was first implemented on the IBM 704 computer by Steve Russell in 1958. Russell read John McCarthy’s article and realized that (McCarthy’s surprise) the LispEval function could be implemented in machine language. As a result, a Lisp interpreter that can be used to run Lisp programs and, more precisely, “evaluate Lisp expressions” has worked.

We will discuss some popular examples of Interpreters used nowadays:

  • Ruby interpreter
  • Perl interpreter
  • MATLAB interpreter

Ruby interpreter

A Ruby interpreter is any program that is able to interpret source code written in the Ruby language.

There is no single version of the Ruby interpreter so that different human translators or interpreters can be used. Instead, there are some interpreter options for Ruby developers. Each version adds a slightly different “flavor” to the language.

How can I see that the interpreter I’m using is actually running “real” Ruby? Use Ruby Spec Suite (also known as ruby ​​/ spec). The Ruby Spec Suite is a set of tests to ensure that a particular Ruby implementation has the correct behavior and results when interpreting a program. Which Ruby interpreter to use is an open question for a particular situation.

If you have a standard version of Ruby installed, you are almost certainly using YARV (for Ruby 1.9 or later) or Ruby MRI (for Ruby 1.8 or later). In many cases, this default option is sufficient for Ruby developers. However, if your Ruby program has high performance requirements, Rubinius or JRuby may be a better choice. Benchmark tests show that Rubinius and JRuby perform about twice as well as Ruby MRI.

Another consideration for using the Ruby interpreter is the issue of threads and concurrency. Ruby MRI uses a feature called the Global Interpreter Lock (GIL). The GIL prevents multiple program threads from accessing the same data at the same time. By default, Ruby is single-threaded and does not allow parallelism or multithreading.

However, this limitation does not exist in other implementations of Ruby interpreters such as Rubinius and JRuby. You can take full advantage of multi-core and multi-CPU systems to significantly improve performance.

The maturity of a Ruby implementation is usually measured by its ability to run the Ruby on Rails (Rails) framework because the implementation is complex and uses many Ruby-specific features. The point at which a particular implementation achieves this goal is called “rail peculiarity.” The reference implementations of JRuby and Rubiniu can all run Rails in production without any changes.

Perl interpreter

The perl executable, normally installed in /usr/bin or /usr/local/bin on your machine, is also called the perl interpreter. Every Perl program must be passed through the Perl interpreter in order to execute. The first line in many Perl programs is something like:

#!/usr/bin/perl

So what does the Perl interpreter do? It compiles the program internally into the parse tree and run it immediately. Perl is commonly known as an interpreted language, but it’s not entirely true. Interpreters are sometimes referred to as interpreters / compilers because they convert to bytecode before actually executing the program. The compiled form is not saved as a file, but Perl version 5.005 contains a working version of the standalone Perl compiler.

Perl’s overall structure is primarily derived from C. Perl is procedural in nature and has variables, expressions, assignment statements, curly blocks, control structures, and subroutines.

Perl also takes features from shell programming. All variables are marked with leading sigils, which allow variables to be interpolated directly into strings. However, unlike the shell, Perl uses sigils on all accesses to variables, and unlike most other programming languages that use sigils, the sigil doesn’t denote the type of the variable but the type of the expression. So for example, while an array is denoted by the sigil “@” (for example @arrayname), an individual member of the array is denoted by the scalar sigil “$” (for example $arrayname[3]). Perl also has many built-in functions that provide tools often used in shell programming (although many of these tools are implemented by programs external to the shell) such as sorting, and calling operating system facilities.[citation needed]

Perl gets the hash (“associative array”) from AWK and the regular expression from sed. These simplify many parsing, word processing, and data management tasks. What Lisp has in common is the implicit return of the last value in a block, and all statements are both larger and usable expressions.

Perl 5 adds features that support complex data structures, first-class functionality (that is, closures as values), and object-oriented programming models. These include references, packages, class-based method dispatch and lexical scope variables, and compiler directives (such as strict pragmas). An important additional feature introduced in Perl 5 was the ability to package code as a reusable module. “The overall purpose of the Perl 5 modular system was to drive the growth of the Perl culture, not the Perl core,” Wall later explained.

All versions of Perl perform automatic data entry and automatic memory management. The interpreter knows the type and storage requirements of each data object in the program. Use reference counting to allocate space for them and free them as needed (thus, you cannot free circular data structures without manual intervention). Legal type conversion — Example: B. Number to string conversion — Performed automatically at run time. Invalid type conversion is a fatal error.

Matlab interpreter

MATLAB (an abbreviation of “MATrix LABoratory”[22]) is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages.

While MATLAB is primarily intended for numerical computation, the optional toolbox uses the MuPAD symbolic engine, which provides access to symbolic computation capabilities. An additional package, Simulink, adds multi-domain graphical simulation and model-based design for dynamic and embedded systems.

As of 2020, MATLAB has more than 4 million users worldwide. They come from different disciplines of technology, natural sciences and business.

MATLAB supports structure data types. Since all variables in MATLAB are arrays, the more appropriate name is “Structure Array”, and each element of the array has the same field name. MATLAB supports object-oriented programming such as classes, inheritance, virtual shipping, packaging, pass-by-value semantics, and pass-by-reference semantics. However, the syntax and calling conventions are very different from other languages.

MATLAB can call functions and subprograms written in the C or Fortran programming language. A wrapper function is created that can be used to pass MATLAB data types. A MEX file (MATLAB executable file) is a dynamically loadable object file created by compiling such a function. Since 2014, more and more bidirectional interfaces with Python have been added.

Libraries written in Perl, Java, ActiveX, or .NET can be called directly from MATLAB, and many MATLAB libraries (such as XML or SQL support) are available as wrappers for implemented Java or ActiveX libraries. increase. Calling MATLAB from Java is more complicated, but you can do it using the MATLAB Toolbox, which is available separately from MathWorks, or by using an undocumented mechanism called JMI (Java-to-MATLAB Interface). I can do it.

Conclusion

Interpreters are often used to execute tag and glue languages ​​because each operator running in a tag language is usually a call to a complex routine such as an editor or compiler. Self-modifying code can be easily implemented in the interpreted language. This is related to the origin of interpretation in the study of Lisp and artificial intelligence. Machine code for hardware architectures can be executed using virtual machines. This is often used when the desired architecture is not available, especially when performing multiple copies.

Sandbox: Some types of sandboxes rely on operating system protection, but interpreters or virtual machines are often used. The actual hardware architecture and the originally intended hardware architecture may or may not be the same. This may seem pointless, except that the sandbox does not force you to actually execute every statement in the source code it processes. In particular, it may refuse to execute code that violates working security restrictions. An emulator designed to run computer software written for outdated and unavailable hardware on modern devices.

--

--