liveBook · Manning

Dependency ordering

As explained in Building User-Friendly DSLs

Dependency order is an ordering of attributes in which no attribute precedes an attribute on which it depends.

Dependency order arranges attributes so that no attribute appears before another attribute it depends on. This ordering lets generated initialization assignments execute safely. section 9.3.4

Apply the ordering rule as follows:

  • Reverse each dependency chain: for 1 → 2, initialize 2 before 1. section 9.3.4
  • Visit dependencies before adding the current attribute to the ordered list. section 9.3.4
  • Use topological sorting to process every attribute, including attributes in incomplete or independent chains. section 9.3.4

Correcting generated initialization

If attribute1 refers to attribute2, generating attribute1 first is out of order. The corrected Rental initialization is: attribute2 = 42, then attribute1 = this.attribute2. section 9.3.4

Listing 9.5 The initial version of the dependencyOrderOf function
const { referencedAttributesInValueOf } = require("../language/queries")

const dependencyOrderOf = (attributes) => {
    const ordered = []           #1

    const visit = (current, chain) => {     #2
        if (ordered.indexOf(current) > -1) {    #3
            return
        }
        const extendedChain = [ ...chain, current ]      #4
        referencedAttributesInValueOf(current).forEach(
            (dependency) =>
                 visit(dependency, extendedChain)   #5
        )
        ordered.push(current)          #6
    }

    attributes.forEach(
        (attribute) => visit(attribute, [])    #7
    )

    return ordered
}
module.exports.dependencyOrderOf = dependencyOrderOf

The recursive implementation tracks visited attributes, follows referenced attributes first, and appends each attribute afterward. section 9.3.4

How Building User-Friendly DSLs covers it

How deep it goes
Defines the ordering rule, reverses dependency chains, and implements recursive topological sorting without detailing cycle handling.
Examples
Uses Rental attributes where attribute1 depends on attribute2, requiring generated assignments to reverse their original order.
What it assumes
Assumes attribute initial values and dependency relationships, while defining dependency order and chains in context.
What you can do after
Build initialization ordering by recursively visiting referenced attributes before emitting each attribute's assignment.
How it connects
Connects attribute dependencies with dependency chains, topological sorting, and initialization assignments.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.