[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: are dylan macros Turing-complete?



Bruce Hoult <bruce@hoult.org> writes:

> I can't see any reason, though, that you can't make a Dylan macro
> that accepts this sort of syntax and outputs a suitably nested list,
> or some code that builds a tree at runtime.  You'd then have a
> function that walked the tree to actually emit the HTML.

I do exactly this in my Dylanlibs [1] libraries. See the dom-builder
library. Code using it looks like:

  with-standard-http-result(stream)
    let dom = 
      with-dom-builder()
        (html
          (head
            (title ["Test HTTP Server"])),
          (body
            (p ["Testing the Dylan HTTP Server."]),
            (p ((a href: "/formtest1") ["Form test 1"])),
            (p ((a href: "/formtest2") ["Form test 2"])),
            (p ["Counter: "], [*counter* := *counter* + 1])))
      end;
    print-html(dom, stream);

You can do loops and such:

     (p 
         ((table border: 1,
                 cellpadding: 3)
           (tr
               (th ["Update Time"]), 
               (th ["Close Time"]),
               (th ["Pool"]),
               [for(n from 0 below odds.first.odds-snapshot-dividends.size)
                  with-dom-builder(*current-dom-element*)
                    (th [format-to-string("%d", n + 1)])
                  end with-dom-builder;
                end for]),
          [for(os in odds, roi in roi-sequence)
             with-dom-builder(*current-dom-element*)
               (tr 
                 (td [time-to-string(os.odds-snapshot-update-time)]),
                 (td [time-to-string(os.odds-snapshot-close-time)]),
                 (td [money-to-string(os.odds-snapshot-pool-size)]),
                 [for(r in roi)
                    with-dom-builder(*current-dom-element*)
                      (td [float-to-formatted-string(r, decimal-places: 3)])
                    end with-dom-builder;                  
                  end for])
             end with-dom-builder;
           end for]))

It gets a bit unweildy for big things but you can factor things out
into their own functions quite well. I also generate SVG dynamically
from a Dylan web server using this.

with-dom-builder generates a simple document object model with
elements, attributes, etc. I have various 'walkers' that walk through
this model generating HTML, XML, SVG or whatever.

[1] http://dylanlibs.sf.net

Chris.
-- 
http://www.double.co.nz/dylan