LaTeX istings support

IN PROGRESS

When writing papers about occam, it is often useful to include source code examples. The Latex Listings Package knows how to format a wide range of languages, but not occam. This page contains a set of language definitions which you can use to enable occam syntax highlighting when writing documents in Latex and typeset sourcecode using the listings package.

% Definition for typesetting occam(-pi) source code
lstdefinelanguage{occam}
{
  keywords={
    PROC,PAR,SEQ,WHILE,
    ALT,CHAN,MOBILE,FOR,
    INITIAL,VAL,INT,BYTE,
    RESULT,INLINE,#PRAGMA,EXTERNAL,
    USE,#INCLUDE,REAL32,INT16,
    INT32,DATA,TYPE,PACKED,
    RECORD,#USE,IS,RETYPES},
  sensitive=true,
  comment=[l]{--},
  alsoletter={\#},
  alsodigit={.}, 
%  string=[b]",
}

The following definition is used to typeset a (subset) ofthe Transputer's instruction set.

% Definition for typesetting Transputer assembly code
lstdefinelanguage{tasm}
{
  keywords={
    AJW,BSUB,CALL,CJ,CSUB0,
    DIFF,ENDP,GETPRI,IN,J,LDC,LDL,LDLP,
    LDNL,LDTIMER,.LEND,.LOADLABADDR,MINT,.NOTPROCESS
    STARTP,STL,STNL}
  sensitive=true,
  comment=[l]{--},
  alsoletter={\#},
  alsodigit={.}, 
%  string=[b]",
}

To use these definitions, paste them into the preamble of your latex file (ie before the \begin{document}). You can also set your default language to occam by setting the language property to occam: \lstset{language=occam} in the preamble.

A super quick guide to using the listings package

Writing code

Using code from a file

Writing code snippets

You can insert code snippets into a sentence by using lstinline. This uses the default language, unless you specify another language using the options. For example:

An \occam process with one output channel carrying integers is defined as \lstinline{PROC a(CHAN INT q)}.
Processes can also have `normal' variable or constant parameters. For example, the following C function
\lstinline[language=c]{void foo(int a, const int b);} can be defined as
\lstinline[language=occam]{PROC foo(INT a, VAL INT b);}. One should not however that these are not entirely
equivalent, as C passes by value, and occam passes by reference. When compiling for the Transterpreter, the
 invocation of the above process could be implemented as follows: 
\lstinline[language=tasm]{LDL 4; LDLP 6; CALL L6} where b is at workspace location 4, a at workspace location 6 and foo at label L6

Writing code snippets in multiple languages

If you are writing code snippets in more than one language, it may be worth defining a few helper commands which will automatically format source code, using the right language defnition:

% Convenient lstinlines
\newcommand{\clstinline}[1]{\lstinline[language=c]!#1!}
\newcommand{\olstinline}[1]{\lstinline[language=occam]!#1!}
\newcommand{\talstinline}[1]{\lstinline[language=tasm]!#1!}

You can now write latex which highlights in the right language, just by using the correct convenience command, for example:

 The C \clstinline{switch} statement is anologous to the \occam \olistinline{CASE} statement,
 which is not to be confused with the \olstinline{CASE} or variant input which is performed on
 channels carrying variant protocols. The \olstinline{CASE} statement in \occam is
 however not used, as the syntax and semantics of the occam olstinline{IF} statement is 
 sufficient to express many common uses of \clstinline{switch} statements as used in C.

Which produces the following output: CLJ needs to provide a picture!