Changeset 9064 for src/Pugs/Compile
- Timestamp:
- 02/19/06 21:56:36 (3 years ago)
- Files:
-
- 1 modified
-
src/Pugs/Compile/Pugs.hs (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Compile/Pugs.hs
r9059 r9064 10 10 11 11 type Str = Str.FastString 12 type Comp a = WriterT aEval a12 type Comp a = WriterT [a] Eval a 13 13 14 14 class (Show x) => Compile x where 15 compile :: x -> Comp Str ing15 compile :: x -> Comp Str 16 16 compile x = fail ("Unrecognized construct: " ++ show x) 17 compileList :: [x] -> Comp Str ing17 compileList :: [x] -> Comp Str 18 18 compileList xs = do 19 19 xsC <- mapM compile xs 20 return $ "[" ++ joinMany xsC ++ "]"20 return $ Str.concat [bl, joinMany xsC, br] 21 21 22 joinMany :: [Str ing] -> String23 joinMany xs = concat (intersperse ", " (filter (not . null) xs))22 joinMany :: [Str] -> Str 23 joinMany xs = Str.join cm (filter (not . Str.null) xs) 24 24 25 25 instance (Compile x) => Compile [x] where 26 26 compile = compileList 27 27 28 29 28 instance Compile (Maybe Exp) where 30 compile Nothing = return "Nothing"29 compile Nothing = return $ Str.pack "Nothing" 31 30 compile (Just exp) = compWith "Just" [compile exp] 32 31 33 compWith :: String -> [Comp String] -> Comp String 32 pl, pr, bl, br :: Str 33 pl = Str.pack "(" 34 pr = Str.pack ")" 35 bl = Str.pack "[" 36 br = Str.pack "]" 37 cm = Str.pack ", " 38 39 ret :: String -> Comp Str 40 ret = return . Str.pack 41 42 compWith :: String -> [Comp Str] -> Comp Str 34 43 compWith con xs = do 35 44 xsC <- sequence xs 36 return $ "(" ++ unwords (con:["("++x++")" | x <- xsC]) ++ ")"45 return $ Str.concat [pl, Str.unwords (Str.pack con:concatMap (\x -> [pl, x, pr]) xsC), pr] 37 46 38 47 instance Compile Exp where … … 40 49 compWith "App" [compile exp1, compile exp2, compile exps] 41 50 compile (Syn syn exps) = do 42 compWith "Syn" [ret urn (show syn), compile exps, compile exps]51 compWith "Syn" [ret (show syn), compile exps] 43 52 compile (Ann ann exp) = do 44 compWith "Ann" [ret urn(show ann), compile exp]53 compWith "Ann" [ret (show ann), compile exp] 45 54 compile (Pad scope pad exp) = do 46 compWith "Pad" [ret urn(show scope), compile pad, compile exp]55 compWith "Pad" [ret (show scope), compile pad, compile exp] 47 56 compile (Stmts exp1 exp2) = do 48 57 compWith "Stmts" [compile exp1, compile exp2] 49 58 compile (Val val) = do 50 59 compWith "Val" [compile val] 51 compile exp = ret urn$ "(" ++ show exp ++ ")"60 compile exp = ret $ "(" ++ show exp ++ ")" 52 61 53 62 instance Compile Pad where 54 63 compile pad = do 55 64 symsC <- mapM compile syms 56 return $ "(mkPad [" ++ joinMany symsC ++ "])"65 return $ Str.concat [Str.pack "(mkPad [", joinMany symsC, Str.pack "])"] 57 66 where 58 67 syms = padToList pad 59 68 60 69 instance Compile (String, [(TVar Bool, TVar VRef)]) where 61 compile ((':':'*':_), _) = return []-- XXX - :*Bool etc; punt for now70 compile ((':':'*':_), _) = return Str.empty -- XXX - :*Bool etc; punt for now 62 71 compile (n, tvars) = do 63 tvarsC <- fmap (filter (not . null)) $ mapM compile tvars64 if null tvarsC then return []else do65 return $ "(" ++ show n ++ ", [" ++ joinMany tvarsC ++ "])"72 tvarsC <- fmap (filter (not . Str.null)) $ mapM compile tvars 73 if null tvarsC then return Str.empty else do 74 return $ Str.concat [pl, Str.pack (show n), Str.pack ", [", joinMany tvarsC, br, pr] 66 75 67 76 instance (Typeable a) => Compile (Maybe (TVar a)) where 68 compile = const . ret urn$ "Nothing"77 compile = const . ret $ "Nothing" 69 78 70 79 instance Compile (TVar Bool, TVar VRef) where 71 80 compile (fresh, tvar) = do 72 81 tvarC <- compile tvar 73 if null tvarC then return []else do82 if Str.null tvarC then return Str.empty else do 74 83 freshC <- compile fresh 75 return $ "(" ++ freshC ++ ", " ++ tvarC ++ ")"84 return $ Str.concat [pl, freshC, cm, tvarC, pr] 76 85 77 86 instance Compile Bool where 78 compile bool = ret urn$ "(" ++ show bool ++ ")"87 compile bool = ret $ "(" ++ show bool ++ ")" 79 88 80 89 instance Compile a => Compile (Map VStr a) where 81 compile map | Map.null map = ret urn$ "(Map.empty)"90 compile map | Map.null map = ret $ "(Map.empty)" 82 91 compile map = error (show map) 83 92 … … 86 95 val <- lift $ readIVar iv 87 96 valC <- compile val 88 return $ "(newScalar " ++ valC ++ ")"97 return $ Str.concat [Str.pack "(newScalar ", valC, pr] 89 98 90 99 instance (Typeable a, Compile a) => Compile (TVar a) where … … 92 101 vref <- liftIO $ atomically (readTVar fresh) 93 102 vrefC <- compile vref 94 if null vrefC then return []else do95 tv <- liftIO $ fmap ( ('t':) . show . hashUnique) newUnique96 tell $ tv ++ " <- liftSTM (newTVar " ++ vrefC ++ ");\n"103 if Str.null vrefC then return Str.empty else do 104 tv <- liftIO $ fmap (Str.pack . ('t':) . show . hashUnique) newUnique 105 tell [Str.concat [tv, Str.pack " <- liftSTM (newTVar ", vrefC, Str.pack ");\n"]] 97 106 return tv 98 107 … … 101 110 vsub <- lift $ code_fetch cv 102 111 vsubC <- compile vsub 103 if null vsubC then return []else do104 return $ "(MkRef (ICode " ++ vsubC ++ "))"112 if Str.null vsubC then return Str.empty else do 113 return $ Str.concat [Str.pack "(MkRef (ICode ", vsubC, pr, pr] 105 114 compile (MkRef (IScalar sv)) | scalar_iType sv == mkType "Scalar::Const" = do 106 115 sv <- lift $ scalar_fetch sv 107 116 svC <- compile sv 108 if null svC then return []else do109 return $ "(MkRef (IScalar " ++ svC ++ "))"117 if Str.null svC then return Str.empty else do 118 return $ Str.concat [Str.pack "(MkRef (IScalar ", svC, pr, pr] 110 119 compile ref = do 111 objc <- liftIO $ fmap ( ('o':) . show . hashUnique) newUnique112 tell $ objc ++ " <- newObject (mkType \"" ++ showType (refType ref) ++ "\");\n"120 objc <- liftIO $ fmap (Str.pack . ('o':) . show . hashUnique) newUnique 121 tell [Str.append objc (Str.pack (" <- newObject (mkType \"" ++ showType (refType ref) ++ "\");\n"))] 113 122 return objc 114 123 … … 118 127 compile (VObject obj) = do 119 128 compWith "VObject" [compile obj] 120 compile val = ret urn$ "(" ++ show val ++ ")"129 compile val = ret $ "(" ++ show val ++ ")" 121 130 122 131 instance Compile VObject where 123 132 compile (MkObject typ attrs Nothing _) = do 124 133 attrsC <- compile attrs 125 uniq <- liftIO $ fmap ( ('u':) . show . hashUnique) newUnique126 tell $ uniq ++ " <- liftIO newUnique;\n"127 return $ "(" ++ unwords ["MkObject", show typ, attrsC, "Nothing", uniq] ++ ")"134 uniq <- liftIO $ fmap (Str.pack . ('u':) . show . hashUnique) newUnique 135 tell [Str.append uniq (Str.pack " <- liftIO newUnique;\n")] 136 return $ Str.unwords [pl, Str.pack "MkObject", Str.pack (show typ), attrsC, Str.pack "Nothing", uniq, pr] 128 137 compile obj = fail $ "Cannot compile Object of Dynamic type: " ++ show obj 129 138 … … 131 140 instance Compile VCode where 132 141 -- compile MkCode{ subBody = Prim _ } = return $ text "return mkPrim" 133 compile MkCode{ subBody = Prim _ } = return []142 compile MkCode{ subBody = Prim _ } = return Str.empty 134 143 compile (MkCode v1 v2 v3 _ v4 v5 v6 v7 v8 v9 v10 _) = do 135 144 compWith "MkCode" 136 145 [ compile v1 137 , ret urn(show v2)138 , ret urn(show v3)139 , ret urn (show v4)140 , ret urn "Nothing"141 , ret urn(show v5)142 , ret urn(show v6)143 , ret urn(show v7)144 , ret urn(show v8)146 , ret (show v2) 147 , ret (show v3) 148 , ret "Nothing" 149 , ret (show v4) 150 , ret (show v5) 151 , ret (show v6) 152 , ret (show v7) 153 , ret (show v8) 145 154 , compile v9 146 155 , compile v10 147 , ret urn"Nothing"156 , ret "Nothing" 148 157 ] 149 158 … … 168 177 , " runAST glob exp" 169 178 , "" 170 , "globC = do {" ++ globT ++ "return " ++globC ++ "}"179 , "globC = do {" ++ Str.unpack (Str.concat globT) ++ "return " ++ Str.unpack globC ++ "}" 171 180 , "" 172 , "expC = do {" ++ expT ++ "return " ++expC ++ "}"181 , "expC = do {" ++ Str.unpack (Str.concat expT) ++ "return " ++ Str.unpack expC ++ "}" 173 182 , "" 174 183 ]
