Interpreters

An interpreter translates and executes a program one statement at a time. Python is a popular example of an interpreted language. Scripts in languages such as JavaScript are interpreted. Compilation of Java source code outputs universal Java bytecode, which is then interpreted by the virtual machine appropriate for the computer that is running the program. The new Oxygene for Java compiler translates the Pascal-like source code to Java bytecode. The bytecode has binary codes for opcodes, but you can see an intelligible, disassembled version on Wikipedia.

Pascal is usually compiled rather than interpreted. In the Delphi and Lazarus IDE's however, an interpreter is present to aid the debugging process. You can, for example, set breakpoints and step through the code a statement at a time (shortcut key F7), examining the current values of variables if necessary. You can use Pascal to write an interpreter for arithmetic expressions, for Pascal programs or for programs written in another language. See Part IV of Let's Build a Compiler!, Peter's Arithmetic and Christopher's Calculator.

There are interpreters for the Pascal language written in Pascal and in other languages, and Pascal has been used to write interpreters for other languages. Very recently, we have installed Pascal Script from RemObjects. The (free) executable file installs scripting components, in our case for Delphi 7. Translation of a script, written in Pascal, proceeds first to bytecode, which is then interpreted if no errors are detected. RemOjects explain the purpose of the software: "A scripting engine allows an end user to customize an application to his or her needs without having to recompile it. In addition, you can update your applications by just sending a new script file that could even be compiled to byte code, which cannot easily be transformed back to source code." The software is not designed for beginners, but is supported by examples and a tutorial.

To use the above tutorial,

  1. Create a new application. Save all. Save the unit as uRun and the project as ScriptFileDemo.
  2. Drop a button and a TPSScript component onto the form.
  3. Name the script component Ce.
  4. Name the button btnRun and give it the caption 'Run'.
  5. Double click on the button and paste the first section of code into the procedure.
  6. Correct the spelling of successful in two places.
  7. Declare the local variable i as an integer.
  8. Reduce the size of the form.
  9. Click on the button.

You should see something like this,

Successful minimal script compilation

Successful minimal script compilation

then this.

Successful minimal script execution

Successful minimal script execution

  1. Copy the ShowNewMessage procedure and paste it under the button click procedure.
  2. Copy the declaration of the procedure (without the TForm1. prefix) and paste it under the declaration of the button click procedure.
  3. Double click in the drop down list of the OnCompile event of Ce in the Object Inspector.
  4. Copy this code of CeCompile
    Sender.AddMethod(Self, @TForm1.ShowNewMessage, 'procedure ShowNewMessage (const Message: string);');     
       
    .
  5. Change the first line of the button click procedure to
    ce.Script.Text := 'begin ShowNewMessage(''Show This !''); end.';
            
  6. While crossing your fingers, execute the program. We obtained this.
    Parameter passed successfully

    Parameter passed successfully

The ablest amongst you might be able to use Pascal Script to add variety to your own applications.

We are impressed by Lawrence Barsanti's blog, in which he presents demonstration code and explains carefully the complexities. For example, "When you extend the compiler, you are telling it that additional classes, functions, and types will be available at runtime. On the other hand, when you extend the runtime, you provide it with the actual code that will be executed when the additional classes and functions are used by a script." You can download the source code and the executable. We include some screenshots of the executable in action with different scripts.

Unchanged Script

Unchanged Script

The other screenshots show only the input and output text. For our first change we made small modifications to the original.

Modified Script

Modified Script

Our next example shows one of several error messages we received while experimenting. Messages pinpointed the errors, but were sometimes misleading.

Error Message

Error Message

The demonstration program makes it easy to display output. We take advantage of this in the following simple script.

Simple Script

Simple Script

The interpreter is capable of handling complex scripts. Or final example is slightly less simple and demonstrates the use of arrays, WHILE and REPEAT loops and an IF statement.

Array, REPEAT, WHILE and IF

Array, REPEAT, WHILE and IF

We would welcome contributions of programs with scripting in Pascal. Pascal Script works in Lazarus but we have not yet tried it.

Programming - a skill for life!

Compilers (including Jack Crenshaw's "Let's Build a Compiler" applied to Intel processors and the Raspberry Pi), assemblers and interpreters