next up previous index
Next: Variables Up: The object model Previous: Restrictions

Annotations

   Within a RainCode script, it is often useful to attach some information to elements of the parse tree. For instance, one can wish to attach to each variable declaration a list that contains all statements that refer to this variable in any way. Similarly, one can attach a measured complexity to each procedure, etc...

  While this can be done using list couples structured as associative arrays, RainCode provides a more convenient way to attach such information to elements of the parse tree. Quite simply, if p is a non-terminal, one can define an annotation named prop and give it the value 7 by executing:

p.prop := 7;

Annotations have several properties:

The last property raises a question: how does one know whether a given annotation has been defined for a non-terminal, since querying it if it is not defined results in a runtime error. The CAN statement must be used before accessing an annotation to check whether it has been defined or not. Typically, when using counters to measure some kind of complexity, the following kind of code would be used:

PROCEDURE Mark(p);
BEGIN
ASSERT SYS.IsNt(p);
IF NOT (p CAN Complexity) THEN
  p.Complexity := 0;
  END;
p.Complexity := p.Complexity + 1;
END Mark;


next up previous index
Next: Variables Up: The object model Previous: Restrictions