Changeset 15297 for src/Pugs/Eval.hs
- Timestamp:
- 02/18/07 15:59:02 (21 months ago)
- Files:
-
- 1 modified
-
src/Pugs/Eval.hs (modified) (25 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Eval.hs
r15296 r15297 1 {-# OPTIONS_GHC -fglasgow-exts -cpp -fno-warn-deprecations -fallow-overlapping-instances -foverloaded-strings#-}1 {-# OPTIONS_GHC -fglasgow-exts -cpp -fno-warn-deprecations -fallow-overlapping-instances #-} 2 2 3 3 {-| … … 69 69 , envLValue = False 70 70 , envGlobal = length (show (padKeys globPad)) `seq` glob -- force eval of all sym names 71 , envPackage = _cast "Main"71 , envPackage = cast "Main" 72 72 , envClasses = initTree 73 73 , envEval = evaluate … … 95 95 runWarn msg = do 96 96 enterEvalContext CxtVoid $ 97 App (_Var "&warn") Nothing [Val ( _VStr msg)]97 App (_Var "&warn") Nothing [Val (VStr msg)] 98 98 return () 99 99 … … 115 115 evaluateMain exp = do 116 116 -- S04: INIT {...}* at run time, ASAP 117 initAV <- reduceVar $ _cast "@*INIT"117 initAV <- reduceVar $ cast "@*INIT" 118 118 initSubs <- fromVals initAV 119 119 enterContext CxtVoid $ do … … 123 123 tryT (evaluate exp) `finallyM` do 124 124 -- S04: END {...} at run time, ALAP 125 endAV <- reduceVar $ _cast "@*END"125 endAV <- reduceVar $ cast "@*END" 126 126 endSubs <- fromVals endAV 127 endMainAV <- reduceVar $ _cast "@Main::END"127 endMainAV <- reduceVar $ cast "@Main::END" 128 128 endMainSubs <- fromVals endMainAV 129 129 enterContext CxtVoid $ do … … 143 143 Just ref -> do 144 144 want <- asks envWant 145 debug ref ( _cast "indent") ('-':) (" Evl [" ++ want ++ "]:\n") exp145 debug ref (cast "indent") ('-':) (" Evl [" ++ want ++ "]:\n") exp 146 146 val <- local (\e -> e{ envBody = exp }) $ reduce exp 147 debug ref ( _cast "indent") (\x -> if null x then [] else tail x) "- Ret: " val147 debug ref (cast "indent") (\x -> if null x then [] else tail x) "- Ret: " val 148 148 trapVal val (return val) 149 149 Nothing -> do … … 270 270 271 271 isStrict :: Eval Bool 272 isStrict = fromVal =<< readVar ( _cast "$*STRICT")272 isStrict = fromVal =<< readVar (cast "$*STRICT") 273 273 274 274 -- Reduction for variables … … 276 276 reduceVar var@MkVar{ v_sigil = sig, v_twigil = twi, v_name = name, v_package = pkg } 277 277 | TAttribute <- twi 278 = reduceSyn ( cast $show sig ++ "{}") [ Syn "{}" [_Var "&self", Val (VStr $ cast name)] ]278 = reduceSyn (show sig ++ "{}") [ Syn "{}" [_Var "&self", Val (VStr $ cast name)] ] 279 279 | TPrivate <- twi 280 = reduceSyn ( cast $show sig ++ "{}") [ Syn "{}" [_Var "&self", Val (VStr $ cast name)] ]280 = reduceSyn (show sig ++ "{}") [ Syn "{}" [_Var "&self", Val (VStr $ cast name)] ] 281 281 | otherwise = do 282 282 v <- findVar var … … 307 307 308 308 -- XXX - Hack to get context propagating to "return" 309 reduceStmts this@(App (Var var) _ _) _ | var == _cast "&return" = reduce this310 reduceStmts this@(Ann _ (App (Var var) _ _)) _ | var == _cast "&return" = reduce this309 reduceStmts this@(App (Var var) _ _) _ | var == cast "&return" = reduce this 310 reduceStmts this@(Ann _ (App (Var var) _ _)) _ | var == cast "&return" = reduce this 311 311 312 312 reduceStmts this rest = do 313 313 let withCxt = case this of 314 App (Var var) _ _ | var == _cast "&yield" -> id315 Ann _ (App (Var var) _ _) | var == _cast "&yield" -> id314 App (Var var) _ _ | var == cast "&yield" -> id 315 Ann _ (App (Var var) _ _) | var == cast "&yield" -> id 316 316 _ -> enterContext cxtVoid 317 317 val <- withCxt (reduce this) … … 383 383 -- Special case: my (undef) is no-op 384 384 reduceSym scope var exp 385 -- | var == _cast "" = evalExp exp385 -- | var == cast "" = evalExp exp 386 386 | scope <= SMy = do 387 387 ref <- newObject (typeOfSigilVar var) … … 418 418 enough to make it redundant. 419 419 -} 420 reduceSyn :: ID-> [Exp] -> Eval Val420 reduceSyn :: String -> [Exp] -> Eval Val 421 421 422 422 reduceSyn "()" [exp] = reduce exp … … 541 541 readRef newAV `finallyM` liftSTM (modifyTVar globTV oldSym) 542 542 where 543 takeVar = _cast "$*TAKE"543 takeVar = cast "$*TAKE" 544 544 545 545 reduceSyn "loop" exps = enterLoop $ do … … 643 643 names <- forM vars $ \var -> case unwrap var of 644 644 Var name -> return name 645 Syn syn [vexp] 646 | [sigil,':',':','(',')'] <- cast syn 647 , Val (VStr name) <- unwrap vexp 648 -> return $ possiblyFixOperatorName (cast (sigil:cast name)) 645 Syn [sigil,':',':','(',')'] [vexp] 646 | Val (VStr name) <- unwrap vexp -> return $ possiblyFixOperatorName (cast (sigil:name)) 649 647 _ -> retError "Cannot bind this as lhs" var 650 648 bindings <- forM (names `zip` vexps) $ \(var, vexp) -> enterLValue $ do … … 729 727 -- XXX evil hack for infinite slices 730 728 | [lhs, App (Var var) invs args] <- unwrap exps 731 , var == _cast "&postfix:..."729 , var == cast "&postfix:..." 732 730 , [idx] <- maybeToList invs ++ args 733 731 -- , not (envLValue env) 734 732 = reduce (Syn "[...]" [lhs, idx]) 735 733 | [lhs, App (Var var) invs args] <- unwrap exps 736 , var == _cast "&infix:.."734 , var == cast "&infix:.." 737 735 , [idx, Val (VNum n)] <- maybeToList invs ++ args 738 736 , n == 1/0 … … 782 780 evalRef ref 783 781 782 reduceSyn (sigil:"::()") exps = do 783 -- These are all parts of the name 784 parts <- mapM fromVal =<< mapM evalExp exps 785 -- Now we only have to add the sigil in front of the string and join 786 -- the parts with "::". 787 let varname = sigil:(concat . (intersperse "::") $ parts) 788 -- Finally, eval the varname. 789 reduceVar (possiblyFixOperatorName (cast varname)) 790 784 791 reduceSyn "{}" [listExp, indexExp] = do 785 792 varVal <- enterLValue $ enterEvalContext (cxtItem "Hash") listExp … … 794 801 795 802 reduceSyn "rx" [exp, adverbs] = do 796 hv <- fromVal =<< evalExp adverbs :: Eval [(VStr, Val)]803 hv <- fromVal =<< evalExp adverbs 797 804 val <- enterEvalContext (cxtItem "Str") exp 798 805 str <- fromVal val … … 867 874 langVal <- evalExp langExp 868 875 lang <- fromVal langVal 869 when (lang /= ("Haskell" :: String)) $876 when (lang /= "Haskell") $ 870 877 retError "Inline: Unknown language" langVal 871 878 pkg <- asks envPackage -- full module name here … … 882 889 retItem $ castV (key, val) 883 890 884 reduceSyn name[lhsExp, rhsExp]885 | syn <- cast name,last syn == '=' = do891 reduceSyn syn [lhsExp, rhsExp] 892 | last syn == '=' = do 886 893 let op = "&infix:" ++ init syn 887 894 lhs <- enterLValue $ evalExp lhsExp … … 929 936 return (sub:rest) 930 937 _ -> return [] 931 932 reduceSyn name exps | (sigil:"::()") <- cast name = do933 -- These are all parts of the name934 parts <- mapM fromVal =<< mapM evalExp exps935 -- Now we only have to add the sigil in front of the string and join936 -- the parts with "::".937 let varname = sigil:(concat . (intersperse "::") $ parts)938 -- Finally, eval the varname.939 reduceVar (possiblyFixOperatorName (_cast varname))940 938 941 939 reduceSyn name exps = … … 1110 1108 af (Just (mconcat (resFeed:c_feeds cap))) args 1111 1109 | App (Var var) Nothing capExps <- unwrapN 1112 , var == _cast "&prefix:|<<" = do1110 , var == cast "&prefix:|<<" = do 1113 1111 caps <- mapM castVal =<< fromVals =<< (enterRValue $ enterEvalContext (cxtSlurpy "Capture") (Syn "," capExps)) 1114 1112 af (Just (mconcat (resFeed:concatMap c_feeds caps))) args … … 1126 1124 1127 1125 dummyVar :: Var 1128 dummyVar = _cast "$"1126 dummyVar = cast "$" 1129 1127 1130 1128 chainFun :: Params -> Exp -> Params -> Exp -> [Val] -> Eval Val … … 1282 1280 tryAnyComprehension pre (pivot:post) 1283 1281 | App (Var var') _ _ <- unwrap pivot 1284 , var' == _cast "&list" = do1282 , var' == cast "&list" = do 1285 1283 -- List comprehension! This: 1286 1284 -- 1 < list(@x) < 2 … … 1302 1300 applyExp :: SubType -> [ApplyArg] -> Exp -> Eval Val 1303 1301 applyExp _ bound (Prim f) = 1304 f [ argValue arg | arg <- bound, (argName arg) /= _cast "%_" ]1302 f [ argValue arg | arg <- bound, (argName arg) /= cast "%_" ] 1305 1303 applyExp styp [] body = do 1306 1304 applyThunk styp [] $ MkThunk (evalExp body) anyType … … 1312 1310 let name = dropWhile (not . isAlpha) (cast $ argName arg) 1313 1311 value = argValue arg 1314 evalExp $ Syn "=" [Syn "{}" [Val invocant, Val ( _VStr name)], Val value]1312 evalExp $ Syn "=" [Syn "{}" [Val invocant, Val (VStr name)], Val value] 1315 1313 applyThunk styp normal $ MkThunk (evalExp body) anyType 1316 1314 where … … 1325 1323 -- introduce self and $_ as the first invocant. 1326 1324 inv <- case styp of 1327 SubPointy -> aliased [ _cast "$_"]1328 _ | styp <= SubMethod -> aliased [ _cast "&self"] -- , "$_"]1325 SubPointy -> aliased [cast "$_"] 1326 _ | styp <= SubMethod -> aliased [cast "&self"] -- , "$_"] 1329 1327 _ -> return [] 1330 1328 pad <- formal
