|
Ethiopia - Scripts |
|
I originally applied script functions only when the named values in a template or page header had a special syntax, something like that of the named macros. When I was about to pour a named string into the template, I'd check to see if its value looked like "@Menu()" - if it did, I'd call the Menu() function and place its result into the page. (Ultimately, I also let functions be called directly from the page source, just like the named macros, but this came after I had added true scripting.) I resisted moving 'smarts' into the template program, but in the end I had three hardwired functions that needed the sort of looping and testing that you can't do with the Format() pattern language. Since one of those functions was used only on one group of pages and since it began to seem increasingly restrictive to insist that every group of pages use the same type of menu, like the one on the left, with the outline expanded to show the current page's position, I finally (Feb-03) wrote a full-fledged script compiler, so that all the configuration of a group of pages is in its template. I used Coco/R to build a recursive descent parser from an 'annotated grammar'; the one pass compiler generates .Net CIL straight from the parser, using the Reflection.Emit interface. The compiled scripts are added to the same symbol table as the named strings, and execute as if they were methods of the symbol table object: they can read any named string, and they can create and manipulate objects in the System namespaces. (This means that they can read and write files, use regexes, and the like.) This was a lot of fun - I'd always wanted to write a compiler - and while it's not really 'done' (I took a few shortcuts that I'd like to handle 'properly' before calling it done) it's quite useful. It can easily handle all three of the functions I once had to hardwire into the template system itself, and has let me do things like write functions that are only used on one page. (In the near future, I'll probably replace the hardwired Back and Next links in this group of expanded Ethiopia pages with script functions.) |
||
Created on May 10, 2003, last updated May 12, 2003
Contact jon@midnightbeach.com
|