packages feed

feldspar-compiler-0.2: Feldspar/Compiler/Plugins/PrettyPrint.hs

{-# LANGUAGE TypeFamilies #-}

module Feldspar.Compiler.Plugins.PrettyPrint where

import Feldspar.Compiler.PluginArchitecture
import Feldspar.Compiler.Options

-- ===========================================================================
--  == PrettyPrint plugin
-- ===========================================================================

instance Default IsRestrict where
    defaultValue = NoRestrict


instance Default IsDefaultArraySize where
    defaultValue = NoDefaultArraySize


data PrettyPrint = PrettyPrint


instance TransformationPhase PrettyPrint where
    type From PrettyPrint = ()
    type To PrettyPrint = PrettyPrintSemanticInfo
    type Downwards PrettyPrint = (IsRestrict, Int)
    type Upwards PrettyPrint = ()
    
    transformFormalParameter _ (platform,defArrSize) _ up =
        FormalParameter {
            formalParameterVariable =  addDefaultArraySizes v defArrSize,
            formalParameterSemInf = platform 
        }
      where
        v = recursivelyTransformedFormalParameterVariable up
    
    transformLocalDeclaration _ (_,defArrSize) _ up =
        LocalDeclaration {
            localDeclarationData = ldd{localVariable = addDefaultArraySizes v defArrSize},
            localDeclarationSemInf = () 
        }
      where
        ldd = recursivelyTransformedLocalDeclarationData up
        v = localVariable ldd


instance Plugin PrettyPrint where
    type ExternalInfo PrettyPrint = (Platform,Int)
    executePlugin PrettyPrint (platform,defArrSize) procedure = fst
        $ executeTransformationPhase PrettyPrint (isRestrict,defArrSize) procedure where
            isRestrict = case platform of
                C99    -> Restrict
                _     -> NoRestrict


addDefaultArraySizes :: (SemanticInfo t) => Variable t -> Int -> Variable t
addDefaultArraySizes v defArrSize = v{variableData = vd{variableType = addDefaultArraySizes' t}}
  where
    vd = variableData v
    t = variableType vd
    addDefaultArraySizes' (ImpArrayType (Norm n) t) = ImpArrayType (Norm n) $ addDefaultArraySizes' t
    addDefaultArraySizes' (ImpArrayType Undefined t)  = ImpArrayType (Defined defArrSize) $ addDefaultArraySizes' t
    addDefaultArraySizes' t                         = t