$Id: CoreLangUnparser.lhs,v 1.2 1998/06/05 21:15:24 leavens Exp $

AUTHOR: Gary T. Leavens

This Haskell module defines an unparser (printer)
for Dave Schmidt's "Core Language"; see chapter 1 of
"The Structure of Typed Programming Languages" (MIT Press, 1994).

> module CoreLangUnparser where
> import CoreLangParser
> import Numeric (showInt)

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

> 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"

> 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 (e1 `Lt` e2) =
>              showString "(" . showsPrec p e1 .
>              showString " < " . showsPrec p e2 . showString ")"
>    showsPrec p (e1 `Gt` e2) =
>              showString "(" . showsPrec p e1 .
>              showString " > " . showsPrec p e2 . showString ")"
>    showsPrec p (e1 `Le` e2) =
>              showString "(" . showsPrec p e1 .
>              showString " <= " . showsPrec p e2 . showString ")"
>    showsPrec p (e1 `Ge` e2) =
>              showString "(" . showsPrec p e1 .
>              showString " >= " . showsPrec p e2 . showString ")"
>    showsPrec p (e1 `And` e2) =
>              showString "(" . showsPrec p e1 .
>              showString " and " . showsPrec p e2 . showString ")"
>    showsPrec p (e1 `Or` e2) =
>              showString "(" . showsPrec p e1 .
>              showString " or " . showsPrec p e2 . showString ")"
>    showsPrec p (Not e) = showString "not " . showsPrec p e 
>    showsPrec p (BoolLit True) = showString "true"
>    showsPrec p (BoolLit False) = showString "false"

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

