| 196 | | findVar :: Var -> Eval (Maybe VRef) |
| 197 | | findVar name = do |
| 198 | | rv <- findVarRef name |
| 199 | | case rv of |
| 200 | | Nothing -> case name of |
| 201 | | ('&':_) -> maybeM (findSub name Nothing []) $ \sub -> do |
| 202 | | return $ codeRef sub |
| 203 | | _ -> return Nothing |
| 204 | | Just ref -> fmap Just $ liftSTM (readTVar ref) |
| 205 | | |
| 206 | | findVarRef :: Var -> Eval (Maybe (TVar VRef)) |
| 207 | | findVarRef name |
| 208 | | | Just (package, name') <- breakOnGlue "::" name |
| 209 | | , Just (sig, "") <- breakOnGlue "CALLER" package = do |
| 210 | | maybeCaller <- asks envCaller |
| 211 | | case maybeCaller of |
| 212 | | Just env -> local (const env) $ do |
| 213 | | findVarRef (sig ++ name') |
| 214 | | Nothing -> retError "cannot access CALLER:: in top level" name |
| 215 | | | Just (package, name') <- breakOnGlue "::" name |
| 216 | | , Just (sig, "") <- breakOnGlue "OUTER" package = do |
| 217 | | maybeOuter <- asks envOuter |
| 218 | | case maybeOuter of |
| 219 | | Just env -> local (const env) $ do |
| 220 | | findVarRef (sig ++ name') |
| 221 | | Nothing -> retError "cannot access OUTER:: in top level" name |
| 222 | | | (_:'?':_) <- name = do |
| 223 | | rv <- getMagical name |
| 224 | | case rv of |
| 225 | | Nothing -> doFindVarRef name |
| 226 | | Just val -> do |
| 227 | | tvar <- liftSTM $ newTVar (MkRef . constScalar $ val) |
| 228 | | return $ Just tvar |
| 229 | | | "%" <- name = do |
| 230 | | {- %CALLER::, %OUTER::, %Package::, etc, all recurse to here. -} |
| 231 | | pad <- asks envLexical |
| 232 | | let plist = padToList pad |
| 233 | | hlist <- mapM padEntryToHashEntry plist |
| 234 | | let hash = IHash $ Map.fromList hlist |
| 235 | | let hashref = MkRef hash |
| 236 | | tvar <- liftSTM $ newTVar hashref |
| 237 | | return $ Just tvar |
| 238 | | | otherwise = doFindVarRef name |
| 239 | | where |
| 240 | | padEntryToHashEntry :: (Var, [(TVar Bool, TVar VRef)]) -> Eval (VStr, Val) |
| 241 | | padEntryToHashEntry (key, (_, tvref) : _) = do |
| 242 | | vref <- liftSTM (readTVar tvref) |
| 243 | | let val = VRef vref |
| 244 | | return (key, val) |
| 245 | | padEntryToHashEntry (_, []) = do fail "Nonexistant var in pad?" |
| 246 | | doFindVarRef :: Var -> Eval (Maybe (TVar VRef)) |
| 247 | | doFindVarRef name = do |
| 248 | | callCC $ \foundIt -> do |
| 249 | | lexSym <- fmap (findSym name . envLexical) ask |
| 250 | | when (isJust lexSym) $ foundIt lexSym |
| 251 | | glob <- liftSTM . readTVar . envGlobal =<< ask |
| 252 | | let globSym = findSym name glob |
| 253 | | when (isJust globSym) $ foundIt globSym |
| 254 | | let globSym = findSym (toGlobal name) glob |
| 255 | | when (isJust globSym) $ foundIt globSym |
| 256 | | return Nothing |
| 257 | | |
| 258 | | posSym :: Value a => (Pos -> a) -> Eval (Maybe Val) |
| 259 | | posSym f = fmap (Just . castV . f) $ asks envPos |
| 260 | | constSym :: String -> Eval (Maybe Val) |
| 261 | | constSym = return . Just . castV |
| 262 | | |
| 263 | | {-| |
| 264 | | Evaluate the \'magical\' variable associated with a given name. Returns |
| 265 | | @Nothing@ if the name does not match a known magical. |
| 266 | | -} |
| 267 | | getMagical :: String -- ^ Name of the magical var to evaluate |
| 268 | | -> Eval (Maybe Val) |
| 269 | | getMagical "$?FILE" = posSym posName |
| 270 | | getMagical "$?LINE" = posSym posBeginLine |
| 271 | | getMagical "$?COLUMN" = posSym posBeginColumn |
| 272 | | getMagical "$?POSITION" = posSym pretty |
| 273 | | getMagical "$?MODULE" = constSym "main" |
| 274 | | getMagical "$?OS" = constSym $ getConfig "osname" |
| 275 | | getMagical "$?CLASS" = fmap (Just . VType . mkType) (asks envPackage) |
| 276 | | getMagical ":?CLASS" = fmap (Just . VType . mkType) (asks envPackage) |
| 277 | | getMagical "$?PACKAGE" = fmap (Just . VType . mkType) (asks envPackage) |
| 278 | | getMagical ":?PACKAGE" = fmap (Just . VType . mkType) (asks envPackage) |
| 279 | | getMagical "$?ROLE" = fmap (Just . VType . mkType) (asks envPackage) |
| 280 | | getMagical ":?ROLE" = fmap (Just . VType . mkType) (asks envPackage) |
| 281 | | getMagical _ = return Nothing |
| | 169 | |
| 925 | | |
| 926 | | findSub :: String -- ^ Name, with leading @\&@. |
| 927 | | -> Maybe Exp -- ^ Invocant |
| 928 | | -> [Exp] -- ^ Other arguments |
| 929 | | -> Eval (Maybe VCode) |
| 930 | | findSub name' invs args = do |
| 931 | | let name = possiblyFixOperatorName name' |
| 932 | | case invs of |
| 933 | | Just _ | Just (package, name') <- breakOnGlue "::" name |
| 934 | | , Just (sig, "") <- breakOnGlue "SUPER" package -> do |
| 935 | | typ <- asks envPackage |
| 936 | | findSuperSub (mkType typ) (sig ++ name') |
| 937 | | Just exp | not (':' `elem` drop 2 name) -> do |
| 938 | | typ <- evalInvType $ unwrap exp |
| 939 | | if typ == mkType "Scalar::Perl5" then runPerl5Sub name else do |
| 940 | | findTypedSub typ name |
| 941 | | _ | [exp] <- args -> do |
| 942 | | typ <- evalInvType $ unwrap exp |
| 943 | | findTypedSub typ name |
| 944 | | _ -> findBuiltinSub name |
| 945 | | where |
| 946 | | findSuperSub :: Type -> String -> Eval (Maybe VCode) |
| 947 | | findSuperSub typ name = do |
| 948 | | let pkg = showType typ |
| 949 | | qualified = (head name:pkg) ++ "::" ++ tail name |
| 950 | | subs <- findWithSuper pkg name |
| 951 | | subs' <- if isJust subs then return subs else findBuiltinSub name |
| 952 | | case subs' of |
| 953 | | Just sub | subName sub == qualified -> return Nothing |
| 954 | | _ -> return subs' |
| 955 | | findTypedSub :: Type -> String -> Eval (Maybe VCode) |
| 956 | | findTypedSub typ name = do |
| 957 | | subs <- findWithPkg (showType typ) name |
| 958 | | if isJust subs then return subs else findBuiltinSub name |
| 959 | | findBuiltinSub :: String -> Eval (Maybe VCode) |
| 960 | | findBuiltinSub name = do |
| 961 | | sub <- findSub' name |
| 962 | | if isNothing sub then possiblyBuildMetaopVCode name else return sub |
| 963 | | evalInvType :: Exp -> Eval Type |
| 964 | | evalInvType x@(Var (':':typ)) = do |
| 965 | | typ' <- evalExpType x |
| 966 | | return $ if typ' == mkType "Scalar::Perl5" then typ' else mkType typ |
| 967 | | evalInvType (App (Var "&new") (Just inv) _) = do |
| 968 | | evalInvType $ unwrap inv |
| 969 | | evalInvType x@(App (Var _) (Just inv) _) = do |
| 970 | | typ <- evalInvType $ unwrap inv |
| 971 | | if typ == mkType "Scalar::Perl5" then return typ else evalExpType x |
| 972 | | evalInvType x = evalExpType $ unwrap x |
| 973 | | runPerl5Sub :: String -> Eval (Maybe VCode) |
| 974 | | runPerl5Sub name = do |
| 975 | | metaSub <- possiblyBuildMetaopVCode name |
| 976 | | if isJust metaSub then return metaSub else do |
| 977 | | return . Just $ mkPrim |
| 978 | | { subName = name |
| 979 | | , subParams = makeParams ["Object", "List", "Named"] |
| 980 | | , subReturns = mkType "Scalar::Perl5" |
| 981 | | , subBody = Prim $ \(inv:named:pos:_) -> do |
| 982 | | sv <- fromVal inv |
| 983 | | posSVs <- fromVals pos |
| 984 | | namSVs <- fmap concat (fromVals named) |
| 985 | | let svs = posSVs ++ namSVs |
| 986 | | found <- liftIO $ canPerl5 sv (tail name) |
| 987 | | found' <- liftIO $ if found |
| 988 | | then return found |
| 989 | | else canPerl5 sv "AUTOLOAD" |
| 990 | | if not found' then evalExp (App (Var name) Nothing (map (Val . PerlSV) (sv:svs))) else do |
| 991 | | env <- ask |
| 992 | | rv <- liftIO $ do |
| 993 | | envSV <- mkVal (VControl $ ControlEnv env) |
| 994 | | subSV <- vstrToSV $ tail name |
| 995 | | invokePerl5 subSV sv svs envSV (enumCxt $ envContext env) |
| 996 | | return $ case rv of |
| 997 | | [sv] -> PerlSV sv |
| 998 | | _ -> VList (map PerlSV rv) |
| 999 | | } |
| 1000 | | possiblyBuildMetaopVCode :: String -> Eval (Maybe VCode) |
| 1001 | | possiblyBuildMetaopVCode op' | "&prefix:[" `isPrefixOf` op', "]" `isSuffixOf` op' = do |
| 1002 | | -- Strip the trailing "]" from op |
| 1003 | | let op = drop 9 (init op') |
| 1004 | | -- We try to find the userdefined sub. |
| 1005 | | -- We use the first two elements of invs as invocants, as these are the |
| 1006 | | -- types of the op. |
| 1007 | | rv = findSub ("&infix:" ++ op) Nothing (take 2 $ args ++ [Val undef, Val undef]) |
| 1008 | | maybeM rv $ \code -> return $ mkPrim |
| 1009 | | { subName = "&prefix:[" ++ op ++ "]" |
| 1010 | | , subType = SubPrim |
| 1011 | | , subAssoc = "spre" |
| 1012 | | , subParams = makeParams ["List"] |
| 1013 | | , subReturns = mkType "Str" |
| 1014 | | , subBody = Prim $ \[vs] -> do |
| 1015 | | list_of_args <- fromVal vs |
| 1016 | | op2Fold (list_of_args) (VCode code) |
| 1017 | | } |
| 1018 | | -- Now we construct the sub. Is there a more simple way to do it? |
| 1019 | | possiblyBuildMetaopVCode op' | "&prefix:" `isPrefixOf` op', "\171" `isSuffixOf` op' = do |
| 1020 | | let op = drop 8 (init op') |
| 1021 | | possiblyBuildMetaopVCode ("&prefix:" ++ op ++ "<<") |
| 1022 | | possiblyBuildMetaopVCode op' | "&prefix:" `isPrefixOf` op', "<<" `isSuffixOf` op' = do |
| 1023 | | let op = drop 8 (init (init op')) |
| 1024 | | rv = findSub ("&prefix:" ++ op) Nothing [head $ args ++ [Val undef]] |
| 1025 | | maybeM rv $ \code -> return $ mkPrim |
| 1026 | | { subName = "&prefix:" ++ op ++ "<<" |
| 1027 | | , subType = SubPrim |
| 1028 | | , subAssoc = subAssoc code |
| 1029 | | , subParams = subParams code |
| 1030 | | , subReturns = mkType "List" |
| 1031 | | , subBody = Prim |
| 1032 | | (\x -> op1HyperPrefix code (listArg x)) |
| 1033 | | } |
| 1034 | | possiblyBuildMetaopVCode op' | "&postfix:\187" `isPrefixOf` op' = do |
| 1035 | | let op = drop 10 op' |
| 1036 | | possiblyBuildMetaopVCode ("&postfix:>>" ++ op) |
| 1037 | | possiblyBuildMetaopVCode op' | "&postfix:>>" `isPrefixOf` op' = do |
| 1038 | | let op = drop 11 op' |
| 1039 | | rv = findSub ("&postfix:" ++ op) Nothing [head $ args ++ [Val undef]] |
| 1040 | | maybeM rv $ \code -> return $ mkPrim |
| 1041 | | { subName = "&postfix:>>" ++ op |
| 1042 | | , subType = SubPrim |
| 1043 | | , subAssoc = subAssoc code |
| 1044 | | , subParams = subParams code |
| 1045 | | , subReturns = mkType "List" |
| 1046 | | , subBody = Prim |
| 1047 | | (\x -> op1HyperPostfix code (listArg x)) |
| 1048 | | } |
| 1049 | | possiblyBuildMetaopVCode op' | "&infix:\187" `isPrefixOf` op', "\171" `isSuffixOf` op' = do |
| 1050 | | let op = drop 8 (init op') |
| 1051 | | possiblyBuildMetaopVCode ("&infix:>>" ++ op ++ "<<") |
| 1052 | | possiblyBuildMetaopVCode op' | "&infix:>>" `isPrefixOf` op', "<<" `isSuffixOf` op' = do |
| 1053 | | let op = drop 9 (init (init op')) |
| 1054 | | rv = findSub ("&infix:" ++ op) Nothing (take 2 (args ++ [Val undef, Val undef])) |
| 1055 | | maybeM rv $ \code -> return $ mkPrim |
| 1056 | | { subName = "&infix:>>" ++ op ++ "<<" |
| 1057 | | , subType = SubPrim |
| 1058 | | , subAssoc = subAssoc code |
| 1059 | | , subParams = makeParams ["Any", "Any"] |
| 1060 | | , subReturns = mkType "List" |
| 1061 | | , subBody = Prim (\[x, y] -> op2Hyper code x y) |
| 1062 | | } |
| 1063 | | -- Taken from Pugs.Prim. Probably this should be refactored. (?) |
| 1064 | | possiblyBuildMetaopVCode _ = return Nothing |
| 1065 | | listArg [x] = x |
| 1066 | | listArg xs = VList xs |
| 1067 | | makeParams = map (\p -> p{ isWritable = isLValue p }) . foldr foldParam [] . map takeWord |
| 1068 | | takeWord = takeWhile isWord . dropWhile (not . isWord) |
| 1069 | | isWord = not . (`elem` "(),:") |
| 1070 | | findAttrs pkg = do |
| 1071 | | maybeM (findVar (':':'*':pkg)) $ \ref -> do |
| 1072 | | meta <- readRef ref |
| 1073 | | fetch <- doHash meta hash_fetchVal |
| 1074 | | fromVal =<< fetch "traits" |
| 1075 | | findWithPkg :: String -> String -> Eval (Maybe VCode) |
| 1076 | | findWithPkg pkg name = do |
| 1077 | | subs <- findSub' (('&':pkg) ++ "::" ++ tail name) |
| 1078 | | if isJust subs then return subs else do |
| 1079 | | findWithSuper pkg name |
| 1080 | | findWithSuper :: String -> String -> Eval (Maybe VCode) |
| 1081 | | findWithSuper pkg name = do |
| 1082 | | -- get superclasses |
| 1083 | | attrs <- fmap (fmap (filter (/= pkg) . nub)) $ findAttrs pkg |
| 1084 | | if isNothing attrs || null (fromJust attrs) then findSub' name else do |
| 1085 | | (`fix` (fromJust attrs)) $ \run pkgs -> do |
| 1086 | | if null pkgs then return Nothing else do |
| 1087 | | subs <- findWithPkg (head pkgs) name |
| 1088 | | if isJust subs then return subs else run (tail pkgs) |
| 1089 | | findSub' :: String -> Eval (Maybe VCode) |
| 1090 | | findSub' name = do |
| 1091 | | subSyms <- findSyms name |
| 1092 | | lens <- mapM argSlurpLen (unwrap $ maybeToList invs ++ args) |
| 1093 | | doFindSub (sum lens) subSyms |
| 1094 | | argSlurpLen :: Exp -> Eval Int |
| 1095 | | argSlurpLen (Val listMVal) = do |
| 1096 | | listVal <- fromVal listMVal |
| 1097 | | return $ length (vCast listVal :: [Val]) |
| 1098 | | argSlurpLen (Var name) = do |
| 1099 | | listMVal <- evalVar name |
| 1100 | | listVal <- fromVal listMVal |
| 1101 | | return $ length (vCast listVal :: [Val]) |
| 1102 | | argSlurpLen (Syn "," list) = return $ length list |
| 1103 | | argSlurpLen _ = return 1 -- XXX |
| 1104 | | doFindSub :: Int -> [(String, Val)] -> Eval (Maybe VCode) |
| 1105 | | doFindSub slurpLen subSyms = do |
| 1106 | | subs' <- subs slurpLen subSyms |
| 1107 | | -- let foo (x, sub) = show x ++ show (map paramContext $ subParams sub) |
| 1108 | | -- trace (unlines $ map foo $ sort subs') return () |
| 1109 | | return $ case sort subs' of |
| 1110 | | ((_, sub):_) -> Just sub |
| 1111 | | _ -> Nothing |
| 1112 | | subs :: Int -> [(String, Val)] -> Eval [((Bool, Bool, Int, Int), VCode)] |
| 1113 | | subs slurpLen subSyms = (liftM catMaybes) $ (`mapM` subSyms) $ \(_, val) -> do |
| 1114 | | sub@(MkCode{ subReturns = ret, subParams = prms }) <- fromVal val |
| 1115 | | let rv = return $ arityMatch sub (length (maybeToList invs ++ args)) slurpLen |
| 1116 | | maybeM rv $ \fun -> do |
| 1117 | | -- if deltaFromCxt ret == 0 then return Nothing else do |
| 1118 | | let pairs = map (typeOfCxt . paramContext) prms |
| 1119 | | `zip` (map unwrap $ maybeToList invs ++ args) |
| 1120 | | deltaCxt <- deltaFromCxt ret |
| 1121 | | deltaArgs <- mapM deltaFromPair pairs |
| 1122 | | let bound = either (const False) (const True) $ bindParams sub invs args |
| 1123 | | return ((isMulti sub, bound, sum deltaArgs, deltaCxt), fun) |
| 1124 | | deltaFromCxt :: Type -> Eval Int |
| 1125 | | deltaFromCxt x = do |
| 1126 | | cls <- asks envClasses |
| 1127 | | cxt <- asks envContext |
| 1128 | | return $ deltaType cls (typeOfCxt cxt) x |
| 1129 | | deltaFromPair (x, y) = do |
| 1130 | | cls <- asks envClasses |
| 1131 | | typ <- evalExpType y |
| 1132 | | return $ deltaType cls x typ |
| 1133 | | |
| 1134 | | evalExpType :: Exp -> Eval Type |
| 1135 | | evalExpType (Var var) = do |
| 1136 | | rv <- findVar var |
| 1137 | | case rv of |
| 1138 | | Nothing -> return $ typeOfSigil (head var) |
| 1139 | | Just ref -> evalValType (VRef ref) |
| 1140 | | evalExpType (Val val) = evalValType val |
| 1141 | | evalExpType (App (Val val) _ _) = do |
| 1142 | | sub <- fromVal val |
| 1143 | | return $ subReturns sub |
| 1144 | | evalExpType (App (Var "&new") (Just (Var (':':name))) _) = return $ mkType name |
| 1145 | | evalExpType (App (Var name) invs args) = do |
| 1146 | | sub <- findSub name invs args |
| 1147 | | case sub of |
| 1148 | | Just sub -> return $ subReturns sub |
| 1149 | | Nothing -> return $ mkType "Any" |
| 1150 | | evalExpType exp@(Syn syn _) | (syn ==) `any` words "{} []" = do |
| 1151 | | val <- evalExp exp |
| 1152 | | evalValType val |
| 1153 | | evalExpType (Cxt cxt _) | typeOfCxt cxt /= (mkType "Any") = return $ typeOfCxt cxt |
| 1154 | | evalExpType (Cxt _ exp) = evalExpType exp |
| 1155 | | evalExpType (Pos _ exp) = evalExpType exp |
| 1156 | | evalExpType (Pad _ _ exp) = evalExpType exp |
| 1157 | | evalExpType (Sym _ _ exp) = evalExpType exp |
| 1158 | | evalExpType (Stmts _ exp) = evalExpType exp |
| 1159 | | evalExpType (Syn "sub" [exp]) = evalExpType exp |
| 1160 | | evalExpType _ = return $ mkType "Any" |