Paul Seamons wrote:
Having read this bit and also the PoD you posted I was wondering if it
would be possible to take the op-tree you describe and compile to some
other language similar to the way that Jemplate works.

Absolutely.

The "optree" is an arrayref of arrayrefs.

Way back in TT1 we compiled the templates down to an optree which was evaluated at runtime. However, I didn't make any real attempt to optimise it (or didn't know how to at the time) and it ran kinda sluggish. When we switched to the compiled Perl for TT2 we got a big boost in speed. But looking at your numbers, it's obviously that the optree approach can be just as fast if not faster if done right.

The TT3 parser is split into the classic front/back end model. The front end generates an optree (similar but slightly different to yours), the back end then walks the tree to generate Perl code (or Javascript, Ruby, C, etc). The TT2 parse does a similar kind of thing, but it's all internalised within the YAPP grammar and is difficult to mess with. TT3 better defines the role of the optree, makes it more accessible and generalises both the front and backs end so you can plug in your own parser or code generator.

So we could easily plug in a different back end which effectively passes the optree back through (with a little bit of finalising along the way) for runtime evaluation.

In short, we should be able to make this kinda thing work in TT3, no problem.

    # node contains (0: DIRECTIVE,
    #                1: start_index,
    #                2: end_index,
    #                3: parsed tag details,
    #                4: sub tree for block types
# 5: continuation sub trees for sub continuation block types (elsif, else, etc)
    #                6: flag to capture next directive

In a similar vein, the TT3 parser creates one big structured expression for the parse tree. The general form is:

   [ type, @args ]

Or for a concrete example:

   x + 3    => [add => [variable => 'x'], [number => 3]]

(actually, that's more illustrative than accurate, but just think Lisp)

For directives, the first argument is a directive object which contains
any additional metadata (line numbers, etc.)

   INCLUDE header y=x+3

            => [$include_directive, [filename => 'header'],
                                    [args => [ ...etc... ]]]

Anyway, if you can send me a link to the code I'd love to take a good look through it and see what I can draw inspiration or borrow code from. There's some bits of the TT3 parser that I'm still not happy with, but this could give me the last big push!

Cheers
A

_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to