Search code examples
lazarusfreepascal

How to Execute External or Dynamic Code in Pascal (FPC/Lazarus)


First at all, I come from Visual FoxPro 9.0.

All my apps made on VFP can execute dynamic code from any events (Buttons, Grids, Forms, etc).

Example:

lcFile = "c:\test\myDynamicCode.txt"
=EXECSCRIPT(FILETOSTR(lcFile))

Is it possible to execute it on Pascal?

Thanks...!


Solution

  • The default install of Lazarus comes with a set of components under the general title "Pascal Script" which can be used to implement a scripting engine and debugger - see

    http://wiki.freepascal.org/Category:Pascal_Script

    and

    http://wiki.freepascal.org/Pascal_Script_Examples

    The scripting engine can compile (to byte code) and execute scripts written in a dialect of Object Pascal which is close to, but not identical with, the Object Pascal which Delphi implements. The package comes with code wrapper around a number of the supplied Lazarus/FPC units which implement components which are intended to be equivalent to a number of Delphi's components.

    In short, you should find that you can use Pascal Script to write and dynamically execute Object Pascal code equivalent to the majority of what you could write and compile in FPC. As you will gather from the code examples in the second link, the Pascal Script engine allows you to add custom functions defined in your own Lazarus/FPC code and make your forms and classes accessible to, and scriptable by, a Pascal Script script.

    If you need a scripting engine to implement another language, there are 3rd party libraries available to assist with this. Most were written for Delphi but should translate easily to FPC + Lazarus. Examples include

    TP Lex/Yacc: http://www.musikwissenschaft.uni-mainz.de/~ag/tply/

    Delphi Compiler Generator: http://www.soft-gems.net/index.php/tools/delphi-compiler-generator

    So, in short, if what you are wanting to do can be expressed in Lazarus/FPC code, there is a very good chance it can be executed in Pascal Script code. If you need an engine for another scripting language, TP Lex/Yacc and DCG will allow you to write your own scripting language, but be aware that it will be a lot more work than using the Pascal Script components.

    Btw, I don't know whether Linux is of any interest to you, but the Linux version of Lazarus/FPC also comes with the Pascal Script commponents.