$Id: WhileNUnparser.lhs,v 1.6 1998/06/05 21:15:13 leavens Exp $

AUTHOR: Gary T. Leavens

This Haskell module defines an unparser (printer) for
for the language "WhileN" of Chapter 2 of Dave Schmidt's book
"The Structure of Typed Programming Languages" (MIT Press, 1994).

> module WhileNUnparser where
> import WhileNParser
> import Numeric (showInt)

The show function is an overloaded function that can be used to output
programs, declartions, commands, expressions, etc.
in a nice way for human readers.

> instance Show Program where
>    showsPrec p (d `In` c) =
>       showsPrec p d . showString " in " . showsPrec p c

> instance Show Declaration where
>    showsPrec p (d1 `Also` d2) =
>      showString "(" . showsPrec p d1 . showString ", "
>                     . showsPrec p d2 . showString ")"
>    showsPrec p (d1 `Then` d2) =
>      showString "(" . showsPrec p d1 . showString "; "
>                     . showsPrec p d2 . showString ")"
>    showsPrec p (Fun i e) =
>      showString "fun " . showString i . showString " = " . showsPrec p e
>    showsPrec p (Const i e) =
>      showString "const " . showString i . showString " = " . showsPrec p e
>    showsPrec p (Proc i c) =
>      showString "proc " . showString i . showString " = " . showsPrec p c
>    showsPrec p (Var i t) =
>      showString "var " . showString i . showString ": " . showsPrec p t
>    showsPrec p (Alias i l) =
>      showString "alias " . showString i . showString " = " . showsPrec p l
>    showsPrec p (Class i t) =
>      showString "class " . showString i . showString " = " . showsPrec p t
>    showsPrec p (Module i d) =
>      showString "module " . showString i . showString " = { "
>                           . showsPrec p d . showString " }"

> instance Show TypeStructure where
>    showsPrec p NewInt = showString "newint"
>    showsPrec p (Record d) =
>      showString "record " . showsPrec p d . showString "end"
>    showsPrec p (TId x) = showsPrec p x

> instance Show Command where
>    showsPrec p (l `Assign` e) =
>      showsPrec p l . showString " := " . showsPrec p e
>    showsPrec p Skip = showString "skip"
>    showsPrec p (c1 `Semi` c2) =
>      showString "(". showsPrec p c1 . showString "; "
>                    . showsPrec p c2 . showString ")"
>    showsPrec p (If e c1 c2) =
>      showString "if " . showsPrec p e .
>      showString " then " . showsPrec p c1 .
>      showString " else " . showsPrec p c2 .
>      showString " fi"
>    showsPrec p (While e c) =
>      showString "while " . showsPrec p e .
>      showString " do " . showsPrec p c .
>      showString " od"
>    showsPrec p (Call x) =
>      showString "call " . showsPrec p x

> instance Show Expression where
>    showsPrec p (Num n) = showInt n
>    showsPrec p (Deref l) = showString "@" . showsPrec p l
>    showsPrec p (e1 `Plus` e2) =
>       showString "(" . showsPrec p e1 . showString " + "
>                      . showsPrec p e2 . showString ")"
>    showsPrec p (e1 `Equals` e2) =
>       showString "(" . showsPrec p e1 . showString " = "
>                      . showsPrec p e2 . showString ")"
>    showsPrec p (Not e) = showString "not " . showsPrec p e
>    showsPrec p (Ident x) = showsPrec p x
>    showsPrec p (BoolLit True) = showString "true"
>    showsPrec p (BoolLit False) = showString "false"

> instance Show Variable where
>    showsPrec p (Varref x) = showsPrec p x
>    showsPrec p (Loc i) = showString "loc " . showInt i

> instance Show IdentifierExpr where
>    showsPrec p (Name id) = showString id
>    showsPrec p (x `Dot` id) = showsPrec p x . showString "." . showString id

