Quasi-Literal Expressions and Patterns in E

by way of "Mark S. Miller" <markm@caplet.com> mzukowski@bco.com
Mon, 19 Apr 1999 12:55:38 -0700


[Forwarded with permission.  --MarkM]

Your quasi-literal stuff is quite interesting.  I've been tackling a similar
but different problem for code generation with literate programming.  I'm
not sure if this is on-topic enough for e-lang, if so then feel free to
forward it.  The problem I'm solving is generating skeleton code that has
certain pieces filled in programmatically, just like a parser-generator
would do.  In fact I will probably use this when writing the E parser
generator for ANTLR.

This is all based on the syntax of noweb, a literate programming tool.  Each
"<<something>>=" starts a chunk definition, each <<something>> is a chunk
reference which is expanded as needed.  I've made my own "notangle"
processor which expands these chunks by either textual substitution or by
running the result through the JPython interpreter if "exec" is the last
word of the chunk name.  <<*>> is the "root" chunk where processing starts.
The beauty of literate programming is the non-linear nature of specifying a
program.  The main idea is that you can write a book in the order you want
to explain the program, but the actual running program is fully specified in
the text.  Thus your documentation is less likely to get out of sync with
your program.  And you can explain your program at precisely the level of
detail you wish.

If you were to allow a program to be made up of non-text, why force the
program to be linearly specified?  Or if it were all text, a chunk could
just as easily reference a file by name to be edited elsewhere and a smart
editor could make the connection easily.

This generates some Lout code to take a year's worth of calendars (which are
.ps files) and throw them on one huge page to print out of our plotter.
Don't worry about what the code generated actually does, but do get excited
about the heirarchical way you can structure code and the way that could
relate to quasi-literals by having chunks be of different types.

# this special chunk is run to initialize the interpreter
<<JPython Initialization>>=
import string, os

<<*>>=
@Include {calpicture.setup}
@Illustration {
	<<template loop exec>>
}


# this is Lout code to take two calendars, rotate and fit on same page with
margins between
# and ending 1 inch below, ready for more graphics
<<template>>=
/1ie
|1ie
90d @Rotate
@IncludeGraphic <<leftCal>>
|2ie
270d @Rotate
@IncludeGraphic <<rightCal>>
|71pe
/1ie

<<template loop exec>>=
# executed by the JPython interpreter
# loop over all the months, generating a
# template chunk each time
for x in range(1,12,2):
	leftCal = 'bcal%d.ps'%x
	rightCal = 'bcal%d.ps'%(x+1)
	tangler.notangle("template", os, indentation)  # feedback to the
"tangler" generating this document.


Monty