Changeset 14150
- Timestamp:
- 10/09/06 17:27:25 (2 years ago)
- Location:
- src/Pugs
- Files:
-
- 2 modified
-
AST/Internals.hs (modified) (6 diffs)
-
Monads.hs (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/AST/Internals.hs
r14142 r14150 1266 1266 , envOuter :: !(Maybe Env) -- ^ Outer block's env 1267 1267 , envBody :: !Exp -- ^ Current AST expression 1268 , env Depth :: ![Frame]-- ^ Recursion depth1268 , envFrames :: !(Set Frame) -- ^ Recursion depth 1269 1269 , envDebug :: !DebugInfo -- ^ Debug info map 1270 1270 , envPos :: !Pos -- ^ Source position range … … 1279 1279 = FrameLoop 1280 1280 | FrameGiven 1281 | FrameGather 1281 1282 | FrameRoutine 1282 1283 deriving (Show, Eq, Ord, Typeable) -- don't derive YAML for now … … 1378 1379 1379 1380 findSymRef :: Var -> Pad -> Eval VRef 1380 findSymRef name pad = do 1381 case findSym name pad of 1382 Just ref -> liftSTM $ readTVar ref 1383 Nothing -> fail $ "Cannot find variable: " ++ show name 1384 1385 findSym :: Var -> Pad -> Maybe (TVar VRef) 1381 findSymRef name pad = liftSTM $ do 1382 ref <- findSym name pad 1383 readTVar ref 1384 1385 {-# SPECIALISE findSym :: Var -> Pad -> Eval (TVar VRef) #-} 1386 {-# SPECIALISE findSym :: Var -> Pad -> Maybe (TVar VRef) #-} 1387 findSym :: Monad m => Var -> Pad -> m (TVar VRef) 1386 1388 findSym name pad = case lookupPad name pad of 1387 Just (x:_) -> Justx1388 _ -> Nothing1389 Just (x:_) -> return x 1390 _ -> fail $ "Cannot find variable: " ++ show name 1389 1391 1390 1392 -- | Look up a symbol in a 'Pad', returning the ref it is bound to. … … 1400 1402 -} 1401 1403 1402 lookupPad key (MkPad map) = case Map.lookup (possiblyFixOperatorName key)map of1404 lookupPad key (MkPad map) = case Map.lookup key map of 1403 1405 Just (MkEntryMulti xs) -> Just [tvar | (_, tvar) <- xs] 1404 1406 Just (MkEntry (_, tvar)) -> Just [tvar] … … 1572 1574 clearRef r = retError "Cannot clearRef" r 1573 1575 1576 {-# SPECIALISE newObject :: Type -> Eval VRef #-} 1577 {-# SPECIALISE newObject :: Type -> IO VRef #-} 1574 1578 newObject :: (MonadSTM m, MonadIO m) => Type -> m VRef 1575 1579 newObject typ = case showType typ of … … 1889 1893 , envCaller = Nothing 1890 1894 , envOuter = Nothing 1891 , env Depth = []1895 , envFrames = Set.empty 1892 1896 , envBody = Val undef 1893 1897 , envDebug = Just ref -- Set to "Nothing" to disable debugging -
src/Pugs/Monads.hs
r14122 r14150 16 16 enterLValue, enterRValue, 17 17 enterLex, enterContext, enterEvalContext, enterPackage, enterCaller, 18 enterGiven, enterWhen, enterLoop, genSymPrim, genSymCC,18 enterGiven, enterWhen, enterLoop, enterGather, genSymPrim, genSymCC, 19 19 enterBlock, enterSub, envEnterCaller, 20 20 evalVal, tempVar, 21 22 enterFrame, assertFrame, emptyFrames, 21 23 22 24 MaybeT, runMaybeT, … … 27 29 import Control.Monad.RWS (MonadPlus(..)) 28 30 import qualified Data.Map as Map 29 31 import qualified Data.Set as Set 30 32 31 33 newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) } … … 106 108 { envLexical = MkPad (lex `Map.intersection` envImplicit env) 107 109 } 108 , env Depth = (FrameRoutine:envDepth env)110 , envFrames = FrameRoutine `Set.insert` envFrames env 109 111 , envImplicit = Map.fromList [(cast "$_", ())] 110 112 } … … 113 115 114 116 {-| 115 Bind @\$_@ to the given topic value in a new lexical scope, then perform 116 the specified evaluation in that scope. 117 118 Used by "Pugs.Eval"'s implementation of 'Pugs.Eval.reduce' for @\"given\"@. 119 -} 120 enterGiven :: Eval a -- ^ Action to perform within the new scope 121 -> Eval a 122 enterGiven = local (\e -> e{ envDepth = (FrameGiven:envDepth e) }) 117 Register the fact that we are inside a specially marked control block. 118 -} 119 enterFrame :: Frame -> Eval a -> Eval a 120 enterFrame f = local (\e -> e{ envFrames = f `Set.insert` envFrames e}) 121 122 enterGather, enterLoop, enterGiven :: Eval Val -> Eval Val 123 enterGather = enterFrame FrameGather 124 enterLoop = enterFrame FrameLoop 125 enterGiven = enterFrame FrameGiven 126 127 assertFrame :: Frame -> Eval a -> Eval a 128 assertFrame f action = do 129 frames <- asks envFrames 130 if Set.member f frames 131 then action 132 else fail ("Cannot use this control structure outside a '" ++ (map toLower (drop 5 (show f))) ++ "' structure") 133 134 emptyFrames :: Set Frame 135 emptyFrames = Set.empty 123 136 124 137 {-| … … 136 149 VControl (ControlGiven GivenBreak) -> retShiftEmpty 137 150 _ -> retShift rv 138 139 {-|140 Register the fact that we are inside a loop block.141 -}142 enterLoop :: Eval Val -- ^ Evaluation representing loop test & body143 -> Eval Val144 enterLoop = local (\e -> e{ envDepth = (FrameLoop:envDepth e) })145 151 146 152 {-|
