Changeset 7925

Show
Ignore:
Timestamp:
11/10/05 18:33:20 (3 years ago)
Author:
gaal
Message:

* when CODE:

  • given { when &sub { ... } } works for 0- and 1-arity subs.
  • still TODO: when { bare closure }, as this requires parser tweaking.
  • tests.
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim/Match.hs

    r7879 r7925  
    1010import Pugs.Types 
    1111import Pugs.Config 
     12import Pugs.Prim.Code 
    1213import qualified RRegex.PCRE as PCRE 
    1314import qualified Data.Map as Map 
     
    8283op2Match :: Val -> Val -> Eval Val 
    8384 
     85op2Match _ y@(VCode _) = do 
     86    (arity :: Int) <- fromVal =<< op1CodeArity y 
     87    res <- fromVal =<< case arity of 
     88        0 -> evalExp $ App (Val y) Nothing [] 
     89        1 -> do 
     90             topic <- readVar "$_" 
     91             evalExp $ App (Val y) Nothing [Val topic] 
     92        _ -> fail ("Unexpected arity in smart match: " ++ (show arity)) 
     93    return $ VBool $ res 
     94 
    8495op2Match x (VRef (MkRef (IScalar sv))) | scalar_iType sv == mkType "Scalar::Const" = do 
    8596    y' <- scalar_fetch' sv 
  • t/statements/given.t

    r7924 r7925  
    44use Test; 
    55 
    6 plan 48; 
     6plan 49; 
    77 
    88=kwid 
     
    244244        } 
    245245    '; 
    246     ok($x, 'given tests closures for truth', :todo); 
     246    ok($x, 'given tests 0-arg closures for truth', :todo); 
     247} 
     248 
     249# given + closure with 0-arg code 
     250{ 
     251    my $x = 41; 
     252    sub always_true { bool::true } 
     253    given 1 { 
     254        when &always_true { $x++ } 
     255    } 
     256    is($x, 42, 'given tests 0-arg subs for truth'); 
    247257} 
    248258 
     
    250260{ 
    251261    my $x = 41; 
    252     sub setx ($value) { $value == 41 } 
    253     eval ' 
    254         given 1 { 
    255             when &setx { $x++ } 
    256         } 
    257     '; 
    258     is($x, 42, 'given tests closures for truth', :todo); 
    259 } 
    260  
     262    sub maybe_true ($value) { $value eq "mytopic" } 
     263    given "mytopic" { 
     264        when &maybe_true { $x++ } 
     265    } 
     266    is($x, 42, 'given tests 1-arg subs for truth'); 
     267} 
     268