Changeset 2968
- Timestamp:
- 05/11/05 16:19:49 (4 years ago)
- svk:copy_cache_prev:
- 4536
- Location:
- src
- Files:
-
- 1 added
- 9 modified
-
Main.hs (modified) (1 diff)
-
Pugs/AST.hs (modified) (3 diffs)
-
Pugs/AST/Internals.hs (modified) (3 diffs)
-
Pugs/Eval.hs (modified) (3 diffs)
-
Pugs/Internals.hs (modified) (2 diffs)
-
Pugs/Monads.hs (modified) (2 diffs)
-
Pugs/Prim.hs (modified) (7 diffs)
-
Pugs/Prim/Eval.hs (added)
-
Pugs/Prim/List.hs (modified) (1 diff)
-
Pugs/Run.hs (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Main.hs
r2790 r2968 28 28 import Pugs.Pretty 29 29 import Pugs.Compile 30 import Pugs.Embed 30 31 import qualified Data.Map as Map 31 32 import Data.IORef -
src/Pugs/AST.hs
r2962 r2968 40 40 -- MonadEval(..), 41 41 42 runEvalSTM, runEvalIO, shiftT, resetT, runEvalMain,42 runEvalSTM, runEvalIO, shiftT, resetT, callCC, 43 43 evalExp, 44 44 undef, defined, … … 68 68 ) where 69 69 import Pugs.Internals 70 import Pugs.Cont (callCC) 71 import qualified Data.Map as Map 70 72 71 73 import Pugs.AST.Internals … … 117 119 evl <- asks envEval 118 120 evl exp 121 122 -- |Create a 'Pad'-transforming transaction that will install a symbol 123 -- definition in the 'Pad' it is applied to, /alongside/ any other mappings 124 -- of the same name. This is to allow for overloaded (i.e. multi) subs, 125 -- where one sub name actually maps to /all/ the different multi subs. 126 -- (Is this correct?) 127 genMultiSym :: MonadSTM m => String -> VRef -> m (Pad -> Pad) 128 genMultiSym name ref = do 129 tvar <- liftSTM $ newTVar ref 130 fresh <- liftSTM $ newTVar True 131 return $ \(MkPad map) -> MkPad $ 132 Map.insertWith (++) name [(fresh, tvar)] map 133 134 -- |Create a 'Pad'-transforming transaction that will install a symbol 135 -- mapping from a name to a thing, in the 'Pad' it is applied to. 136 -- Unlike 'genMultiSym', this version just installs a single definition 137 -- (right?), shadowing any earlier or outer definition. 138 genSym :: MonadSTM m => String -> VRef -> m (Pad -> Pad) 139 genSym name ref = do 140 tvar <- liftSTM $ newTVar ref 141 fresh <- liftSTM $ newTVar True 142 return $ \(MkPad map) -> MkPad $ Map.insert name [(fresh, tvar)] map -
src/Pugs/AST/Internals.hs
r2962 r2968 7 7 import Pugs.Rule 8 8 import Pugs.Types 9 import Pugs.Cont hiding (shiftT, resetT) 9 10 import qualified Data.Set as Set 10 11 import qualified Data.Map as Map … … 861 862 unionPads (MkPad map1) (MkPad map2) = MkPad $ Map.union map1 map2 862 863 863 -- |Create a 'Pad'-transforming transaction that will install a symbol864 -- definition in the 'Pad' it is applied to, /alongside/ any other mappings865 -- of the same name. This is to allow for overloaded (i.e. multi) subs,866 -- where one sub name actually maps to /all/ the different multi subs.867 -- (Is this correct?)868 genMultiSym :: MonadSTM m => String -> VRef -> m (Pad -> Pad)869 genMultiSym name ref = do870 tvar <- liftSTM $ newTVar ref871 fresh <- liftSTM $ newTVar True872 return $ \(MkPad map) -> MkPad $873 Map.insertWith (++) name [(fresh, tvar)] map874 875 -- |Create a 'Pad'-transforming transaction that will install a symbol876 -- mapping from a name to a thing, in the 'Pad' it is applied to.877 -- Unlike 'genMultiSym', this version just installs a single definition878 -- (right?), shadowing any earlier or outer definition.879 genSym :: MonadSTM m => String -> VRef -> m (Pad -> Pad)880 genSym name ref = do881 tvar <- liftSTM $ newTVar ref882 fresh <- liftSTM $ newTVar True883 return $ \(MkPad map) -> MkPad $ Map.insert name [(fresh, tvar)] map884 885 864 type Eval x = EvalT (ContT Val (ReaderT Env SIO)) x 886 865 type EvalMonad = EvalT (ContT Val (ReaderT Env SIO)) … … 927 906 ask = lift ask 928 907 local f m = EvalT $ local f (runEvalT m) 929 930 runEvalMain :: Env -> Eval Val -> IO Val931 runEvalMain env eval = withSocketsDo $ do932 my_perl <- initPerl5 ""933 val <- runEvalIO env eval934 freePerl5 my_perl935 return val936 908 937 909 findSymRef :: (MonadSTM m) => String -> Pad -> m VRef -
src/Pugs/Eval.hs
r2956 r2968 37 37 import Pugs.Prim 38 38 import Pugs.Prim.Match (op2Match) 39 import Pugs.Prim.List (op0Zip) 39 40 import Pugs.Context 40 41 import Pugs.Monads 41 42 import Pugs.Pretty 42 43 import Pugs.Types 44 import Pugs.Prim.Eval (retEvalResult) 45 import Pugs.External 43 46 44 47 -- |Construct a new, initially empty 'Env' (evaluation environment). … … 568 571 { '-' -> "__"; _ | isAlphaNum v -> [v] ; _ -> "_" } 569 572 #endif 570 op1 "require_haskell" (VStr $file ++ ".o")573 externRequire "Haskell" (file ++ ".o") 571 574 retEmpty 572 575 syn | last syn == '=' -> do … … 615 618 reduce (App (Var "&zip") invs args) = do 616 619 vals <- mapM (enterRValue . enterEvalContext (cxtItem "Array")) (invs ++ args) 617 val <- op0 "Y"vals620 val <- op0Zip vals 618 621 retVal val 619 622 -
src/Pugs/Internals.hs
r2943 r2968 17 17 module UTF8, 18 18 module Unicode, 19 module Pugs.Embed,20 19 module Pugs.Compat, 21 module Pugs.Cont,22 20 module RRegex, 23 21 module RRegex.Syntax, … … 71 69 import UTF8 72 70 import Unicode 73 import Pugs.Cont hiding (shiftT, resetT)74 import Pugs.Embed75 71 import Pugs.Compat 76 72 import RRegex -
src/Pugs/Monads.hs
r2966 r2968 16 16 import Pugs.Types 17 17 18 headVal :: [Val] -> Eval Val 18 19 headVal [] = retEmpty 19 20 headVal (v:_) = return v … … 92 93 , subBody = Prim ((esc =<<) . headVal) 93 94 } 94 95 95 96 enterSub :: VCode -> Eval Val -> Eval Val 96 97 enterSub sub action -
src/Pugs/Prim.hs
r2957 r2968 11 11 -} 12 12 13 module Pugs.Prim where 13 module Pugs.Prim ( 14 primOp, 15 primDecl, 16 initSyms, 17 op2DefinedOr, 18 op2ChainedList, 19 op1Exit, 20 -- used by Pugs.Compile.Haskell 21 op0, op1, op2, 22 ) where 14 23 import Pugs.Internals 15 24 import Pugs.Junc … … 17 26 import Pugs.Types 18 27 import Pugs.Pretty 19 import Pugs.Parser28 import Text.Printf 20 29 import Pugs.External 21 import Text.Printf30 import Pugs.Embed 22 31 import qualified Data.Map as Map 23 32 … … 29 38 import Pugs.Prim.Numeric 30 39 import Pugs.Prim.Lifts 40 import Pugs.Prim.Eval 31 41 32 42 op0 :: Ident -> [Val] -> Eval Val … … 44 54 op0 "not" = const retEmpty 45 55 op0 "so" = const (return $ VBool True) 46 op0 "¥" = fmap (VList . concat . op0Zip) . mapM fromVal56 op0 "¥" = op0Zip 47 57 op0 "Y" = op0 "¥" 48 58 op0 "File::Spec::cwd" = const $ do … … 235 245 strs <- fromVal v 236 246 fail (concat strs) 237 op1 "exit" = \v -> do 238 rv <- fromVal v 239 if rv /= 0 240 then shiftT . const . return . VControl . ControlExit . ExitFailure $ rv 241 else shiftT . const . return . VControl . ControlExit $ ExitSuccess 247 op1 "exit" = op1Exit 242 248 op1 "readlink" = \v -> do 243 249 str <- fromVal v … … 427 433 op1 other = \_ -> fail ("Unimplemented unaryOp: " ++ other) 428 434 429 op1EvalHaskell :: Val -> Eval Val 430 op1EvalHaskell cv = do 431 str <- fromVal cv :: Eval String 432 ret <- liftIO (evalHaskell str) 433 glob <- askGlobal 434 errSV <- findSymRef "$!" glob 435 case ret of 436 Right str -> do 437 writeRef errSV VUndef 438 return $ VStr str 439 Left err -> do 440 writeRef errSV (VStr err) 441 retEmpty 435 op1Exit v = do 436 rv <- fromVal v 437 if rv /= 0 438 then shiftT . const . return . VControl . ControlExit . ExitFailure $ rv 439 else shiftT . const . return . VControl . ControlExit $ ExitSuccess 442 440 443 441 op1StrFirst :: (Char -> Char) -> Val -> Eval Val … … 490 488 f x y 491 489 return (VBool True) 492 493 opEval :: Bool -> String -> String -> Eval Val494 opEval fatal name str = do495 env <- ask496 let env' = runRule env id ruleProgram name str497 val <- resetT $ local (const env') $ do498 evl <- asks envEval499 evl (envBody env')500 retEvalResult fatal val501 502 retEvalResult :: Bool -> Val -> Eval Val503 retEvalResult fatal val = do504 glob <- askGlobal505 errSV <- findSymRef "$!" glob506 case val of507 VError str _ | not fatal -> do508 writeRef errSV (VStr str)509 retEmpty510 _ -> do511 writeRef errSV VUndef512 return val513 490 514 491 mapStr :: (Word8 -> Word8) -> [Word8] -> String -
src/Pugs/Prim/List.hs
r2961 r2968 11 11 import Pugs.Prim.Numeric 12 12 13 op0Zip :: [[Val]] -> [[Val]] 14 op0Zip lists | all null lists = [] 15 op0Zip lists = (map zipFirst lists):(op0Zip (map zipRest lists)) 13 op0Zip = fmap (VList . concat . op0Zip') . mapM fromVal 14 15 op0Zip' :: [[Val]] -> [[Val]] 16 op0Zip' lists | all null lists = [] 17 op0Zip' lists = (map zipFirst lists):(op0Zip' (map zipRest lists)) 16 18 where 17 19 zipFirst [] = undef -
src/Pugs/Run.hs
r2926 r2968 18 18 import Pugs.Eval 19 19 import Pugs.Prim 20 import Pugs.Embed 20 21 import qualified Data.Map as Map 21 22 … … 23 24 args <- getArgs 24 25 f $ canonicalArgs args 26 27 runEvalMain :: Env -> Eval Val -> IO Val 28 runEvalMain env eval = withSocketsDo $ do 29 my_perl <- initPerl5 "" 30 val <- runEvalIO env eval 31 freePerl5 my_perl 32 return val 25 33 26 34 runEnv :: Env -> IO Val … … 79 87 #endif 80 88 let subExit = \x -> case x of 81 [x] -> op1 "exit" x82 _ -> op1 "exit"undef89 [x] -> op1Exit x -- needs refactoring (out of Prim) 90 _ -> op1Exit undef 83 91 emptyEnv name $ 84 92 [ genSym "@*ARGS" $ MkRef argsAV
