Changeset 3473
- Timestamp:
- 05/20/05 00:14:37 (4 years ago)
- svk:copy_cache_prev:
- 5016
- Location:
- src/Pugs
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Eval.hs
r3469 r3473 809 809 -- We use the first two elements of invs as invocants, as these are the 810 810 -- types of the op. 811 userdefined_sub <- findSub ("&infix:" ++ op) (take 2 invs) [] 812 subbody <- return $ case userdefined_sub of 813 -- If we've found a userdefined sub, we use it. 814 (Just sub) -> Prim $ \_ -> do 815 list_of_args <- return $ map evaluate invs 816 list_of_args' <- mapM (\a -> do { z <- a; return z }) list_of_args 817 op2Fold (VList list_of_args') (VCode sub) 818 -- Else, we use the code as defined in Pugs.Prim 819 _ -> Prim $ f op 811 code <- findSub ("&infix:" ++ op) (take 2 (invs ++ [Val undef, Val undef])) [] 812 if isNothing code then return Nothing else do 813 let subBody = const $ do 814 list_of_args <- mapM evaluate invs 815 op2Fold (VList list_of_args) (VCode $ fromJust code) 820 816 -- Now we construct the sub. Is there a more simple way to do it? 821 code <- return $ mkPrim 822 { subName = "&prefix:[" ++ op ++ "]" 823 , subType = SubPrim 824 , subAssoc = "spre" 825 , subParams = params 826 , subReturns = mkType "Str" 827 , subBody = subbody 828 } 829 return $ Just code 817 return . Just $ mkPrim 818 { subName = "&prefix:[" ++ op ++ "]" 819 , subType = SubPrim 820 , subAssoc = "spre" 821 , subParams = params 822 , subReturns = mkType "Str" 823 , subBody = Prim subBody 824 } 830 825 where 831 826 -- Taken from Pugs.Prim. Probably this should be refactored. (?) 832 f op = \[a] -> op1 ("[" ++ op ++ "]") a833 827 prms' = map takeWord ["(List)"] 834 828 prms'' = foldr foldParam [] prms' -
src/Pugs/Prim.hs
r3466 r3473 252 252 else return undef 253 253 254 op1 ('[':op) = op1Fold . op2 . init $ op255 254 op1 "rand" = \v -> do 256 255 x <- fromVal v … … 1197 1196 \\n Str left ~^ (Str, Str)\ 1198 1197 \\n Str left ?| (Str, Str)\ 1199 \\n Pair non=> (Any, Any)\1198 \\n Pair right => (Any, Any)\ 1200 1199 \\n Int non cmp (Str, Str)\ 1201 1200 \\n Int non <=> (Num, Num)\ -
src/Pugs/Prim/List.hs
r3372 r3473 1 1 module Pugs.Prim.List ( 2 op0Zip, op1 Fold, op1Pick, op1Sum,2 op0Zip, op1Pick, op1Sum, 3 3 op2Fold, op2Grep, op2Map, op2Join, 4 4 sortByM, … … 22 22 zipRest [] = [] 23 23 zipRest (_:xs) = xs 24 25 op1Fold :: (Val -> Val -> Eval Val) -> Val -> Eval Val26 op1Fold op v = do27 args <- fromVal v28 case args of29 (a:as) -> foldM op a as30 _ -> return undef31 24 32 25 op1Pick :: Val -> Eval Val … … 58 51 op2Fold sub@(VCode _) list = op2Fold list sub 59 52 op2Fold list sub = do 53 code <- fromVal sub 60 54 args <- fromVal list 61 55 if null args then return undef else do … … 64 58 local (\e -> e{ envContext = cxtItemAny }) $ do 65 59 evl (App (Val sub) [Val x, Val y] []) 66 foldM doFold (head args) (tail args) 60 case subAssoc code of 61 "left" -> foldM doFold (head args) (tail args) 62 "right" -> do 63 let args' = reverse args 64 foldM (flip doFold) (head args') (tail args') 65 "chain" -> callCC $ \esc -> do 66 let doFold' x y = do 67 val <- doFold x y 68 case val of 69 VBool False -> esc val 70 _ -> return val 71 foldM doFold' (head args) (tail args) 72 _ -> fail $ "Cannot reduce over associativity:" ++ show (subAssoc code) 67 73 68 74 op2Grep :: Val -> Val -> Eval Val
