ANN: Tutorial on how to use the C preprocessor with CFDG

Here you can discuss and share functionality improvements and helper programs to make Context Free better.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
User avatar
TorfusPolymorphus
Posts: 27
Joined: Sun Dec 28, 2008 2:16 pm
Contact:

ANN: Tutorial on how to use the C preprocessor with CFDG

Post by TorfusPolymorphus »

Macros and variables would come in handy when writing CFDG scripts. As they are not currently supported by the CFDG software, one has to use some workaround. One possible workaround is to use the C preprocessor. It turns out it works perfectly with CFDG. I've written a small shell script and a small tutorial for automating this. They are available from my website.

With the shell script, using cfdgpp instead of cfdg to compile your CFDG scripts will automatically preprocess them with the C preprocessor. You can thus write:

Code: Select all

startshape sun

#define SUN(A) rule sun { A * { r (360/A) } ray { h 0 b 1 sat 1 |h 60 } }

SUN(5)
SUN(6)
SUN(7)
SUN(8)
SUN(9)
SUN(10)

rule ray {
	CIRCLE { }
	ray { y .1 r 1 s .965 h .05| }
}

rule ray 0.15 {
	ray { flip 90 }
}
and it will render like

Code: Select all

startshape sun

rule sun { 5 * { r (360/5) } ray { h 0 b 1 sat 1 |h 60 } }
rule sun { 6 * { r (360/6) } ray { h 0 b 1 sat 1 |h 60 } }
rule sun { 7 * { r (360/7) } ray { h 0 b 1 sat 1 |h 60 } }
rule sun { 8 * { r (360/8) } ray { h 0 b 1 sat 1 |h 60 } }
rule sun { 9 * { r (360/9) } ray { h 0 b 1 sat 1 |h 60 } }
rule sun { 10 * { r (360/10) } ray { h 0 b 1 sat 1 |h 60 } }

rule ray {
	CIRCLE { }
	ray { y .1 r 1 s .965 h .05| }
}

rule ray 0.15 {
	ray { flip 90 }
}
All features of the C preprocessor can thus be used within your CFDG scripts.

Any bug reports/comments/ideas for improvements are greatly appreciated as always. Enjoy!

Post Reply