Shape declaration

A shape declaration gives the name of a shape, lists is parameters if there are any, and introduces the rules for drawing the shape. Each rule contains shape replacements, variable declarations, and control flow elements. In this example the shape LeftOrRightFlower is declared with no parameters and two rules are then declared for how to draw a LeftOrRightFlower.

If a shape has just one rule then it is OK to omit the rule keyword and just list the shape replacements after the shape declaration:

The loop line is an example of a control flow element.

= Randomness, Rule Weights, and Having Multiple Rules for a Shape =

If there is only one rule for all the shapes in a CFDG file then the result will always look the same. But when there is more than one rule for a given shape then Context Free/CFDG has to choose which one to use at random each time the shape is invoked. The probability that a particular rule will be used is proportional to its weight. The exact equation is the rule's weight divided by the sum of the rule weights for the given shape. Another use for multiple rules is to provide an ending condition to an infinite recursion:

Percentages in Rule Weights
In expressions, a percentage is simply a short-hand for a number divided by 100; for example, 9.6% is directly converted to 0.096. But percentages in rule weights are treated specially. If a shape's rules have a mixture of percentages and ordinary constants and the percentages add up to <100% then the probability for rules with percentages is exactly equal to the percentage value. Any left-over probability is divided among the remaining rules according to its weight. Context Free will issue a warning if the percentage weights sum to more than 100%, or they sum to 100% and there are non-percentage weights, or they sum to <100% and there are no non-percentage weights.

=Shape Parameters= A shape can have parameters that are defined when the shape is invoked (either in a shape replacement or in a variable declaration). The parameter values provided at invocation are stored with the shape's state and are bound to parameter variables when the shape is executed. For example, the 242 shapes declared in the i_curves.cfdg example can be reduced to one parameterized rule: