Rule with parameters

Rules can be parameterized, i.e. 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)

During execution of rule Lot, a new shape with shape symbol Footprint and float parameter "20" is generated. Also, the height parameter will have the value 20.

Note:

Arbitrary mathematical expressions can be built 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 as well as the Floor rule 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 makes sure that only the matching one is used during shape creation (i.e. during execution of the Lot rule above, a Footprint shape with a string parameter is created).

Note:

If no matching rule exists a new leaf is generated.


In this topic