Precompiler

 

Introduction

The precompiler is a Lisp function Library which provides the services of a tiny C preprocessor to Lisp programs.

The precompiler adds C-like pre-compiler directives to any source file. This allows C-like conditional compilation of Lisp source files depending upon the settings of global variables in the workspace.


Example

#define _MSWIN true
#if _MSWIN
(callMSWindows arg1 arg2 arg3) ;; Do something in MSWindows only if we're in Windows
#endif


The precompiler directives use Lisp S-expressions everywhere. This makes the precompiler directives easy and simple to use for Lisp programmers. Each compiler directive MUST start at the very beginning of the source line or it will NOT be recognized as a precompiler directive. For those precompiler directives requiring Lisp S-expressions, The entire remainder of the line is considered the Lisp S-expression. This means that, with the exceptions of Lisp comments, nothing else but the precompiler directive and the Lisp S-expression can be on the line.

Directives

The precompiler provides the following C-like precompiler directives to any source file.

precompiler

The precompiler precompiler function provides the C-like precompiler services to the Lisp compiler. The precompiler may be found in the Lisp function Libraries accompanying AIS. Once the precompiler Library has been compiled, the workspace is now ready to provide precompiler services.


Syntax

(precompiler lispSource)


#define

The precompiler #define directive defines a Lisp global variable during the prcompilation phase.


Syntax

#define name S-expression


When the precompiler encounters a #define directive, it evaluates the Lisp S-expression and sets the name equal to the result. For example, the directive #define _64Bit (= (expt 2.0 62) (* (expt 2.0 31) (expt 2.0 31))) sets the workspace global variable _64Bit to true or false depending upon whether or not the current context is running on a 64 bit or 32 bit computer.


The #define variable assignment happens immediately as soon as the directive is encountered. So the assignment is ready and available for the precompiler directives which may follow in the Lisp source code.

#ifdef

The precompiler #ifdef directive checks to see whether the specified global variable is #void. If NOT, the code following the #ifdef directive up to the next #endif directive is included; otherwise the code following the ifdef directive up to the next #endif directive is excluded


Syntax

#ifdef name


Example

#define _MSWIN true
#ifdef _MSWIN
(callMSWindows arg1 arg2 arg3) ;; Do something in MSWindows only if we're in Windows
#endif

#ifndef

The precompiler #ifndef directive checks to see whether the specified global variable is #void. If so, the code following the #ifndef directive up to the next #endif directive is included; otherwise the code following the ifndef directive up to the next #endif directive is excluded


Syntax

#ifndef name


Example

#ifndef _MSWIN
(callLinux arg1 arg2 arg3) ;; Do something in Linux only if we're NOT in Windows
#endif

#if

The precompiler #if directive checks to see whether the specified S-expression is true. If true, the code following the #if directive up to the next #endif directive is included; otherwise the code following the if directive up to the next #endif directive is excluded


Syntax

#if S-expression


Example

#define #define _64Bit (= (expt 2.0 62) (* (expt 2.0 31) (expt 2.0 31)))
#if _64Bit
(callTimer64 arg1) ;; Do something in 64bits only if we're in a 64 bit environment
#endif

#else

The precompiler #else adds if then else capability to the #if, #ifdef, and #ifndef directives. If the #else directive is present, then the code following the #else directive up to the next #endif directive is included or excluded as the else option.


Syntax

#else


Example

#define _MSWIN true
#ifdef _MSWIN
(callMSWindows arg1 arg2 arg3) ;; Do something in MSWindows only if we're in Windows
#else
(callLinux arg1 arg2 arg3) ;; Do something in Linux only if we're not in Windows
#endif

#endif

The precompiler #endif terminates the #if, #ifdef, and #ifndef directives. And incooperation with the #else directive is provides if then else capabilities to the #if, #ifdef, and #ifndef directives.


Syntax

#endif


Example

#define #define _64Bit (= (expt 2.0 62) (* (expt 2.0 31) (expt 2.0 31)))
#if _64Bit
(callTimer64 arg1) ;; Do something in 64bits only if we're in a 64 bit environment
#else
(callTimer32 arg1) ;; Do something in 32bits only if we're NOT in a 64 bit environment
#endif

evalPlisp

The function evalPlisp runs the specified Lisp source code through the preprocessor and then runs the Lisp compiler. The evalPLisp function is defined when the precompiler Library is compiled.


Syntax

(evalPLisp lispSource)


Example

(setq lispSource (fileReadAll sourceFileName)
(evalPLisp lispSource) ;; Run the code through the precompiler then evaluate

runPScript

The function runPScript loads the specified Lisp source file into memory, then runs the specified Lisp source code through the preprocessor and then runs the Lisp compiler. The runPScript function is defined when the precompiler Library is compiled.


Syntax

(runPScript lispSource)


Example

(runPScript sourceFileName) ;; Run the code through the precompiler then evaluate