Changeset 2401
- Timestamp:
- 04/27/05 17:17:20 (4 years ago)
- svk:copy_cache_prev:
- 3914
- Location:
- src/Pugs
- Files:
-
- 6 modified
-
AST.hs (modified) (1 diff)
-
Compile.hs (modified) (2 diffs)
-
Compile/Haskell.hs (modified) (1 diff)
-
Compile/Parrot.hs (modified) (6 diffs)
-
Compile/Pugs.hs (modified) (1 diff)
-
Run.hs (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/AST.hs
r2397 r2401 718 718 719 719 type Eval x = ContT Val (ReaderT Env IO) x 720 721 runEval :: Env -> Eval Val -> IO Val 722 runEval 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 720 729 721 730 findSymRef :: (MonadIO m) => String -> Pad -> m VRef -
src/Pugs/Compile.hs
r2396 r2401 12 12 module Pugs.Compile where 13 13 import Pugs.AST 14 import Pugs.Internals 14 15 import Pugs.Compile.Pugs (genPugs) 15 16 import Pugs.Compile.Parrot (genPIR) … … 17 18 18 19 compile :: String -> Env -> IO String 19 compile "Haskell" =genGHC20 compile "Pugs" =genPugs21 compile "Parrot" =genPIR22 compile s = const (error $ "Cannot compile to " ++ s)20 compile "Haskell" env = fmap vCast $ runEval env genGHC 21 compile "Pugs" env = fmap vCast $ runEval env genPugs 22 compile "Parrot" env = fmap vCast $ runEval env genPIR 23 compile s _ = fail $ "Cannot compile to " ++ s 23 24 -
src/Pugs/Compile/Haskell.hs
r2400 r2401 15 15 import Pugs.Prim 16 16 17 genGHC :: Env -> IO String 18 genGHC Env{ envBody = exp } = 19 TH.runQ [d| mainCC = runComp $(compile exp) |] >>= \str -> return . unlines $ 17 genGHC :: Eval Val 18 genGHC = do 19 exp <- asks envBody 20 liftIO (TH.runQ [d| mainCC = runComp $(compile exp) |]) >>= \str -> return . VStr . unlines $ 20 21 [ "{-# OPTIONS_GHC -fglasgow-exts -fth -O #-}" 21 22 , "module MainCC where" -
src/Pugs/Compile/Parrot.hs
r2396 r2401 5 5 import Pugs.Pretty 6 6 import Pugs.AST 7 import Pugs.Types 7 8 import Data.HashTable 8 9 import Text.PrettyPrint 10 import qualified Pugs.Types.Scalar as Scalar 11 import qualified Pugs.Types.Code as Code 9 12 10 13 -- XXX This compiler needs a totaly rewrite using Parrot AST, 11 14 -- XXX and maybe TH-based AST combinators 12 15 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 ] 16 genPIR :: Eval Val 17 genPIR = 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 35 instance Compile Doc where 36 compile = id 37 38 instance Compile Pad where 39 compileEval pad = fmap vcat $ mapM compileEval (padToList pad) 40 41 instance 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 51 instance Compile (IORef VRef) where 52 compileEval x = do 53 ref <- liftIO $ readIORef x 54 compileEval ref 55 56 instance 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 67 instance 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 73 instance Compile Param where 74 compile prm = text ".param pmc" <+> varText (paramName prm) 21 75 22 76 class (Show x) => Compile x where 77 compileEval :: x -> Eval Doc 78 compileEval x = return (compile x) 23 79 compile :: x -> Doc 24 80 compile x = internalError ("Unrecognized construct: " ++ show x) … … 61 117 compile (Var name) = varText name 62 118 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 ]66 119 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 71 121 compile (Syn "if" exps) = compileCond "unless" exps 72 122 compile (Syn "unless" exps) = compileCond "if" exps … … 87 137 , label last 88 138 ] 139 compile (App "&return" [] [val]) = text ".return" <+> parens (compile val) 89 140 compile (App "&last" _ _) = text "invoke last" 90 141 compile (App "&substr" [] [str, start, Val (VInt 1)]) = hcat $ … … 102 153 compile $ App "&print" invs (args ++ [Val $ VStr "\n"]) 103 154 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 "¬" [] []) = 163 text "new" <+> compile (Val VUndef) 105 164 compile (Val (VStr x)) = showText $ encodeUTF8 (concatMap quoted x) 106 165 compile (Val (VInt x)) = integer x … … 121 180 compile (Syn "mval" [exp]) = compile exp 122 181 compile (Syn "," things) = vcat $ map compile things 123 compile ( App "¬" [] [])=124 text "new" <+> compile (Val VUndef)182 compile (Syn syn [lhs, exp]) | last syn == '=' = 183 compile $ Syn "=" [lhs, App ("&infix:" ++ init syn) [lhs, exp] []] 125 184 compile (Cxt _ exp) = compile exp 126 185 compile x = error $ "Cannot compile: " ++ (show x) … … 129 188 showText = text . show 130 189 190 compileAssign :: (Compile a) => a -> Exp -> Doc 191 compileAssign lhs rhs@(Var _) = hsep [ compile lhs, text "=", text "assign", compile rhs ] 192 compileAssign 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 ] 198 compileAssign lhs (Syn "[]" [arr, idx]) = vcat $ 199 [ compile lhs <+> text "=" <+> compile arr <> text "[" <> compile idx <> text"]" 200 ] 201 compileAssign lhs rhs = hsep [ compile lhs, text "=", compile rhs ] -
src/Pugs/Compile/Pugs.hs
r2396 r2401 5 5 import Pugs.Internals 6 6 7 genPugs :: Env -> IO String 8 genPugs Env{ envBody = exp, envGlobal = globRef } = do 9 glob <- readIORef globRef 10 return . unlines $ 7 genPugs :: Eval Val 8 genPugs = do 9 Env{ envBody = exp, envGlobal = globRef } <- ask 10 glob <- liftIO $ readIORef globRef 11 return . VStr . unlines $ 11 12 [ "{-# OPTIONS_GHC -fglasgow-exts -fno-warn-unused-imports -fno-warn-unused-binds -O #-}" 12 13 , "module MainCC where" -
src/Pugs/Run.hs
r2396 r2401 23 23 args <- getArgs 24 24 f $ canonicalArgs args 25 26 runEval :: Env -> Eval Val -> IO Val27 runEval env eval = withSocketsDo $ do28 my_perl <- initPerl5 ""29 val <- (`runReaderT` env) $ do30 (`runContT` return) $31 resetT eval32 freePerl5 my_perl33 return val34 25 35 26 runEnv :: Env -> IO Val
