Changeset 9064 for src/Pugs/Compile

Show
Ignore:
Timestamp:
02/19/06 21:56:36 (3 years ago)
Author:
audreyt
Message:

* Pugs.Compile.Pugs: -CPugs now uses FastPackedString?;

the generated code is also much more compact (but we are
not using it for Prelude anymore.)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Compile/Pugs.hs

    r9059 r9064  
    1010 
    1111type Str = Str.FastString 
    12 type Comp a = WriterT a Eval a 
     12type Comp a = WriterT [a] Eval a 
    1313 
    1414class (Show x) => Compile x where 
    15     compile :: x -> Comp String 
     15    compile :: x -> Comp Str 
    1616    compile x = fail ("Unrecognized construct: " ++ show x) 
    17     compileList :: [x] -> Comp String 
     17    compileList :: [x] -> Comp Str 
    1818    compileList xs = do 
    1919        xsC <- mapM compile xs 
    20         return $ "[" ++ joinMany xsC ++ "]" 
     20        return $ Str.concat [bl, joinMany xsC, br] 
    2121 
    22 joinMany :: [String] -> String 
    23 joinMany xs = concat (intersperse ", " (filter (not . null) xs)) 
     22joinMany :: [Str] -> Str 
     23joinMany xs = Str.join cm (filter (not . Str.null) xs) 
    2424 
    2525instance (Compile x) => Compile [x] where 
    2626    compile = compileList 
    2727 
    28  
    2928instance Compile (Maybe Exp) where 
    30     compile Nothing = return "Nothing" 
     29    compile Nothing = return $ Str.pack "Nothing" 
    3130    compile (Just exp) = compWith "Just" [compile exp] 
    3231 
    33 compWith :: String -> [Comp String] -> Comp String 
     32pl, pr, bl, br :: Str 
     33pl = Str.pack "(" 
     34pr = Str.pack ")" 
     35bl = Str.pack "[" 
     36br = Str.pack "]" 
     37cm = Str.pack ", " 
     38 
     39ret :: String -> Comp Str 
     40ret = return . Str.pack 
     41 
     42compWith :: String -> [Comp Str] -> Comp Str 
    3443compWith con xs = do 
    3544    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] 
    3746 
    3847instance Compile Exp where 
     
    4049        compWith "App" [compile exp1, compile exp2, compile exps] 
    4150    compile (Syn syn exps) = do 
    42         compWith "Syn" [return (show syn), compile exps, compile exps] 
     51        compWith "Syn" [ret (show syn), compile exps] 
    4352    compile (Ann ann exp) = do 
    44         compWith "Ann" [return (show ann), compile exp] 
     53        compWith "Ann" [ret (show ann), compile exp] 
    4554    compile (Pad scope pad exp) = do 
    46         compWith "Pad" [return (show scope), compile pad, compile exp] 
     55        compWith "Pad" [ret (show scope), compile pad, compile exp] 
    4756    compile (Stmts exp1 exp2) = do 
    4857        compWith "Stmts" [compile exp1, compile exp2] 
    4958    compile (Val val) = do 
    5059        compWith "Val" [compile val] 
    51     compile exp = return $ "(" ++ show exp ++ ")" 
     60    compile exp = ret $ "(" ++ show exp ++ ")" 
    5261 
    5362instance Compile Pad where 
    5463    compile pad = do 
    5564        symsC <- mapM compile syms 
    56         return $ "(mkPad [" ++ joinMany symsC ++ "])" 
     65        return $ Str.concat [Str.pack "(mkPad [", joinMany symsC, Str.pack "])"] 
    5766        where 
    5867        syms = padToList pad 
    5968 
    6069instance Compile (String, [(TVar Bool, TVar VRef)]) where 
    61     compile ((':':'*':_), _) = return [] -- XXX - :*Bool etc; punt for now 
     70    compile ((':':'*':_), _) = return Str.empty -- XXX - :*Bool etc; punt for now 
    6271    compile (n, tvars) = do 
    63         tvarsC <- fmap (filter (not . null)) $ mapM compile tvars 
    64         if null tvarsC then return [] else do 
    65         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] 
    6675 
    6776instance (Typeable a) => Compile (Maybe (TVar a)) where 
    68     compile = const . return $ "Nothing" 
     77    compile = const . ret $ "Nothing" 
    6978 
    7079instance Compile (TVar Bool, TVar VRef) where 
    7180    compile (fresh, tvar) = do 
    7281        tvarC  <- compile tvar 
    73         if null tvarC then return [] else do 
     82        if Str.null tvarC then return Str.empty else do 
    7483        freshC <- compile fresh 
    75         return $ "(" ++ freshC ++ ", " ++ tvarC ++ ")" 
     84        return $ Str.concat [pl, freshC, cm, tvarC, pr] 
    7685 
    7786instance Compile Bool where 
    78     compile bool = return $ "(" ++ show bool ++ ")" 
     87    compile bool = ret $ "(" ++ show bool ++ ")" 
    7988 
    8089instance Compile a => Compile (Map VStr a) where 
    81     compile map | Map.null map = return $ "(Map.empty)" 
     90    compile map | Map.null map = ret $ "(Map.empty)" 
    8291    compile map = error (show map)  
    8392 
     
    8695        val     <- lift $ readIVar iv 
    8796        valC    <- compile val 
    88         return $ "(newScalar " ++ valC ++ ")" 
     97        return $ Str.concat [Str.pack "(newScalar ", valC, pr] 
    8998 
    9099instance (Typeable a, Compile a) => Compile (TVar a) where 
     
    92101        vref    <- liftIO $ atomically (readTVar fresh) 
    93102        vrefC   <- compile vref 
    94         if null vrefC then return [] else do 
    95         tv      <- liftIO $ fmap (('t':) . show . hashUnique) newUnique 
    96         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"]] 
    97106        return tv 
    98107 
     
    101110        vsub    <- lift $ code_fetch cv 
    102111        vsubC   <- compile vsub 
    103         if null vsubC then return [] else do 
    104         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] 
    105114    compile (MkRef (IScalar sv)) | scalar_iType sv == mkType "Scalar::Const" = do 
    106115        sv  <- lift $ scalar_fetch sv 
    107116        svC <- compile sv 
    108         if null svC then return [] else do 
    109         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] 
    110119    compile ref = do 
    111         objc   <- liftIO $ fmap (('o':) . show . hashUnique) newUnique 
    112         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"))] 
    113122        return objc 
    114123 
     
    118127    compile (VObject obj) = do 
    119128        compWith "VObject" [compile obj] 
    120     compile val = return $ "(" ++ show val ++ ")" 
     129    compile val = ret $ "(" ++ show val ++ ")" 
    121130 
    122131instance Compile VObject where 
    123132    compile (MkObject typ attrs Nothing _) = do 
    124133        attrsC <- compile attrs 
    125         uniq   <- liftIO $ fmap (('u':) . show . hashUnique) newUnique 
    126         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] 
    128137    compile obj = fail $ "Cannot compile Object of Dynamic type: " ++ show obj 
    129138 
     
    131140instance Compile VCode where 
    132141    -- compile MkCode{ subBody = Prim _ } = return $ text "return mkPrim" 
    133     compile MkCode{ subBody = Prim _ } = return [] 
     142    compile MkCode{ subBody = Prim _ } = return Str.empty 
    134143    compile (MkCode v1 v2 v3 _ v4 v5 v6 v7 v8 v9 v10 _) = do  
    135144        compWith "MkCode" 
    136145            [ compile v1 
    137             , return (show v2) 
    138             , return (show v3) 
    139             , return (show v4) 
    140             , return "Nothing" 
    141             , return (show v5) 
    142             , return (show v6) 
    143             , return (show v7) 
    144             , return (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) 
    145154            , compile v9 
    146155            , compile v10 
    147             , return "Nothing" 
     156            , ret "Nothing" 
    148157            ] 
    149158 
     
    168177        , "    runAST glob exp" 
    169178        , "" 
    170         , "globC = do {" ++ globT ++ "return " ++ globC ++ "}" 
     179        , "globC = do {" ++ Str.unpack (Str.concat globT) ++ "return " ++ Str.unpack globC ++ "}" 
    171180        , "" 
    172         , "expC = do {" ++ expT ++ "return " ++ expC ++ "}" 
     181        , "expC = do {" ++ Str.unpack (Str.concat expT) ++ "return " ++ Str.unpack expC ++ "}" 
    173182        , "" 
    174183        ]