Rule with parameters

Rules can be parameterized; in other words, a signature with parameters can be defined, and the matching signature is chosen during generation.

Note:

No explicit parameter type is required. The CGA compiler automatically finds the type of the parameter. There are three types in the CGA grammar: float, Boolean, and string. The float type is used for all numbers (also integers). For each type there is also an array variant: float array, Boolean array, and string array.

Example 1

Lot               --> s('0.8,'1,'0.8)
                      center(xz) 
                      Footprint(20)
                      
Footprint(height) --> extrude(height*0.8)

When the Lot rule is run, a new shape is generated with a shape symbol of Footprint and a float parameter of 20. Also, the height parameter has a value of 20.

Note:

You can build arbitrary mathematical expressions with float parameters.

Example 2

Lot                    --> s('0.8,'1,'0.8) 
                           center(xz) 
                           Footprint(20,geometry.area)

Footprint(height, area) --> t(0,0,1) 
                           extrude(height) 
                           Envelope(area)

Envelope(area)         --> split(y) { ~4 : Floor(area) }*

The Footprint rule takes two parameters, and the Envelope and Floor rules take one parameter.

Note:

Notice how area is passed from rule to rule.

Example 3

Lot                    --> Footprint("just an example")

Footprint(height,area) --> t(0,0,1) 
                           extrude(height) 
                           Envelope(area)
                           
Footprint(text)        --> print(text)

Rule overloading is shown in example 3. There are two Footprint rules, one with two float parameters and one with one string parameter. The compiler automatically ensures that only the matching one is used during shape creation (in other words, when the Lot rule above is run, a Footprint shape with a string parameter is created).

Note:

If no matching rule exists, a new leaf is generated.


In this topic