Changeset 7843 for src/Pugs/Compile
- Timestamp:
- 11/05/05 11:41:57 (3 years ago)
- Files:
-
- 1 modified
-
src/Pugs/Compile/Pugs.hs (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Compile/Pugs.hs
r5360 r7843 1 {-# OPTIONS_GHC -cpp -fglasgow-exts -fth#-}1 {-# OPTIONS_GHC -cpp -fglasgow-exts #-} 2 2 3 3 #include "../pugs_config.h" … … 8 8 import Pugs.Internals 9 9 import Text.PrettyPrint 10 import qualified Data.Map as Map 10 11 11 12 class (Show x) => Compile x where … … 25 26 26 27 prettyDo :: [Doc] -> Doc 27 prettyDo docs = parens $ sep (text "do":punctuate semi docs)28 prettyDo docs = parens $ text "do" <+> braces (sep $ punctuate semi docs) 28 29 29 30 prettyRecord :: String -> [(String, Doc)] -> Doc … … 32 33 33 34 prettyBind :: String -> Doc -> Doc 34 prettyBind var doc = text var `sep1` nest 1 (text "<-" <+> doc) 35 35 prettyBind var doc = text var <+> text "<-" <+> doc 36 36 37 37 instance Compile (Maybe Exp) where … … 127 127 ] 128 128 129 instance Compile (TVar Bool) where 130 compile fresh = do 131 bool <- liftSTM $ readTVar fresh 132 return $ text "liftSTM" <+> parens (text "newTVar" <+> text (show bool)) 133 134 instance Compile (TVar VRef) where 129 instance Compile Bool where 130 compile bool = return $ text "return" <+> parens (text $ show bool) 131 132 instance Compile a => Compile (Map VStr a) where 133 compile map | Map.null map = return (text "return Map.empty") 134 compile map = error (show map) 135 136 instance Compile (IVar VScalar) where 137 compile iv = do 138 val <- readIVar iv 139 valC <- compile val 140 return $ prettyDo 141 [ prettyBind "val" valC 142 , text "newScalar val" 143 ] 144 145 instance (Typeable a, Compile a) => Compile (TVar a) where 135 146 compile fresh = do 136 147 vref <- liftSTM $ readTVar fresh … … 169 180 , text "return $ VCode code" 170 181 ] 182 compile (VObject obj) = do 183 objC <- compile obj 184 return $ prettyDo 185 [ prettyBind "obj" objC 186 , text "return $ VObject obj" 187 ] 171 188 compile x = return $ text "return" $+$ parens (text $ show x) 172 189 173 -- We need a compile VObject! 190 instance Compile VObject where 191 compile (MkObject typ attrs Nothing _) = do 192 attrsC <- compile attrs 193 let vobj = prettyRecord "MkObject" $ 194 [ ("objType", text (show typ)) 195 , ("objAttrs", text "attrs") 196 , ("objOpaque", text "Nothing") 197 , ("objId", text "id") 198 ] 199 return $ prettyDo 200 [ prettyBind "attrs" attrsC 201 , prettyBind "id" (text "liftIO newUnique") 202 , text "return" <+> parens vobj 203 ] 204 compile obj = fail $ "Cannot compile Object of Dynamic type: " ++ show obj 174 205 175 206 -- Haddock can't cope with Template Haskell … … 207 238 expC <- compile exp 208 239 return . VStr . unlines $ 209 [ "{-# OPTIONS_GHC -fglasgow-exts -fno-warn-unused-imports -fno-warn-unused-binds -O#-}"210 , "module Main CCwhere"240 [ "{-# OPTIONS_GHC -fglasgow-exts -fno-warn-unused-imports -fno-warn-unused-binds #-}" 241 , "module Main where" 211 242 , "import Pugs.Run" 212 243 , "import Pugs.AST" 213 244 , "import Pugs.Types" 214 245 , "import Pugs.Internals" 215 , "" 216 , "mainCC = do" 246 , "import qualified Data.Map as Map" 247 , "" 248 , "main = do" 217 249 , " glob <- globC" 218 250 , " exp <- expC"
