An iterative approach will not work
here, at least not in a DSL-based crawler generator. The smart iteration operators in
such a generator are too specialized: what we would really want is a lower-level dowhile
block. This would print out the command for the current object, traverse to the
next object, and repeat as long as that next object existed. In an API-based generator, it
would look something like Listing 11.7.
GENERATOR OUTPUT PATTERNS 287
Listing 11.7 API-based generator for sequential code.
do {
System.out.println(currObj.Command());
currObj = currObj.nextObject();
} while (currObj != null)
With a crawler we can use a recursive approach, as shown in Listing 11.8. We
output the command for the current object, then traverse any outgoing relationship to
the next object and recurse.
Listing 11.8 Crawler-based generator for sequential code.
Report 'handleObject'
:Command /* output for this object */
do ~From~To.() /* traverse to next object(s) */
{
subreport 'handleObject' run /* recurse */
}
These approaches will work providing there are no cycles: in simple languages like
this, cycles may well be forbidden by the modeling language anyway.Abene?¬?t of this
simplicity is the minimal program size and execution time of the resulting code: it
really is just a sequential list of commands.
Pages:
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534