title: Translating a declarative DSL into nested function calls
body: <p>I have a python library which builds special iterators (a behavior tree) out of <a href="https://github.com/eykd/owyl/blob/master/examples/boids.py#L281" rel="nofollow">nested function calls</a>. While the API has a fairly nice and light-weight syntax (due to it being python), it could really use a declarative DSL.</p>
  
  <p>Here's a rough sketch of what I'm envisioning:</p>
  
  <p>The DSL (using YAML):</p>
  
  <pre><code>tree:
    - sequence:
      - do_action1
      - do_action2
      - select:
        - do_action3
        - sequence:
          - do_action4
          - do_action5
        - do_action6
  </code></pre>
  
  <p>would result in the following nested function calls:</p>
  
  <pre><code>visit(
      sequence(
          do_action1(),
          do_action2(),
          select(
              do_action3(),
              sequence(
                  do_action4(),
                  do_action5(),
                  ),
              do_action6(),
              )
          )
      )
  </code></pre>
  
  <p>I'm having trouble visualizing exactly how to do this. Because the DSL must represent a tree, a simple depth-first traversal seems appropriate. But in order to build the nested function calls, I have to turn this inside out somehow. It probably involves something clever with an intermediary stack or some-such, but I can't quite grasp it. What's the correct way to perform this transformation?</p>