The PrologJ Compiler

Overview

The PrologJ system includes a compiler that can translate Prolog source file into a Java .class file, which can be loaded into the Prolog system at run time, or can be used by other Java .class files (either compiled from Prolog source code or written directly in Java), or can be run as a stand-alone application (perhaps in conjunction with other .class files).

The compilation process turns a Prolog source file into a single Java class whose name is the same as the base name of the source file (name minus the .pro file type). (This requires that the base name of any Prolog source file that is to be compiled must be a legal Java class name.)

The compiled file contains a translation of each clause appearing in the source file. Though it would be unusual to do so, it is legal to compile a file that contains no clause definitions, in which case the compiled file still contains the other components discussed below.

The compiled file may contain a main method and/or one or more entry points callable from Java code, if the source file contains, respectively, a :- main/1 directive or one or more :- entry/1 directives.

In addition to the above, the compiled file also contains a static initializer method that is run by the JVM when the class file is loaded. The static initializer performs the following tasks in the order indicated:

The compiled code makes use of various classes defined in the PrologJ implementation. For this reason, the file prologj.jar must always be in the classpath when the compiled file is used.

Running the Compiler

The PrologJ compiler can be started in one of three ways. Note that these are similar to the ways that one starts the interpreter. When starting from the command line, the interpreter or compiler is run depending on whether or not any file(s) to compile are specified on the command line.

Regardless of how the compiler is started, the :- set_prolog_flag/3 directive may be included in the source code to specify any special flag settings needed.

If the compiler is started up by using the Compiler menu in the console window, the Library Components menu may be used to specify what predicates are considered to be built-in predicates in the code being compiled, and the Flags menu may be used to specify the initial settings of the flags, before actually running the compiler. Additional items in the Compiler menu control various other aspects of the compilation process. These are all documented in the documentation for the console window.

If the compiler is started up from the command line or a script, the following options may be specified. Each option name may be abbreviated to as little as just its first letter. Note that these options are similar to those available when starting the interpreter, but not identical. The compiler is started just when one or more files are specified on the command line; otherwise the interpreter is started.

If the compiler is started up from the command line or a script, each file must be a full specification for one file to be compiled. It must include the .pro extension. (This is consistent with the requirements for javac and most other language compilers.) The file is sought in the current working directory; if it is located somewhere else the parameter must include a suitable path specification.

Dealing with Code Size Limitations

When compiling a very large source file, it is possible that the resultant java file may cause a code size overflow error during compilation by javac. In this case, the source file should be broken up into smaller files that are compiled separately. (The main file can cause the others to be loaded by use of the :- ensure_loaded/1 directive.)

Copyright © 2005 - Russell C. Bjork. See the file See file COPYING in the root directory for copyright information.

Valid XHTML 1.0!