Changeset 2401

Show
Ignore:
Timestamp:
04/27/05 17:17:20 (4 years ago)
Author:
autrijus
svk:copy_cache_prev:
3914
Message:

* compile subroutines to Parrot.

Location:
src/Pugs
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/AST.hs

    r2397 r2401  
    718718 
    719719type Eval x = ContT Val (ReaderT Env IO) x 
     720 
     721runEval :: Env -> Eval Val -> IO Val 
     722runEval env eval = withSocketsDo $ do 
     723    my_perl <- initPerl5 "" 
     724    val <- (`runReaderT` env) $ do 
     725        (`runContT` return) $ 
     726            resetT eval 
     727    freePerl5 my_perl 
     728    return val 
    720729 
    721730findSymRef :: (MonadIO m) => String -> Pad -> m VRef 
  • src/Pugs/Compile.hs

    r2396 r2401  
    1212module Pugs.Compile where 
    1313import Pugs.AST 
     14import Pugs.Internals 
    1415import Pugs.Compile.Pugs (genPugs) 
    1516import Pugs.Compile.Parrot (genPIR) 
     
    1718 
    1819compile :: String -> Env -> IO String 
    19 compile "Haskell" = genGHC 
    20 compile "Pugs" = genPugs 
    21 compile "Parrot" = genPIR 
    22 compile s = const (error $ "Cannot compile to " ++ s) 
     20compile "Haskell" env = fmap vCast $ runEval env genGHC 
     21compile "Pugs"    env = fmap vCast $ runEval env genPugs 
     22compile "Parrot"  env = fmap vCast $ runEval env genPIR 
     23compile s _ = fail $ "Cannot compile to " ++ s 
    2324 
  • src/Pugs/Compile/Haskell.hs

    r2400 r2401  
    1515import Pugs.Prim 
    1616 
    17 genGHC :: Env -> IO String 
    18 genGHC Env{ envBody = exp } = 
    19     TH.runQ [d| mainCC = runComp $(compile exp) |] >>= \str -> return . unlines $ 
     17genGHC :: Eval Val 
     18genGHC = do 
     19    exp <- asks envBody 
     20    liftIO (TH.runQ [d| mainCC = runComp $(compile exp) |]) >>= \str -> return . VStr . unlines $ 
    2021        [ "{-# OPTIONS_GHC -fglasgow-exts -fth -O #-}" 
    2122        , "module MainCC where" 
  • src/Pugs/Compile/Parrot.hs

    r2396 r2401  
    55import Pugs.Pretty 
    66import Pugs.AST 
     7import Pugs.Types 
    78import Data.HashTable 
    89import Text.PrettyPrint 
     10import qualified Pugs.Types.Scalar as Scalar 
     11import qualified Pugs.Types.Code   as Code 
    912 
    1013-- XXX This compiler needs a totaly rewrite using Parrot AST, 
    1114-- XXX and maybe TH-based AST combinators 
    1215 
    13 genPIR :: Env -> IO String 
    14 genPIR Env{ envBody = exp } = return . unlines $ 
    15     [ "#!/usr/bin/env parrot" 
    16     , ".sub main @MAIN" 
    17     , "" 
    18     , renderStyle (Style LeftMode 0 0) (compile exp) 
    19     , ".end" 
    20     ] 
     16genPIR :: Eval Val 
     17genPIR = do 
     18    Env{ envBody = exp, envGlobal = globRef } <- ask 
     19 
     20    glob <- liftIO $ readIORef globRef 
     21 
     22    -- get a list of functions 
     23    init <- compileEval glob 
     24 
     25    return . VStr . unlines $ 
     26        [ "#!/usr/bin/env parrot" 
     27        , renderStyle (Style PageMode 0 0) init 
     28        , renderStyle (Style PageMode 0 0) $ vcat 
     29            [ text ".sub main @MAIN" 
     30            , nest 4 (compile exp) 
     31            , text ".end" 
     32            ] 
     33        ] 
     34 
     35instance Compile Doc where 
     36    compile = id 
     37 
     38instance Compile Pad where 
     39    compileEval pad = fmap vcat $ mapM compileEval (padToList pad) 
     40 
     41instance Compile (String, [IORef VRef]) where 
     42    compileEval (('&':name), [sym]) = do 
     43        imc <- compileEval sym 
     44        return $ vcat 
     45            [ text (".sub \"" ++ name ++ "\"") 
     46            , nest 4 imc 
     47            , text ".end" 
     48            ] 
     49    compileEval x = internalError ("Unrecognized construct: " ++ show x) 
     50 
     51instance Compile (IORef VRef) where 
     52    compileEval x = do 
     53        ref <- liftIO $ readIORef x 
     54        compileEval ref 
     55 
     56instance Compile VRef where 
     57    compileEval (MkRef (ICode cv)) = do 
     58        vsub <- Code.fetch cv 
     59        compileEval vsub 
     60    compileEval (MkRef (IScalar sv)) 
     61        | Scalar.iType sv == mkType "Scalar::Const" = do 
     62            sv  <- Scalar.fetch sv 
     63            ref <- fromVal sv 
     64            compileEval (ref :: VCode) 
     65    compileEval x = internalError ("Unrecognized construct: " ++ show x) 
     66 
     67instance Compile VCode where 
     68    compileEval sub = do 
     69        prms <- mapM compileEval (subParams sub) 
     70        body <- compileEval (subFun sub) 
     71        return . vcat $ prms ++ [ text "", body ] 
     72 
     73instance Compile Param where 
     74    compile prm = text ".param pmc" <+> varText (paramName prm) 
    2175 
    2276class (Show x) => Compile x where 
     77    compileEval :: x -> Eval Doc 
     78    compileEval x = return (compile x) 
    2379    compile :: x -> Doc 
    2480    compile x = internalError ("Unrecognized construct: " ++ show x) 
     
    61117    compile (Var name) = varText name 
    62118    compile (Syn ";" stmts) = vcat $ map compile stmts 
    63     compile (Syn "=" [var, Syn "[]" [lhs, rhs]]) = vcat $ 
    64         [ compile var <+> text "=" <+> compile lhs <> text "[" <> compile rhs <> text"]" 
    65         ] 
    66119    compile (Syn "block" blocks) = vcat $ map compile blocks 
    67     compile (Syn "=" [lhs, rhs@(Var _)]) = hsep $ 
    68         [ compile lhs, text "=", text "assign", compile rhs ] 
    69     compile (Syn "=" [lhs, rhs]) = hsep $ 
    70         [ compile lhs, text "=", compile rhs ] 
     120    compile (Syn "=" [lhs, rhs]) = compileAssign lhs rhs 
    71121    compile (Syn "if" exps) = compileCond "unless" exps 
    72122    compile (Syn "unless" exps) = compileCond "if" exps 
     
    87137            , label last 
    88138            ] 
     139    compile (App "&return" [] [val]) = text ".return" <+> parens (compile val) 
    89140    compile (App "&last" _ _) = text "invoke last" 
    90141    compile (App "&substr" [] [str, start, Val (VInt 1)]) = hcat $ 
     
    102153        compile $ App "&print" invs (args ++ [Val $ VStr "\n"]) 
    103154    compile (App "&print" invs args) = vcat $ 
    104         map ((text "print" <+>) . compile) (invs ++ args) 
     155        map (\x -> vcat 
     156                [ text ".local pmc tmp" 
     157                , text "tmp = new PerlUndef" 
     158                , compileAssign (text "tmp") x 
     159                , text "print tmp" 
     160                ]) 
     161            (invs ++ args) 
     162    compile (App "&not" [] []) = 
     163        text "new" <+> compile (Val VUndef) 
    105164    compile (Val (VStr x))  = showText $ encodeUTF8 (concatMap quoted x) 
    106165    compile (Val (VInt x))  = integer x 
     
    121180    compile (Syn "mval" [exp]) = compile exp 
    122181    compile (Syn "," things) = vcat $ map compile things 
    123     compile (App "&not" [] []) = 
    124         text "new" <+> compile (Val VUndef) 
     182    compile (Syn syn [lhs, exp]) | last syn == '=' = 
     183        compile $ Syn "=" [lhs, App ("&infix:" ++ init syn) [lhs, exp] []] 
    125184    compile (Cxt _ exp) = compile exp 
    126185    compile x = error $ "Cannot compile: " ++ (show x) 
     
    129188showText = text . show 
    130189 
     190compileAssign :: (Compile a) => a -> Exp -> Doc 
     191compileAssign lhs rhs@(Var _) = hsep [ compile lhs, text "=", text "assign", compile rhs ] 
     192compileAssign lhs (App ('&':name) _ [arg]) = vcat $ 
     193        [ text ".local pmc tmp" 
     194        , text "tmp = new PerlUndef" 
     195        , compileAssign (text "tmp") arg 
     196        , hsep [compile lhs, text "=", text name <> parens (text "tmp")] 
     197        ] 
     198compileAssign lhs (Syn "[]" [arr, idx]) = vcat $ 
     199        [ compile lhs <+> text "=" <+> compile arr <> text "[" <> compile idx <> text"]" 
     200        ] 
     201compileAssign lhs rhs = hsep [ compile lhs, text "=", compile rhs ] 
  • src/Pugs/Compile/Pugs.hs

    r2396 r2401  
    55import Pugs.Internals 
    66 
    7 genPugs :: Env -> IO String 
    8 genPugs Env{ envBody = exp, envGlobal = globRef } = do 
    9     glob <- readIORef globRef 
    10     return . unlines $ 
     7genPugs :: Eval Val 
     8genPugs = do 
     9    Env{ envBody = exp, envGlobal = globRef } <- ask 
     10    glob <- liftIO $ readIORef globRef 
     11    return . VStr . unlines $ 
    1112        [ "{-# OPTIONS_GHC -fglasgow-exts -fno-warn-unused-imports -fno-warn-unused-binds -O #-}" 
    1213        , "module MainCC where" 
  • src/Pugs/Run.hs

    r2396 r2401  
    2323    args <- getArgs 
    2424    f $ canonicalArgs args 
    25  
    26 runEval :: Env -> Eval Val -> IO Val 
    27 runEval env eval = withSocketsDo $ do 
    28     my_perl <- initPerl5 "" 
    29     val <- (`runReaderT` env) $ do 
    30         (`runContT` return) $ 
    31             resetT eval 
    32     freePerl5 my_perl 
    33     return val 
    3425 
    3526runEnv :: Env -> IO Val