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

Literate Programming in Scheme



Here is a tiny ``literate programming'' package for Scheme.  The basic idea is
that code sections are surrounded by |\ttcode| and |\endtt|.  Then
there are a couple of filters:  one to rip through a text and install the code
in indicated files, one just to filter the code out of a text, and a bit of
Chez scheme code to load through the appropriate filter.

There's not much here, but I've used it for writing a book chapter, and it
seemed to help a whole lot.

This all ought to run in LaTeX, except that it doesn't seem possible to hack
the |verbatim| environment to produce a |code| environment.  If anyone knows
how to do this, please let me know!


Mitchell Wand
College of Computer Science
Northeastern University
360 Huntington Avenue #161CN
Boston, MA 02115

CSNet:  wand@corwin.ccs.northeastern.edu

#!/bin/sh
#	This is a shell archive.
#	Run the file through sh to extract its contents.
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	lit-to-code
#	lit-to-code.tex
#	literate.tex
#	lit-load.tex
#	lit-to-scheme.tex
#	tt.tex
# This archive created: Thu Oct 19 09:35:04 1989
echo shar: extracting lit-to-code '(162 characters)'
sed 's/^XX//' << \SHAR_EOF > lit-to-code
XXawk '
XX/^%filename/ {print "filename:", $2; filename = $2;}
XX/^\\endtt/ {q = 0}
XX(q==1)  {print filename "< " $0;
XX	 print $0 > filename;}
XX/^\\ttcode/ {q=1}
XX' $*
XX
XX
XX
XX
SHAR_EOF
if test 162 -ne "`wc -c lit-to-code`"
then
echo shar: error transmitting lit-to-code '(should have been 162 characters)'
fi
echo shar: extracting lit-to-code.tex '(732 characters)'
sed 's/^XX//' << \SHAR_EOF > lit-to-code.tex
XX\input sections
XX\input tt
XX\def\ttcode{\ttnoi}
XX%filename lit-to-code.a
XX
XX\sectionbegin {\tt lit-to-code}
XX
XXThis is a small {\tt awk} program to filter ``literate'' programs and install
XXthem in files.  It uses the keyword {\tt \%filename} to mark the filename.
XXCode to be installed in the file is delimited by {\tt \\ttcode} and
XX{\tt \\endtt} (which must start at the beginning of the line).  Code between
XXthese markers is installed in the indicated file.  
XX
XXThe target file may be changed by just inserting a new {\tt
XX\%filename} command.  The shell script will also take multiple source files as
XXarguments. 
XX
XX\ttcode
XXawk '
XX/^%filename/ {filename = $2}
XX/^\\endtt/ {q = 0}
XX(q==1)  {print $0 > filename}
XX/^\\ttcode/ {q=1}
XX' $*
XX\endtt
XX
XX
XX
SHAR_EOF
if test 732 -ne "`wc -c lit-to-code.tex`"
then
echo shar: error transmitting lit-to-code.tex '(should have been 732 characters)'
fi
echo shar: extracting literate.tex '(231 characters)'
sed 's/^XX//' << \SHAR_EOF > literate.tex
XX%%% macros for a tiny ``literate programming'' environment for Scheme.  Code
XX%%% is delimited by \ttcode and \endtt (must be at beginning of line). 
XX
XX\input tt
XX\define\ttcode{\ttnoi}
XX
XX%%% this is hardly worth a file, n'est-ce pas?
SHAR_EOF
if test 231 -ne "`wc -c literate.tex`"
then
echo shar: error transmitting literate.tex '(should have been 231 characters)'
fi
echo shar: extracting lit-load.tex '(591 characters)'
sed 's/^XX//' << \SHAR_EOF > lit-load.tex
XX\input sections
XX\input tt
XX\def\ttcode{\ttnoi}
XX%filename lit-load.s
XX
XX\sectionbegin {\tt lit-load}
XX
XXThis is a small loader to load a ``literate'' scheme program.  It merely takes
XXthe input file and pipes it through a small filter program called {\tt
XXlit-to-scheme}. 
XX
XX\ttcode
XX(define lit-load
XX  (lambda (filename)
XX    (let* ((process-string
XX		(string-append "lit-to-scheme " filename))
XX	   (process (process process-string))
XX	   (port (car process)))
XX      (let loop ()
XX	(let ((inp (read port)))
XX	  (if (eof-object? inp)
XX	   '()
XX	    (begin
XX	      (eval inp)
XX	      (loop))))))))
XX\endtt
XX
XX\bye
SHAR_EOF
if test 591 -ne "`wc -c lit-load.tex`"
then
echo shar: error transmitting lit-load.tex '(should have been 591 characters)'
fi
echo shar: extracting lit-to-scheme.tex '(438 characters)'
sed 's/^XX//' << \SHAR_EOF > lit-to-scheme.tex
XX\input sections
XX\input tt
XX%filename lit-to-scheme
XX
XX\sectionbegin {\tt lit-to-scheme}
XX
XXThis is a small filter program for use with {\tt lit-load}.  It just pipes the
XXsegments between instances of {\tt \\ttcode} and {\tt \\endtt} to the standard
XXoutput.  It takes a single filename as as argument.
XX
XX{\bf Bug:}  It will not do anything clever about protections.
XX
XX\ttcode
XXawk '
XX/^\\endtt/ {q = 0}
XX(q==1)  
XX/^\\ttcode/ {q=1}
XX' $1
XX\endtt
XX
XX\bye
SHAR_EOF
if test 438 -ne "`wc -c lit-to-scheme.tex`"
then
echo shar: error transmitting lit-to-scheme.tex '(should have been 438 characters)'
fi
echo shar: extracting tt.tex '(2005 characters)'
sed 's/^XX//' << \SHAR_EOF > tt.tex
XX\catcode`\^^I=\active % tab = \space
XX\def^^I{\space}
XX
XX\def\begindisplay{\obeylines\startdisplay}
XX{\obeylines\gdef\startdisplay#1
XX  {\catcode`|^^M=5$$#1\halign\bgroup\indent##\hfil&&\qquad##\hfil\cr}}
XX\def\enddisplay{\crcr\egroup$$}
XX
XX\catcode`\|=\active
XX{\obeylines\gdef|{\ttverbatim\let^^M=\ \let|=\endgroup}}
XX
XX\chardef\other=12
XX\def\fullttverbatim{\begingroup \catcode`\\=\other \catcode`\{=\other
XX  \def^^I{\ \ \ \ \ \ \ \ } % tab = 8 spaces
XX  \catcode`\}=\other \catcode`\$=\other \catcode`\&=\other
XX  \catcode`\#=\other \catcode`\%=\other \catcode`\~=\other
XX  \catcode`\_=\other \catcode`\^=\other 
XX  \obeyspaces \obeylines \tt}
XX{\obeyspaces\gdef {\ }} % \obeyspaces now gives \ , not \space
XX
XX\def\semittverbatim{\begingroup \catcode`\\=\other
XX  \def^^I{\ \ \ \ \ \ \ \ } % tab = 8 spaces
XX  \catcode`\&=\other
XX  \catcode`\#=\other \catcode`\%=\other \catcode`\~=\other
XX  \obeyspaces \obeylines \tt}
XX
XX\let\ttverbatim=\fullttverbatim
XX
XX\def\endline{\leavevmode\endgraf}
XX
XX\def\begintt{$$\let\par=\endline \ttverbatim \parskip=0pt
XX  \catcode`\|=0 \rightskip=-5pc \ttfinish}
XX{\catcode`\|=0 |catcode`|\=\other % | is temporary escape character
XX  |obeylines % end of line is active
XX  |gdef|ttfinish#1^^M#2\endtt{#1|vbox{#2}|endgroup$$}}
XX
XX\def\tti{\let\par=\endline \ttverbatim \parskip=0pt
XX  \catcode`\|=0 \rightskip=-5pc \ttifinish}
XX
XX\def\ttnos{\let\par=\endline \ttverbatim \parskip=0pt
XX  \catcode`\|=0 \rightskip=-5pc \ttnosfinish}
XX
XX\def\beginttnoi{\begintt\parindent=0pt}
XX\def\ttnosi{\ttnos\parindent=0pt}
XX\def\ttnoi{\tti\parindent=0pt}
XX
XX\def\ttctr{$$\let\par=\cr \ttverbatim \parskip=0pt
XX  \catcode`\|=0 \rightskip=-5pc \ttctrfinish}
XX
XX{\catcode`\|=0 |catcode`|\=\other % | is temporary escape character
XX  |obeylines % end of line is active
XX  |gdef|ttifinish#1\endtt{|endline #1 |endline|endgroup|noindent} %
XX  |gdef|ttnosfinish#1\endtt{#1|endgroup|noindent} %
XX  |gdef|ttctrfinish#1^^M#2\endtt{#1|vbox{|halign{##|hfil|cr#2}}|endgroup$$}}
XX
XX\def\semitt{\let\ttverbatim=\semittverbatim}
XX
XX\def\ttcode{\ttnoi}
SHAR_EOF
if test 2005 -ne "`wc -c tt.tex`"
then
echo shar: error transmitting tt.tex '(should have been 2005 characters)'
fi
#	End of shell archive
exit 0