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,
- Create a new application. Save all. Save the unit as uRun and the project as ScriptFileDemo.
- Drop a button and a TPSScript component onto the form.
- Name the script component Ce.
- Name the button btnRun and give it the caption 'Run'.
- Double click on the button and paste the first section of code into the procedure.
- Correct the spelling of successful in two places.
- Declare the local variable i as an integer.
- Reduce the size of the form.
- Click on the button.
You should see something like this,
Successful minimal script compilation
Successful minimal script execution
- Copy the ShowNewMessage procedure and paste it under the button click procedure.
- Copy the declaration of the procedure (without the TForm1. prefix) and paste it under the declaration of the button click procedure.
- Double click in the drop down list of the OnCompile event of Ce in the Object Inspector.
- Copy this code of CeCompile
Sender.AddMethod(Self, @TForm1.ShowNewMessage, 'procedure ShowNewMessage (const Message: string);');.
- Change the first line of the button click procedure to
ce.Script.Text := 'begin ShowNewMessage(''Show This !''); end.';
- While crossing your fingers, execute the program. We obtained this.
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.
The other screenshots show only the input and output text. For our first change we made small modifications to the original.
Our next example shows one of several error messages we received while experimenting. Messages pinpointed the errors, but were sometimes misleading.
The demonstration program makes it easy to display output. We take advantage of this in the following 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
We would welcome contributions of programs with scripting in Pascal. Pascal Script works in Lazarus but we have not yet tried it.