Menu

#3 Preprocessor support

open
nobody
None
5
2005-11-09
2005-11-09
No

preprocessor support in JAL

The main use of preprocessor support in jal is to write
macros, short
sequences of statements used quite often, but for which
a function call would
be overkill (or too expensive in some cases). Macros
should be used with
caution and moderation though, because it's easy to use
them improperly or to
get burned with unforeseen "side effects" of expansion.
A short, and silly, example could be:

silly-example.jal
------------------------------------------------------------------------

include f877_4
include jlib

pragma target fuses 0x3F32

disable_a_d_functions

var bit LED1 is pin_a0
var bit LED2 is pin_a1

pin_a0_direction = output
pin_a1_direction = output

#define CYCLE_1(ms) \
delay_10ms( (ms) ) \
LED1 = high \
LED2 = low

#define CYCLE_2(ms) \
delay_10ms( (ms) ) \
LED1 = low \
LED2 = high

forever loop
CYCLE_1(25)

CYCLE_2(25)
end loop

To use this example, we have to call the preprocessor
before jal.
For example:

$ cpp silly-example.jal silly-example-expanded.jal
$ jal silly-example-expanded.jal

This first command will process "silly-example.jal"
emitting its output in
"silly-example-expanded.jal". Then with the second
command we simply compile
the generated file.

If you like, I'll add a command line switch to jal to
invoke automatically the
preprocessor, thus avoiding the two separate commands
shown above.

Cheers,
Marco

--
Marco Pantaleoni

-- ------------------------------------------------
I've added that switch. Now if you run jal with the
"-P" switch, it will
invoke cpp for you.
Compiling the example of the previous message would become:

$ jal -P silly-example.jal

(jal will call cpp handling automatically the temporary
file creation and
removal).

You can also specify a different "cpp" passing a
command string to -P, as in:

$ jal -P/usr/local/bin/cpp silly-example.jal

you can pass complex commands quoting with simple or
double quotes the
command:

$ jal -P'/usr/local/bin/cpp -funny' silly-example.jal

Cheers,
Marco

Discussion


Log in to post a comment.

MongoDB Logo MongoDB