Context Navigation


Changeset 190


Ignore:
Timestamp:
Jan 24, 2008, 6:41:23 PM (18 years ago)
Author:
neil.c.c.brown
Message:

Added an explanation of how to write a pass using the generics

File:
1 edited

Legend:

Unmodified
Added
Removed
  • docs/trunk/hacking-guide/tock-intro.tex

    r189 r190
    404404\subsection{Generics}
    405405
    406%TODO
    406We use the \lstinline|Data.Generics| module of GHC to do our generics (also known as the Scrap Your
    407Boilerplate or SYB approach). The deep-down mechanics are very confusing. How to use it is simpler,
    408and is explained in the three excellent SYB papers. Things are made slightly tricky again because
    409we usually perform custom traversals.
    410
    411The `everywhere' (or `everywhereM') function(s) described in the SYB papers traverse an entire tree
    412structure looking to apply your transformation. Unfortunately this includes examining each character
    413of each string in every Meta tag, which makes things (unacceptably) slow. Therefore Adam implemented
    414a custom traversal pattern. Since you will almost always want this traversal, you do not have to
    415worry too much about the internals, just about how to use it.
    416
    417Here's an example; the wrapper from our previous code example:
    418
    419\begin{lstlisting}
    420removeParAssign :: Data t => t -> PassM t
    421removeParAssign = doGeneric `extM` doProcess
    422 where
    423 doGeneric :: Data t => t -> PassM t
    424 doGeneric = makeGeneric removeParAssign
    425
    426 doProcess :: A.Process -> PassM A.Process
    427 doProcess (A.Assign m vs@(_:_:_) (A.ExpressionList _ es)) = ...
    428 doProcess p = doGeneric p
    429\end{lstlisting}
    430
    431You should follow this template for any new passes you write. All you need to customise is the
    432name of the pass (of course), and change the type and name of the doProcess function if you
    433want to process something other than a process. For example:
    434
    435\begin{lstlisting}
    436twiddleExpressions :: Data t => t -> PassM t
    437twiddleExpressions = doGeneric `extM` doExpression
    438 where
    439 doGeneric :: Data t => t -> PassM t
    440 doGeneric = makeGeneric twiddleExpressions
    441
    442 doExpression :: A.Expression -> PassM A.Expression
    443 doExpression (...) = ... -- First pattern match
    444 doExpression (...) = ... -- Second pattern match
    445 doExpression p = doGeneric p
    446\end{lstlisting}
    447
    448Note that you must include the last case for your doProcess/doExpression function; otherwise you
    449will get an error if your pattern-matches are not exhaustive (which they rarely will be).
    450The net effect is to apply doExpression to all expressions in the given AST, in an efficient manner.
    451In other words, it's a compiler pass that operates on the expressions in the tree.
    407452
    408453\section{Add Your Own Code}
Note: See TracChangeset for help on using the changeset viewer.

AltStyle によって変換されたページ (->オリジナル) /