Writing Code Generators Using LiveCode

Writing Code Generators Using LiveCode

Writing Code Generators Using LiveCode: Build Apps That Make Apps

by David Simpson

This article describes real-world examples for using LiveCode to develop code generator apps for a variety of software development languages.

Why Develop a Code Generator?

You might have a situation in which it is necessary to write the same types of applications on a routine basis. If there are commonly used features you need within an application there is no reason to write that code manually for each application. Of course, commonly used routines can be put into a library files for most development languages. However a static library is not going to be the best fit for every situation. Sometimes it may be better to custom generate code and use variables within the code generator to substitute changes you desire to make throughout the app.

At other times, it may be helpful to take code from one programming language and convert it into a different programming language. An automated code conversion process might be the first step to take when converting a large IT system from one development environment to another.

There is no reason to manually re-type all of the code when doing a conversion like this. Building even a simple code generator can save a lot of typing, reduce errors, and provide the developer with a sense of accomplishment. Sometimes, one of the biggest hurdles to performing a large-scale conversion project is just overcoming the inertia of starting the task.

LiveCode is very well designed for text processing, reading, and generating files. Furthermore, LiveCode can build your application as a standalone app for multiple operating systems and mobile devices. This is the route I have followed during the development of FmPro Migrator Platinum Edition.

First - A Code Meta Generator

Link to Stack File

LiveCode is a great development environment for quickly building little utilities to help you with your development projects. This tool is one of the first I created to help generate LiveCode scripts from other languages. Now, just because the title is "XML to LiveCode Converter" doesn't mean a whole lot. This little utility will take any source text and turn it into a series of PUT statements which you can paste into the LiveCode IDE script editor.


XML to LiveCode Screenshot


This annotated screenshot shows the how to use this little stack.


  • Paste in some code into the source code field, enter a variable or array name in the Output Variable Name field.
  • Click on the Generate Code & Put on Clipboard button.


The source text is converted into lines of PUT statements, written into the LiveCode field which is also put onto the clipboard.

Here is what the code looks like within the button:

Generate LiveCode Button Script


It is just a matter of looping thru the lines of code copied from the source field into the tSourceCode variable, and processing each line. The simple bit of processing being done here is the replacement of embedded quote characters with the quote constant surrounded by concatenation characters. This is followed by adding a PUT statement before the text, followed by the ending of the statement which substitutes the supplied variable name.

Following the processing of all of the lines of code in the source field, the output code is written into the output field and then copied to the clipboard using the copy command on line 20.

The result may be dozens or hundreds of lines of LiveCode code which is ready for pasting directly into the IDE. From this point onward I usually will perform my own variable substitution within the generated code. As a result of using this simple little utility, I immediately have blocks of syntactically correct code for my project. This process results in a great reduction in typing errors, syntax errors and overall frustration.

Example #1 - Generating LiveCode Stack Files


LiveCode Conversion Details

5 Part LiveCode Conversion Article


One of the unique features built into LiveCode is the storage of custom properties within stack files. This feature is immediately useful when generating LiveCode stack files from databases. FmPro Migrator Platinum Edition internally stores a binary copy of a template stack file which FmPro Migrator Platinum Edition populates with cards created from the layouts of a source database (FileMaker Pro, Microsoft Access and Visual FoxPro).

Stack File Custom Property

The contents of Custom Properties do not get modified by LiveCode when opening a stack on different platforms. However the contents of regular fields are modified with the correct line endings for the platform upon which the stack has been opened. This feature insures that if you put a binary file within a Custom Property, its contents won't be unexpectedly changed.

In addition to creating the objects on each card (graphic objects, fields, vector graphics and text objects), FmPro Migrator Platinum Edition writes customized handlers onto each card for reading the data from the database file into the stack fields. Scripts from the source database are also converted into LiveCode instructions, and written into a text file.

Example #2 - CakePHP Web Application Generation

PHP Conversion Details

As was shown in the LiveCode example, I make use of custom properties to store a variety of source files for a CakePHP website. First, I store the entire CakePHP distribution as a zip file and unzip the file to create the entire CakePHP project directory.


Cake PHP Web Application Directory

The entire directory structure within the app1 directory, has been created by writing out a custom property having the CakePHP application directory within a zip file. Then the zip file is expanded to the directory structure shown in the screenshot.

LiveCode Unzip Code

This little bit of code writes out the zip file (#590), opens the zip archive (#592), then loops thru each of the items within the archive to extract the contents (#600 - 614). The features of this code have been turned into a function for use in multiple places, but this example shows how the function works as well.

PHP Custom Property Loading Buttons

For the PHP Conversion processing, there are quite a few objects stored within custom properties, as shown in the above screenshot. There are a variety of PHP and JavaScript libraries as well as binary image and sound files stored as custom properties. Since I know that there will be upgrades to these open source libraries, I also include special build routines on this hidden utility card as well. The build scripts enable me to easily and take a downloaded source directory and customize its contents prior to including it within my application.

Example #3 - The VFP Code Converter

This is an example I wrote myself as a commercial application sold through the Mac App Store. Yes, you can make applications that convert code that are very sellable software products. Don't feel limited by the code examples here - just about any language will work, using the same principles here.

Available on Mac App Store


VFP Code Converter Screenshot

The VFP Code Converter incorporates a more sophisticated code conversion technique. This utility reads Visual FoxPro .PRG files, and converts them into server-side JavaScript code for use within the Servoy application server. However this technique can also be applied to doing conversions for other languages as well.

VFP Code Converter Switch Block

The heart of the conversion process involves: breaking up each line of code into words (Line# 25 - 29), then comparing the word to a known keyword for the source programming language (Line #51). Prior to this comparison processing, blank lines, and commented lines are accounted for and logged for statistical processing (Line #34 - 50). The end result is an output JavaScript file with many of the commonly required code changes implemented automatically.

Another valuable result is the list of conversion statistics showing the estimated conversion percentage, including coverage of commented lines, unsupported lines, and the unprocessed keywords list. If the unprocessed keywords list shows a high incidence of certain keywords getting missed during processing, then it represents a good feedback statistic for future enhancements.


The Database to LiveCode Conversion feature built into FmPro Migrator Platinum Edition enables LiveCode developers to quickly convert popular database files into full-featured LiveCode database front end applications, revLets or even mobile apps (IOS, Android). A working application can be created in seconds, without manual coding for commonly requested database features. SQL Yoga is used as the database API for generated LiveCode stack files - and SQL Yoga is also included in OmegaBundle 2012.

For the OmegaBundle, FmPro Migrator Platinum Edition includes license keys for the following features:

  • PHPToLiveCode License Key (Qty = Unlimited)
  • BasicToLiveCode License Key (Qty = Unlimited)
  • DBToLiveCode License Key (Qty = 250)
  • AccessToFmPro License Key (Qty = 250)
  • FmProToAccess License Key (Qty = 250)
  • DBToDotNet License Key (Qty = 250)
  • VFP Conversion License Key (Qty = 250)
  • PHP Conversion License Key (Qty = 250)

About the Author

David Simpson is the president of .com Solutions Inc., and a LiveCode developer of database applications since 2001.