Changeset 190
- 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
- docs/trunk/hacking-guide/tock-intro.tex (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
docs/trunk/hacking-guide/tock-intro.tex
r189 r190 404 404 \subsection{Generics} 405 405 406 %TODO 406 We use the \lstinline|Data.Generics| module of GHC to do our generics (also known as the Scrap Your 407 Boilerplate or SYB approach). The deep-down mechanics are very confusing. How to use it is simpler, 408 and is explained in the three excellent SYB papers. Things are made slightly tricky again because 409 we usually perform custom traversals. 410 411 The `everywhere' (or `everywhereM') function(s) described in the SYB papers traverse an entire tree 412 structure looking to apply your transformation. Unfortunately this includes examining each character 413 of each string in every Meta tag, which makes things (unacceptably) slow. Therefore Adam implemented 414 a custom traversal pattern. Since you will almost always want this traversal, you do not have to 415 worry too much about the internals, just about how to use it. 416 417 Here's an example; the wrapper from our previous code example: 418 419 \begin{lstlisting} 420 removeParAssign :: Data t => t -> PassM t 421 removeParAssign = 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 431 You should follow this template for any new passes you write. All you need to customise is the 432 name of the pass (of course), and change the type and name of the doProcess function if you 433 want to process something other than a process. For example: 434 435 \begin{lstlisting} 436 twiddleExpressions :: Data t => t -> PassM t 437 twiddleExpressions = 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 448 Note that you must include the last case for your doProcess/doExpression function; otherwise you 449 will get an error if your pattern-matches are not exhaustive (which they rarely will be). 450 The net effect is to apply doExpression to all expressions in the given AST, in an efficient manner. 451 In other words, it's a compiler pass that operates on the expressions in the tree. 407 452 408 453 \section{Add Your Own Code}
Note:
See TracChangeset
for help on using the changeset viewer.