Changeset 2396
- Timestamp:
- 04/27/05 15:02:41 (4 years ago)
- svk:copy_cache_prev:
- 3914
- Location:
- src
- Files:
-
- 13 modified
-
Main.hs (modified) (2 diffs)
-
Pugs/AST.hs (modified) (7 diffs)
-
Pugs/AST.hs-boot (modified) (2 diffs)
-
Pugs/Compile.hs (modified) (1 diff)
-
Pugs/Compile/Haskell.hs (modified) (1 diff)
-
Pugs/Compile/Parrot.hs (modified) (2 diffs)
-
Pugs/Compile/Pugs.hs (modified) (1 diff)
-
Pugs/Eval.hs (modified) (4 diffs)
-
Pugs/Monads.hs (modified) (2 diffs)
-
Pugs/Parser.hs (modified) (7 diffs)
-
Pugs/Pretty.hs (modified) (2 diffs)
-
Pugs/Run.hs (modified) (1 diff)
-
Pugs/Types.hs (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/Main.hs
r2358 r2396 89 89 90 90 dump :: String -> IO () 91 dump = (doParseWith $ \exp _ -> print exp) "-" 92 93 comp :: String -> IO () 94 comp = (doParseWith $ \exp _ -> putStrLn =<< compile "Haskell" exp) "-" 91 dump = (doParseWith $ \env _ -> print $ envBody env) "-" 92 93 dumpGlob :: String -> IO () 94 dumpGlob = (doParseWith $ \env _ -> do 95 glob <- readIORef $ envGlobal env 96 print $ userDefined glob) "-" 97 98 userDefined :: Pad -> Pad 99 userDefined (MkPad pad) = MkPad $ Map.filterWithKey doFilter pad 100 where 101 doFilter (_:'*':_) _ = False 102 doFilter _ _ = True 95 103 96 104 repLoop :: IO () … … 117 125 118 126 doExternal :: String -> FilePath -> String -> IO () 119 doExternal mod = doParseWith $ \e xp_ -> do120 str <- externalize mod exp127 doExternal mod = doParseWith $ \env _ -> do 128 str <- externalize mod $ envBody env 121 129 putStrLn str 122 130 123 131 doCompile :: [Char] -> FilePath -> String -> IO () 124 doCompile backend = doParseWith $ \exp _ -> do 125 str <- compile backend exp 132 doCompile backend = doParseWith $ \env _ -> do 133 glob <- readIORef $ envGlobal env 134 globRef <- newIORef $ userDefined glob 135 str <- compile backend env{ envGlobal = globRef } 126 136 writeFile "dump.ast" str 127 137 128 doParseWith :: ( Pugs.AST.Exp-> FilePath -> IO a) -> FilePath -> String -> IO a138 doParseWith :: (Env -> FilePath -> IO a) -> FilePath -> String -> IO a 129 139 doParseWith f name prog = do 130 140 env <- emptyEnv [] 131 runRule env (f' . envBody)ruleProgram name $ decodeUTF8 prog132 where 133 f' (Val err@(VError _ _))= do141 runRule env f' ruleProgram name $ decodeUTF8 prog 142 where 143 f' Env{ envBody = Val err@(VError _ _) } = do 134 144 hPutStrLn stderr $ pretty err 135 145 exitFailure 136 f' e xp = f expname146 f' env = f env name 137 147 138 148 -
src/Pugs/AST.hs
r2390 r2396 508 508 , subName = "&?" 509 509 , subType = SubPrim 510 , subPad = Map.empty510 , subPad = mkPad [] 511 511 , subAssoc = "pre" 512 512 , subParams = [] … … 521 521 , subName = "&?" 522 522 , subType = SubBlock 523 , subPad = Map.empty523 , subPad = mkPad [] 524 524 , subAssoc = "pre" 525 525 , subParams = [] … … 677 677 where 678 678 showCxt CxtVoid = "Void" 679 showCxt (CxtItem typ) = "Scalar (" ++ show typ ++ ")" 680 showCxt (CxtSlurpy typ) = "List (" ++ show typ ++ ")" 681 682 type Pad = Map Var [IORef VRef] 679 showCxt (CxtItem typ) = "Scalar (" ++ showType typ ++ ")" 680 showCxt (CxtSlurpy typ) = "List (" ++ showType typ ++ ")" 681 682 newtype Pad = MkPad (Map Var [IORef VRef]) 683 deriving (Eq, Ord, Typeable) 684 685 instance Show Pad where 686 show pad = "(mkPad [" ++ 687 concat (intersperse ", " $ map dump $ padToList pad) ++ 688 "])" 689 where 690 dump (n, ioRefs) = "(" ++ show n ++ ", [" ++ 691 concat (intersperse ", " $ map dumpIORef ioRefs) ++ 692 "])" 693 dumpIORef ioRef = unsafePerformIO $ do 694 ref <- readIORef ioRef 695 dump <- (`runReaderT` undefined) $ (`runContT` return) $ resetT $ do 696 dumpRef ref 697 return $ "unsafePerformIO (newIORef " ++ vCast dump ++ ")" 698 699 mkPad = MkPad . Map.fromList 700 lookupPad key (MkPad map) = Map.lookup key map 701 padToList (MkPad map) = Map.assocs map 702 diffPads (MkPad map1) (MkPad map2) = MkPad $ Map.difference map1 map2 703 unionPads (MkPad map1) (MkPad map2) = MkPad $ Map.union map1 map2 683 704 684 705 genMultiSym name ref = liftIO $ do 685 706 ioRef <- newIORef ref 686 return $ Map.insertWith (++) name [ioRef]707 return $ \(MkPad map) -> MkPad $ Map.insertWith (++) name [ioRef] map 687 708 688 709 genSym name ref = liftIO $ do 689 710 ioRef <- newIORef ref 690 return $ Map.insert name [ioRef]711 return $ \(MkPad map) -> MkPad $ Map.insert name [ioRef] map 691 712 692 713 show' :: (Show a) => a -> String … … 705 726 706 727 findSym :: String -> Pad -> Maybe (IORef VRef) 707 findSym name pad = case Map.lookupname pad of728 findSym name pad = case lookupPad name pad of 708 729 Just (x:_) -> Just x 709 730 _ -> Nothing … … 864 885 forceRef r = retError "cannot forceRef" (Val $ VRef r) 865 886 887 dumpRef :: VRef -> Eval Val 888 dumpRef (MkRef (ICode cv)) = do 889 vsub <- Code.assuming cv [] [] 890 return (VStr $ "(MkRef (ICode $ " ++ show vsub ++ "))") 891 dumpRef (MkRef (IScalar sv)) | Scalar.iType sv == mkType "Scalar::Const" = do 892 sv <- Scalar.fetch sv 893 return (VStr $ "(MkRef (IScalar $ " ++ show sv ++ "))") 894 dumpRef ref = return (VStr $ "(unsafePerformIO . newObject $ mkType \"" ++ show (refType ref) ++ "\")") 895 866 896 readRef :: VRef -> Eval Val 867 897 readRef (MkRef (IScalar sv)) = Scalar.fetch sv … … 905 935 clearRef r = retError "cannot clearRef" (Val $ VRef r) 906 936 907 newObject :: Type -> EvalVRef937 newObject :: (MonadIO m) => Type -> m VRef 908 938 newObject (MkType "Scalar") = liftIO $ 909 939 return . scalarRef =<< newIORef undef … … 914 944 newObject (MkType "Code") = liftIO $ 915 945 return . codeRef =<< newIORef mkSub 916 newObject typ = do 917 retError ("Cannot create object" ++ (show typ)) (Val undef) 946 newObject typ = fail ("Cannot create object: " ++ showType typ) 918 947 919 948 -- XXX: Refactor doHash and doArray into one -- also see Eval's [] and {} -
src/Pugs/AST.hs-boot
r2370 r2396 25 25 type VBlock = Exp 26 26 type Params = [Param] 27 type Pad = Map Var [IORef VRef]28 27 29 28 data Env … … 36 35 37 36 newtype VThunk = MkThunk (Eval Val) 37 newtype Pad = MkPad (Map Var [IORef VRef]) 38 38 39 39 data Exp -
src/Pugs/Compile.hs
r2009 r2396 11 11 12 12 module Pugs.Compile where 13 import Pugs.AST 13 14 import Pugs.Compile.Pugs (genPugs) 14 15 import Pugs.Compile.Parrot (genPIR) 15 16 import Pugs.Compile.Haskell (genGHC) 16 17 18 compile :: String -> Env -> IO String 17 19 compile "Haskell" = genGHC 18 20 compile "Pugs" = genPugs -
src/Pugs/Compile/Haskell.hs
r2319 r2396 10 10 11 11 import Pugs.Internals 12 import Language.Haskell.TH as TH12 import qualified Language.Haskell.TH as TH 13 13 import Pugs.AST 14 14 import Pugs.Run 15 15 import Pugs.Prim 16 16 17 genGHC exp = runQ [d| mainCC = runComp $(compile exp) |] >>= \str -> return . unlines $ 18 [ "{-# OPTIONS_GHC -fglasgow-exts -fth -O #-}" 19 , "module MainCC where" 20 , "import GHC.Base" 21 , "import Run" 22 , "import AST" 23 , "import Prim" 24 , "import Internals" 25 , "import Language.Haskell.TH as TH" 26 , "" 27 , pprint str 28 ] 17 genGHC :: Env -> IO String 18 genGHC Env{ envBody = exp } = 19 TH.runQ [d| mainCC = runComp $(compile exp) |] >>= \str -> return . unlines $ 20 [ "{-# OPTIONS_GHC -fglasgow-exts -fth -O #-}" 21 , "module MainCC where" 22 , "import GHC.Base" 23 , "import Run" 24 , "import AST" 25 , "import Pugs.Types" 26 , "import Prim" 27 , "import Internals" 28 , "import Language.Haskell.TH as TH" 29 , "" 30 , TH.pprint str 31 ] 29 32 30 33 compile (App op [inv] []) = compile (App op [] [inv]) -
src/Pugs/Compile/Parrot.hs
r2371 r2396 7 7 import Data.HashTable 8 8 import Text.PrettyPrint 9 import qualified Data.Map as Map10 9 11 10 -- XXX This compiler needs a totaly rewrite using Parrot AST, 12 11 -- XXX and maybe TH-based AST combinators 13 12 14 genPIR :: (Pugs.Compile.Parrot.Compile x, Monad m) => x -> mString15 genPIR exp= return . unlines $13 genPIR :: Env -> IO String 14 genPIR Env{ envBody = exp } = return . unlines $ 16 15 [ "#!/usr/bin/env parrot" 17 16 , ".sub main @MAIN" … … 118 117 , varText name <+> text "=" <+> text "new" <+> varInit name 119 118 ] 120 | name <- Map.keyspad119 | (name, _) <- padToList pad 121 120 ] 122 121 compile (Syn "mval" [exp]) = compile exp -
src/Pugs/Compile/Pugs.hs
r2078 r2396 2 2 3 3 module Pugs.Compile.Pugs where 4 import Pugs.AST 4 5 import Pugs.Internals 5 6 6 genPugs :: (Show a, Monad m) => a -> m String 7 genPugs exp = return . unlines $ 8 [ "{-# OPTIONS_GHC -fglasgow-exts -fno-warn-unused-imports -fno-warn-unused-binds -O #-}" 9 , "module MainCC where" 10 , "import Pugs.Run" 11 , "import Pugs.AST" 12 , "import Pugs.Internals" 13 , "" 14 , "mainCC = runAST $ " ++ show exp 15 ] 7 genPugs :: Env -> IO String 8 genPugs Env{ envBody = exp, envGlobal = globRef } = do 9 glob <- readIORef globRef 10 return . unlines $ 11 [ "{-# OPTIONS_GHC -fglasgow-exts -fno-warn-unused-imports -fno-warn-unused-binds -O #-}" 12 , "module MainCC where" 13 , "import Pugs.Run" 14 , "import Pugs.AST" 15 , "import Pugs.Types" 16 , "import Pugs.Internals" 17 , "" 18 , "mainCC = runAST glob ast" 19 , " where" 20 , " glob = " ++ show glob 21 , " ast = " ++ show exp 22 , "" 23 ] 16 24 -
src/Pugs/Eval.hs
r2370 r2396 35 35 uniq <- liftIO $ newUnique 36 36 syms <- liftIO $ initSyms 37 glob <- liftIO $ newIORef (combine (pad ++ syms) Map.empty)37 glob <- liftIO $ newIORef (combine (pad ++ syms) $ mkPad []) 38 38 return $ Env 39 39 { envContext = CxtVoid 40 , envLexical = Map.empty40 , envLexical = mkPad [] 41 41 , envLValue = False 42 42 , envGlobal = glob … … 95 95 syms <- forM [lex, glob] $ \pad -> do 96 96 forM names $ \name' -> do 97 case Map.lookupname' pad of97 case lookupPad name' pad of 98 98 Just ioRefs -> do 99 99 refs <- liftIO $ mapM readIORef ioRefs … … 147 147 let doRest = reduceStatements rest e 148 148 lex <- asks envLexical 149 local (\e -> e{ envLexical = lex' ` Map.union` lex }) doRest149 local (\e -> e{ envLexical = lex' `unionPads` lex }) doRest 150 150 Syn "env" [] | null rest -> const $ do 151 151 env <- ask … … 153 153 Syn "dump" [] | null rest -> \e -> do 154 154 Env{ envGlobal = globals, envLexical = lexicals } <- ask 155 liftIO $ modifyIORef globals ( Map.unionlexicals)155 liftIO $ modifyIORef globals (unionPads lexicals) 156 156 reduceStatements rest e 157 157 _ | null rest -> const $ do -
src/Pugs/Monads.hs
r2390 r2396 15 15 import Pugs.Context 16 16 import Pugs.Types 17 import qualified Data.Map as Map18 17 19 18 headVal [] = retEmpty … … 89 88 | typ >= SubBlock = do 90 89 blockRec <- genSym "&?BLOCK" (codeRef (orig sub)) 91 return $ \e -> e{ envLexical = combine [blockRec] (subPad sub `Map.union` pad) } 90 return $ \e -> e 91 { envLexical = combine [blockRec] (subPad sub `unionPads` pad) } 92 92 | otherwise = do 93 93 subRec <- sequence -
src/Pugs/Parser.hs
r2370 r2396 22 22 import Pugs.Rule.Error 23 23 import Pugs.Pretty 24 import qualified Data.Map as Map25 24 26 25 -- Lexical units -------------------------------------------------- … … 31 30 eof 32 31 env <- getState 33 return $ env { envBody = statements }32 return $ env { envBody = statements, envStash = "" } 34 33 35 34 ruleBlock :: RuleParser Exp … … 232 231 , subFun = fun 233 232 } 234 exp = Stmts 235 [ (Sym scope name, namePos) 236 , (Syn ":=" [Var name, Syn "sub" [subExp]], bodyPos) 237 ] 233 decl = (Sym scope name, namePos) 234 exp = (Syn ":=" [Var name, Syn "sub" [subExp]], bodyPos) 238 235 -- XXX: user-defined infix operator 239 236 if scope == SGlobal 240 then do { unsafeEvalExp exp; return emptyExp } 241 else return exp 237 then do { unsafeEvalExp (Stmts [decl, exp]); return emptyExp } 238 else do 239 lexDiff <- unsafeEvalLexDiff [decl] 240 let lexExp = (Pad scope lexDiff, namePos) 241 return $ Stmts [lexExp, exp] 242 242 243 243 subNameWithPrefix prefix = (<?> "subroutine name") $ lexeme $ try $ do … … 317 317 exp <- ruleExpression 318 318 return (sym, Just exp) 319 env <- getState 320 env' <- unsafeEvalStmts decl 321 setState env' 319 lexDiff <- unsafeEvalLexDiff decl 322 320 let lexExp = (Pad scope lexDiff, pos) 323 lexDiff = envLexical env' `Map.difference` envLexical env324 321 return $ case expMaybe of 325 322 Just exp -> Stmts [lexExp, (Syn sym [lhs, exp], pos)] … … 393 390 _ -> fail "" 394 391 392 unsafeEvalLexDiff decl = do 393 env <- getState 394 env' <- unsafeEvalStmts decl 395 setState env' 396 return $ envLexical env' `diffPads` envLexical env 397 395 398 unsafeEvalStmts stmts = do 396 399 pos <- getPosition 400 env <- getState 397 401 val <- unsafeEvalExp $ Stmts (stmts ++ [(Syn "env" [], pos)]) 398 402 case val of 399 Val (VControl (ControlEnv env)) -> return env 400 _ -> error $ pretty val 403 Val (VControl (ControlEnv env')) -> 404 return env'{ envDebug = envDebug env } 405 _ -> error $ pretty val 401 406 402 407 unsafeEvalExp exp = do … … 547 552 { isMulti = False 548 553 , subName = "<anon>" 549 , subPad = Map.empty554 , subPad = mkPad [] 550 555 , subType = typ 551 556 , subAssoc = "pre" … … 650 655 return . concat . unsafePerformIO $ do 651 656 glob <- readIORef $ envGlobal env 652 forM ( Map.assocs glob ++ Map.assocs(envLexical env)) $ \(name, ioRefs) -> do657 forM (padToList glob ++ padToList (envLexical env)) $ \(name, ioRefs) -> do 653 658 refs <- mapM readIORef ioRefs 654 659 return $ map (\ref -> (dropWhile isPunctuation $ name, ref)) refs -
src/Pugs/Pretty.hs
r2377 r2396 18 18 import Text.PrettyPrint 19 19 import qualified Data.Set as Set 20 import qualified Data.Map as Map21 20 22 21 defaultIndent :: Int … … 40 39 41 40 instance Pretty Pad where 42 format pad = cat $ map formatAssoc (Map.assocs pad)41 format pad = cat $ map formatAssoc $ padToList pad 43 42 where 44 43 formatAssoc (name, var) = format name <+> text ":=" <+> (nest defaultIndent $ vcat $ map format var) -
src/Pugs/Run.hs
r2358 r2396 36 36 runEnv env = runEval env $ evaluateMain (envBody env) 37 37 38 runAST :: Exp -> IO Val39 runAST ast = do38 runAST :: Pad -> Exp -> IO Val 39 runAST glob ast = do 40 40 hSetBuffering stdout NoBuffering 41 name <- getProgName 42 args <- getArgs 43 env <- prepareEnv name args 44 runEnv env{ envBody = ast, envDebug = Nothing } 41 name <- getProgName 42 args <- getArgs 43 env <- prepareEnv name args 44 glob' <- readIORef $ envGlobal env 45 globRef <- newIORef (glob `unionPads` glob') 46 runEnv env{ envBody = ast, envGlobal = globRef, envDebug = Nothing } 45 47 46 48 runComp :: Eval Val -> IO Val -
src/Pugs/Types.hs
r2362 r2396 21 21 22 22 instance Show Type where 23 show (MkType typ) = typ 24 show (TypeOr t1 t2) = show t1 ++ "|" ++ show t2 25 show (TypeAnd t1 t2) = show t1 ++ "&" ++ show t2 23 show t = "(mkType \"" ++ showType t ++ "\")" 24 25 showType (MkType typ) = typ 26 showType (TypeOr t1 t2) = showType t1 ++ "|" ++ showType t2 27 showType (TypeAnd t1 t2) = showType t1 ++ "&" ++ showType t2 26 28 27 29 type ClassTree = Tree Type
