Changeset 14953 for src/Pugs/Prim.hs

Show
Ignore:
Timestamp:
12/30/06 14:29:18 (23 months ago)
Author:
luqui
Message:

Bring pugs up-to-date on cmp and sort semantics.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim.hs

    r14933 r14953  
    181181op1 "sort" = \v -> do 
    182182    args    <- fromVal v 
    183     (valList, sortBy) <- case args of 
     183    (valList, sortByGiven) <- case args of 
    184184        (v:vs) -> do 
    185185            ifValTypeIsa v "Code" 
     
    189189                    (return (args, Nothing))) 
    190190        _  -> return (args, Nothing) 
    191     case sortBy of 
    192         Nothing -> do 
    193             strs    <- mapM fromVal valList 
    194             retSeq . map snd . sort $ (strs :: [VStr]) `zip` valList 
    195         Just subVal -> do 
    196             sub <- fromVal subVal 
    197             sorted <- (`sortByM` valList) $ \v1 v2 -> do 
    198                 rv  <- enterEvalContext (cxtItem "Int") $ App (Val sub) Nothing [Val v1, Val v2] 
    199                 int <- fromVal rv 
    200                 return (int <= (0 :: Int)) 
    201             retSeq sorted 
     191    sortBy <- case sortByGiven of 
     192        Nothing -> readVar (cast "&infix:cmp") 
     193        Just subVal -> return subVal 
     194    sub <- fromVal sortBy 
     195    sorted <- (`sortByM` valList) $ \v1 v2 -> do 
     196        rv  <- enterEvalContext (cxtItem "Int") $ App (Val sub) Nothing [Val v1, Val v2] 
     197        int <- fromVal rv 
     198        return (int <= (0 :: Int)) 
     199    retSeq sorted 
    202200op1 "Scalar::reverse" = \v -> do 
    203201    str     <- fromVal v 
     
    952950op2 "=>" = \x y -> return $ castV (x, y) 
    953951op2 "="  = \x y -> evalExp (Syn "=" [Val x, Val y]) 
    954 op2 "cmp"= op2Ord vCastStr 
     952op2 "cmp"= op2OrdNumStr 
    955953op2 "leg"= op2Ord vCastStr 
    956954op2 "<=>"= op2Ord vCastRat 
     
    15151513        EQ -> 0 
    15161514        GT -> 1 
     1515 
     1516isNumeric :: Val -> Bool 
     1517isNumeric (VNum {}) = True 
     1518isNumeric (VRat {}) = True 
     1519isNumeric (VInt {}) = True 
     1520isNumeric _ = False 
     1521 
     1522op2OrdNumStr :: Val -> Val -> Eval Val 
     1523op2OrdNumStr x y 
     1524    | isNumeric x && isNumeric y = op2Ord vCastRat x y 
     1525    | otherwise                  = op2Ord vCastStr x y 
    15171526 
    15181527op3Caller :: Type -> Int -> Val -> Eval Val 
     
    20692078\\n   Bool      left    ?&      safe   (Bool, Bool)\ 
    20702079\\n   Pair      right   =>      safe   (Any, Any)\ 
    2071 \\n   Int       non     cmp     safe   (Str, Str)\ 
     2080\\n   Int       non     cmp     safe   (Any, Any)\ 
    20722081\\n   Int       non     leg     safe   (Str, Str)\ 
    20732082\\n   Int       non     <=>     safe   (Num, Num)\