Changeset 2938 for src/Pugs/Prim.hs

Show
Ignore:
Timestamp:
05/10/05 20:21:58 (4 years ago)
Author:
bsmith
svk:copy_cache_prev:
4498
Message:

Factored file test implementations into Pugs.Prim.FileTest?.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim.hs

    r2936 r2938  
    2929import Pugs.Prim.Keyed 
    3030import Pugs.Prim.Yaml 
     31import qualified Pugs.Prim.FileTest as FileTest 
    3132 
    3233op0 :: Ident -> [Val] -> Eval Val 
     
    250251op1 "rmdir" = boolIO removeDirectory 
    251252op1 "chdir" = boolIO setCurrentDirectory 
    252 op1 "-r"    = fileTestIO fileTestIsReadable 
    253 op1 "-w"    = fileTestIO fileTestIsWritable 
    254 op1 "-x"    = fileTestIO fileTestIsExecutable 
    255 op1 "-e"    = fileTestIO fileTestExists 
    256 op1 "-z"    = fileTestIO fileTestSizeIsZero 
    257 op1 "-s"    = fileTestIO fileTestFileSize 
    258 op1 "-f"    = fileTestIO fileTestIsFile 
    259 op1 "-d"    = fileTestIO fileTestIsDirectory 
     253op1 "-r"    = FileTest.isReadable 
     254op1 "-w"    = FileTest.isWritable 
     255op1 "-x"    = FileTest.isExecutable 
     256op1 "-e"    = FileTest.exists 
     257op1 "-z"    = FileTest.sizeIsZero 
     258op1 "-s"    = FileTest.fileSize 
     259op1 "-f"    = FileTest.isFile 
     260op1 "-d"    = FileTest.isDirectory 
    260261op1 "end"   = op1Cast (VInt . (-1 +) . (genericLength :: VList -> VInt)) 
    261262op1 "elems" = op1Cast (VInt . (genericLength :: VList -> VInt)) 
     
    12321233foldParam ('~':str) = \ps -> (((buildParam str "" "$?1" (Val VUndef)) { isLValue = False }) { isLazy = True }:ps) 
    12331234foldParam x         = doFoldParam x [] 
    1234  
    1235 -- filetest operators -- 
    1236  
    1237 -- Officially, these should return a stat object, which sometimes pretends 
    1238 -- to be a boolean, and may(?) return the filename in string context. 
    1239 -- DARCS was working on stat, and we should perhaps grab their work: 
    1240 --  http://www.abridgegame.org/pipermail/darcs-users/2005-February/005499.html 
    1241 -- They currently (2004-04-05) seem to be using: 
    1242 --  http://abridgegame.org/cgi-bin/darcs.cgi/darcs/win32/System/Posix.hs 
    1243 -- For the moment, these return filename and false or undef. 
    1244 -- Known Bugs: multiple stat()s are done, and filename isnt a boolean. 
    1245  
    1246 fileTestIO :: (Value n) => (n -> IO Val) -> Val -> Eval Val 
    1247 fileTestIO f v = do 
    1248     str <- fromVal =<< fromVal' v 
    1249     tryIO undef $ f str 
    1250  
    1251 fileTestIsReadable :: FilePath -> IO Val 
    1252 fileTestIsReadable f = do 
    1253     p <- getPermissions f 
    1254     let b = readable p 
    1255     return $ if b then castV f else VBool False 
    1256  
    1257 fileTestIsWritable :: FilePath -> IO Val 
    1258 fileTestIsWritable f = do 
    1259     p <- getPermissions f 
    1260     let b = writable p 
    1261     return $ if b then castV f else VBool False 
    1262  
    1263 fileTestIsExecutable :: FilePath -> IO Val 
    1264 fileTestIsExecutable f = do 
    1265     p <- getPermissions f 
    1266     let b = executable p || searchable p 
    1267     return $ if b then castV f else VBool False 
    1268  
    1269 fileTestExists :: FilePath -> IO Val 
    1270 fileTestExists f = do 
    1271     b1 <- doesFileExist f 
    1272     b2 <- doesDirectoryExist f 
    1273     return $ if b1 || b2 then castV f else VBool False 
    1274  
    1275 fileTestIsFile :: FilePath -> IO Val 
    1276 fileTestIsFile f = do 
    1277     b <- doesFileExist f 
    1278     return $ if b then castV f else VBool False 
    1279  
    1280 fileTestIsDirectory :: FilePath -> IO Val 
    1281 fileTestIsDirectory f = do 
    1282     b <- doesDirectoryExist f 
    1283     return $ if b then castV f else VBool False 
    1284  
    1285 fileTestFileSize :: FilePath -> IO Val 
    1286 fileTestFileSize f = do 
    1287     n <- statFileSize f 
    1288     return $ VInt n 
    1289  
    1290 fileTestSizeIsZero :: FilePath -> IO Val 
    1291 fileTestSizeIsZero f = do 
    1292     n <- statFileSize f 
    1293     return $ if n == 0 then VBool True else VBool False 
    12941235 
    12951236prettyVal :: Int -> Val -> Eval VStr