Commit f00cc10b authored by Björn Peemöller 's avatar Björn Peemöller
Browse files

Implemented generic `showModule` for intermediate language

parent efb6ab8d
......@@ -83,6 +83,7 @@ Executable cymake
, Html.SyntaxColoring
, IL
, IL.Pretty
, IL.ShowModule
, IL.Type
, Imports
, Interfaces
......
......@@ -11,7 +11,8 @@
This module is a simple re-export of the definition of the AST of IL
and the pretty-printing of IL modules.
-}
module IL ( module IL.Type, ppModule ) where
module IL ( module IL.Type, ppModule, showModule ) where
import IL.Pretty (ppModule)
import IL.Pretty (ppModule)
import IL.ShowModule (showModule)
import IL.Type
{- |
Module : $Header$
Description : Custom Show implementation for IL
Copyright : (c) 2015 Björn Peemöller
License : OtherLicense
Maintainer : bjp@informatik.uni-kiel.de
Stability : experimental
Portability : portable
This module implements a generic show function comparable to the one
obtained by @deriving Show@. However, the internal representation of
identifiers is hidden to avoid syntactic clutter.
-}
module IL.ShowModule (showModule) where
import Data.Char (ord)
import Data.Generics (Data, extQ, ext1Q, ext2Q, gmapQ, toConstr, showConstr)
import Data.List (intersperse)
import Curry.Base.Ident
import IL.Type
showModule :: Module -> String
showModule m = gshowsPrec m []
gshowsPrec :: Data a => a -> ShowS
gshowsPrec
= genericShowsPrec
`ext1Q` showsList
`ext2Q` showsTuple
`extQ` (shows :: String -> ShowS)
`extQ` showsEscape
`extQ` showsModuleIdent
`extQ` showsIdent
`extQ` showsQualIdent
where
genericShowsPrec :: Data a => a -> ShowS
genericShowsPrec t
= showParen (not (null args))
$ showString (showConstr (toConstr t))
. (if null args then id else showChar ' ')
. foldr (.) id args
where args = intersperse (showChar ' ') (gmapQ gshowsPrec t)
showsList :: Data a => [a] -> ShowS
showsList xs = showChar '['
. foldr (.) (showChar ']')
(intersperse (showChar ',') (map gshowsPrec xs))
showsTuple :: (Data a, Data b) => (a, b) -> ShowS
showsTuple (x, y)
= showChar '(' . gshowsPrec x . showChar ',' . gshowsPrec y . showChar ')'
showsEscape :: Char -> ShowS
showsEscape c
| o < 10 = showString "'\\00" . shows o . showChar '\''
| o < 32 = showString "'\\0" . shows o . showChar '\''
| o == 127 = showString "'\\127'"
| otherwise = shows c
where o = ord c
showsModuleIdent :: ModuleIdent -> ShowS
showsModuleIdent m = showString (moduleName m)
showsIdent :: Ident -> ShowS
showsIdent i = showString (showIdent i)
showsQualIdent :: QualIdent -> ShowS
showsQualIdent q = showString (qualName q)
......@@ -57,22 +57,22 @@ import Curry.Base.Position (SrcRef(..), SrcRefOf (..))
import Base.Expr
data Module = Module ModuleIdent [ModuleIdent] [Decl]
deriving (Eq, Show)
deriving (Eq, Show, Data, Typeable)
data Decl
= DataDecl QualIdent Int [ConstrDecl [Type]]
| NewtypeDecl QualIdent Int (ConstrDecl Type)
| FunctionDecl QualIdent [Ident] Type Expression
| ExternalDecl QualIdent CallConv String Type
deriving (Eq, Show)
deriving (Eq, Show, Data, Typeable)
data ConstrDecl a = ConstrDecl QualIdent a
deriving (Eq, Show)
deriving (Eq, Show, Data, Typeable)
data CallConv
= Primitive
| CCall
deriving (Eq, Show)
deriving (Eq, Show, Data, Typeable)
data Type
= TypeConstructor QualIdent [Type]
......@@ -84,7 +84,7 @@ data Literal
= Char SrcRef Char
| Int SrcRef Integer
| Float SrcRef Double
deriving (Eq, Show)
deriving (Eq, Show, Data, Typeable)
data ConstrTerm
-- |literal patterns
......@@ -93,7 +93,7 @@ data ConstrTerm
| ConstructorPattern QualIdent [Ident]
-- |default
| VariablePattern Ident
deriving (Eq, Show)
deriving (Eq, Show, Data, Typeable)
data Expression
-- |literal constants
......@@ -118,18 +118,18 @@ data Expression
| Letrec [Binding] Expression
-- |typed expression
| Typed Expression Type
deriving (Eq, Show)
deriving (Eq, Show, Data, Typeable)
data Eval
= Rigid
| Flex
deriving (Eq, Show)
deriving (Eq, Show, Data, Typeable)
data Alt = Alt ConstrTerm Expression
deriving (Eq, Show)
deriving (Eq, Show, Data, Typeable)
data Binding = Binding Ident Expression
deriving (Eq, Show)
deriving (Eq, Show, Data, Typeable)
instance Expr Expression where
fv (Variable v) = [v]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment