Changeset 5357 for src/Pugs/Compile/Pugs.hs
- Timestamp:
- 07/10/05 14:47:37 (4 years ago)
- svk:copy_cache_prev:
- 7349
- Files:
-
- 1 modified
-
src/Pugs/Compile/Pugs.hs (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Compile/Pugs.hs
r5356 r5357 8 8 import Pugs.Internals 9 9 import Text.PrettyPrint 10 #if !defined(PUGS_HAVE_PERL5) && !defined(PUGS_HAVE_PARROT) && defined(PUGS_HAVE_TH) && (__GLASGOW_HASKELL__ <= 604)11 import qualified Language.Haskell.TH as TH12 #endif13 10 14 11 class (Show x) => Compile x where … … 16 13 compile x = fail ("Unrecognized construct: " ++ show x) 17 14 compileList :: [x] -> Eval Doc 18 compileList = liftMprettyList . mapM compile15 compileList = fmap prettyList . mapM compile 19 16 20 17 instance (Compile x) => Compile [x] where … … 30 27 prettyDo docs = parens $ sep (text "do":punctuate semi docs) 31 28 32 #if !defined(PUGS_HAVE_PERL5) && !defined(PUGS_HAVE_PARROT) && defined(PUGS_HAVE_TH) && (__GLASGOW_HASKELL__ <= 604)33 29 prettyRecord :: String -> [(String, Doc)] -> Doc 34 30 prettyRecord con = (text con <+>) . braces . sep . punctuate comma . map assign 35 31 where assign (name, val) = text name <+> char '=' <+> val 36 #endif37 32 38 33 prettyBind :: String -> Doc -> Doc … … 40 35 41 36 37 instance Compile (Maybe Exp) where 38 compile Nothing = return $ text "return Nothing" 39 compile (Just exp) = do 40 expC <- compile exp 41 return $ prettyDo 42 [ prettyBind "exp" expC 43 , text "return (Just exp)" 44 ] 42 45 43 46 instance Compile Exp where 47 compile (App exp1 exp2 exps) = do 48 exp1C <- compile exp1 49 exp2C <- compile exp2 50 expsC <- compileList exps 51 return $ prettyDo 52 [ prettyBind "exp1" exp1C 53 , prettyBind "exp2" exp2C 54 , prettyBind "exps" (text "sequence" `sep1` expsC) 55 , text "return (App exp1 exp2 exps)" 56 ] 44 57 compile (Syn syn exps) = do 45 58 expsC <- compileList exps 46 59 return $ prettyDo 47 [ prettyBind "exps" (text "sequence" `sep1` expsC) 48 , text "return" <+> parens (text $ "Syn " ++ show syn ++ " exps") 49 ] 60 [ prettyBind "exps" (text "sequence" `sep1` expsC) 61 , text "return" <+> parens (text $ "Syn " ++ show syn ++ " exps") 62 ] 63 compile (Cxt cxt exp) = compileShow2 "Cxt" cxt exp 64 compile (Pos pos exp) = compileShow2 "Pos" pos exp 65 compile (Pad scope pad exp) = do 66 padC <- compile pad 67 expC <- compile exp 68 return $ prettyDo 69 [ prettyBind "pad" padC 70 , prettyBind "exp" expC 71 , text ("return (Pad " ++ show scope ++ " pad exp)") 72 ] 50 73 compile (Stmts exp1 exp2) = do 51 74 exp1C <- compile exp1 52 75 exp2C <- compile exp2 53 76 return $ prettyDo 54 [ prettyBind "exp1" exp1C 55 , prettyBind "exp2" exp2C 56 , text "return (Stmts exp1 exp2)" 57 ] 58 compile (Pad scope pad exp) = do 59 padC <- compile pad 60 expC <- compile exp 61 return $ prettyDo 62 [ prettyBind "pad" padC 63 , prettyBind "exp" expC 64 , text ("return (Pad " ++ show scope ++ " pad exp)") 65 ] 66 compile (Pos pos exp) = compileShow2 "Pos" pos exp 67 compile (Cxt cxt exp) = compileShow2 "Cxt" cxt exp 77 [ prettyBind "exp1" exp1C 78 , prettyBind "exp2" exp2C 79 , text "return (Stmts exp1 exp2)" 80 ] 81 compile (Val val) = do 82 valC <- compile val 83 return $ prettyDo 84 [ prettyBind "val" valC 85 , text "return (Val val)" 86 ] 68 87 compile exp = return $ text "return" $+$ parens (text $ show exp) 69 88 … … 117 136 vref <- liftSTM $ readTVar fresh 118 137 vrefC <- compile vref 119 return $ prettyDo 120 [ prettyBind "vref" vrefC121 , text "liftSTM (newTVar vref)"122 ]138 return $ prettyDo 139 [ prettyBind "vref" vrefC 140 , text "liftSTM (newTVar vref)" 141 ] 123 142 124 143 instance Compile VRef where 125 144 compile (MkRef (ICode cv)) = do 126 vsub <- code_fetch cv 127 vsubC <- compile vsub 128 return (text "return (MkRef " <> 129 parens (sep [text "ICode $ ", vsubC]) <> text ")") 130 compile (MkRef (IScalar sv)) | scalar_iType sv == mkType "Scalar::Const" 131 = do 132 sv <- scalar_fetch sv 145 vsub <- code_fetch cv 146 vsubC <- compile vsub 147 return $ prettyDo 148 [ prettyBind "vsub" vsubC 149 , text "return (MkRef $ ICode vsub)" 150 ] 151 compile (MkRef (IScalar sv)) | scalar_iType sv == mkType "Scalar::Const" = do 152 sv <- scalar_fetch sv 133 153 svC <- compile sv 134 return (text "return (MkRef " <> 135 parens (sep [text "IScalar $ ", svC]) <> text ")") 136 154 return $ prettyDo 155 [ prettyBind "sv" svC 156 , text "return (MkRef $ IScalar sv)" 157 ] 137 158 compile ref = do 138 159 return $ text $ "newObject (mkType \"" ++ showType (refType ref) ++ "\")" 139 160 140 161 instance Compile Val where 141 compile (VCode vc) = liftM ((text "VCode" <+>) . parens) $ compile vc 142 compile x = return $ text $ show x 162 compile (VCode code) = do 163 codeC <- compile code 164 return $ prettyDo 165 [ prettyBind "code" codeC 166 , text "return $ VCode code" 167 ] 168 compile x = return $ text "return" $+$ parens (text $ show x) 169 170 -- This wants a total rewrite. I strongly want Data.Generics at this point now. 143 171 144 172 -- Haddock can't cope with Template Haskell 145 173 instance Compile VCode where 146 #if !defined(HADDOCK) && !defined(PUGS_HAVE_PERL5) && !defined(PUGS_HAVE_PARROT) && defined(PUGS_HAVE_TH) && (__GLASGOW_HASKELL__ <= 604) 147 compile code | subType code == SubPrim = return $ text "mkPrim" 174 compile code | subType code == SubPrim = return $ text "return mkPrim" 148 175 compile code = do 149 return $ prettyRecord "MkCode" $ 150 $(liftM TH.ListE $ 151 mapM (\name -> [|(name, tshow $ 152 $(TH.varE $ TH.mkName name) code{ subEnv = Nothing })|]) $ 153 ["isMulti", "subName", "subType", "subEnv", "subAssoc", 154 "subParams", "subBindings", "subSlurpLimit", 155 "subReturns", "subLValue", "subCont"]) 156 ++ 157 [] 158 where 159 tshow :: Show a => a -> Doc 160 tshow = text . show 161 #else 162 compile code | subType code == SubPrim = return $ text "mkPrim" 163 compile code = return $ text $ show code{ subEnv = Nothing } 164 #endif 176 bodyC <- compile $ subBody code 177 let comp :: Show a => (VCode -> a) -> Doc 178 comp f = text $ show (f code) 179 vsub = prettyRecord "MkCode" $ 180 [ ("isMulti", comp isMulti) 181 , ("subName", comp subName) 182 , ("subType", comp subType) 183 , ("subEnv", text "Nothing") 184 , ("subAssoc", comp subAssoc) 185 , ("subParams", comp subParams) 186 , ("subBindings", comp subBindings) 187 , ("subSlurpLimit", comp subSlurpLimit) 188 , ("subReturns", comp subReturns) 189 , ("subLValue", comp subLValue) 190 , ("subBody", text "body") 191 , ("subCont", text "Nothing") 192 ] 193 return $ prettyDo 194 [ prettyBind "body" bodyC 195 , text "return" <+> parens vsub 196 ] 165 197 166 198 genPugs :: Eval Val
