Changeset 7901

Show
Ignore:
Timestamp:
11/07/05 20:38:21 (3 years ago)
Author:
iblech
Message:

* t/builtins/numify.t: Added Larry's testcase posted to p6c (link to

nntp.perl.org included in the test).

* t/var/caller.t: "my $a is env" is spelled "env $a" now, according to latest S02 (r6537).
* Pugs.Parser: "+" is a valid twigil now.
* Pugs.AST.Scope, Pugs.Lexer: Added SEnv.

Files:
5 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/AST/Scope.hs

    r6432 r7901  
    99           | SLet    -- ^ Hypotheticalised (reverted upon failure) 
    1010           | STemp   -- ^ Temporary (reverted at scope exit) 
     11           | SEnv    -- ^ Environment (declared with @env@) 
    1112           | SMy     -- ^ Lexical 
    1213           | SOur    -- ^ Package 
  • src/Pugs/Lexer.hs

    r7752 r7901  
    275275    readScope "let"     = SLet 
    276276    readScope "temp"    = STemp 
     277    readScope "env"     = SEnv 
    277278    readScope _         = SGlobal 
    278279 
  • src/Pugs/Parser.hs

    r7848 r7901  
    19771977 
    19781978ruleTwigil :: RuleParser String 
    1979 ruleTwigil = option "" . choice . map string $ words " ^ * ? . : " 
     1979ruleTwigil = option "" . choice . map string $ words " ^ * ? . : + " 
    19801980 
    19811981ruleMatchPos :: RuleParser String 
  • t/builtins/numify.t

    r6894 r7901  
    44use Test; 
    55 
    6 plan 37; 
     6plan 40; 
    77 
    88is(int('-1.999'), -1, "int('-1.999') is -1"); 
     
    1111is(int('0o678'), 0o67, "int('0o678') is 0o67"); 
    1212is(int('3e4d5'), 3e4, "int('3e4d5') is 3e4"); 
     13 
     14# Per Larry: http://www.nntp.perl.org/group/perl.perl6.compiler/1134 
     15is(+'0012', 12, "+'0012' is 12"); 
     16is(+'0000',  0, "+'0000' is  0"); 
     17is(+'000a',  0, "+'000a' is  0 (illegal number)"); 
    1318 
    1419is(+'1.9e3', 1900, "+'1.9e3' is 1900"); 
  • t/var/caller.t

    r7663 r7901  
    44use Test; 
    55 
    6 plan 8; 
     6plan 17; 
    77 
    88{ 
    9   my $a is env = 9; 
     9  env $a = 9; 
    1010  my $sub = sub { $CALLER::a }; 
    1111 
     
    1717 
    1818{ 
    19   my $a is env = 9; 
     19  env $a = 9; 
    2020  my $sub2 = sub { $CALLER::a }; 
    2121  my $sub1 = sub { 
    22     my $a is env = 10; 
     22    env $a = 10; 
    2323    $sub2(); 
    2424  }; 
    2525 
    2626  { 
    27     my $a = 11; 
     27    env $a = 11; 
    2828    is $sub1(), 10, '$CALLER:: with nested subs works'; 
    29   } 
    30 } 
    31  
    32 { 
    33   my $a is env = 9; 
    34   my $sub2 = sub { $CALLER::a }; 
    35   my $sub1 = sub ($a is env) { 
    36     $sub2(); 
    37   }; 
    38  
    39   { 
    40     my $a = 11; 
    41     is $sub1(15), 15, '$CALLER:: works when accessing subparams, too'; 
    4229  } 
    4330} 
     
    4633  my $get_caller = sub { return sub { $CALLER::CALLER::a } }; 
    4734  my $sub1 = sub { 
    48     my $a is env = 3; 
     35    env $a = 3; 
    4936    $get_caller(); 
    5037  }; 
    5138  my $sub2 = sub { 
    52     my $a is env = 5; 
     39    env $a = 5; 
    5340    $get_caller(); 
    5441  }; 
     
    6552# L<S02/"Names" /The CALLER package refers to the lexical scope/> 
    6653{ 
    67   # $_ is always implicitly declared "is env". 
     54  # $_ is always implicitly declared "env". 
    6855  my sub foo () { $CALLER::_ } 
    6956  my sub bar () { 
     
    7360 
    7461  $_ = 23; 
    75   is bar(), 42, '$_ is implicitly declared "is env" (1)'; 
     62  is bar(), 42, '$_ is implicitly declared "env" (1)'; 
    7663} 
    7764 
    7865{ 
    79   # $_ is always implicitly declared "is env". 
     66  # $_ is always implicitly declared "env". 
    8067  # (And, BTW, $_ is lexical.) 
    8168  my sub foo () { $_ = 17; $CALLER::_ } 
     
    8673 
    8774  $_ = 23; 
    88   is bar(), 42, '$_ is implicitly declared "is env" (2)'; 
     75  is bar(), 42, '$_ is implicitly declared "env" (2)'; 
    8976} 
    9077 
    9178{ 
    9279  # ...but other vars are not 
    93   my sub foo { my $abc = 17; $CALLER::_ } 
     80  my sub foo { my $abc = 17; $CALLER::abc } 
    9481  my sub bar { 
    9582    my $abc = 42; 
     
    9986  my $abs = 23; 
    10087  dies_ok { bar() }, 
    101     'vars not declared "is env" are not accessible via $CALLER::_'; 
     88    'vars not declared "env" are not accessible via $CALLER::'; 
     89} 
     90 
     91# Vars declared with env() default to being rw in the creating scope and 
     92# readonly when accessed with $CALLER::. 
     93{ 
     94  env $foo = 42; 
     95  $foo++; 
     96  is $foo, 43, "env() vars are rw in the creating scope (1)"; 
     97} 
     98 
     99{ 
     100  env $foo = 42; 
     101  { $foo++ } 
     102  is $foo, 43, "env() vars are rw in the creating scope (2)"; 
     103} 
     104 
     105{ 
     106  my sub modify { $CALLER::foo++ } 
     107  env $foo = 42; 
     108  dies_ok { modify() }, 'env() vars are ro when accessed with $CALLER::'; 
     109} 
     110 
     111{ 
     112  my sub modify { $CALLER::_++ } 
     113  $_ = 42; 
     114  lives_ok { modify() }, '$_ is implicitly rw (1)'; 
     115  is $_, 43,             '$_ is implicitly rw (2)'; 
     116} 
     117 
     118{ 
     119  my sub modify { $CALLER::foo++ } 
     120  env $foo is rw = 42; 
     121  lives_ok { modify() }, 
     122      'env() vars declared "is rw" are rw when accessed with $CALLER:: (1)'; 
     123  is $foo, 43, 
     124      'env() vars declared "is rw" are rw when accessed with $CALLER:: (2)'; 
     125} 
     126 
     127=begin underspecced 
     128 
     129# Is $+foo really short for $CALLER::foo? S02 doesn't make this 100% clear. 
     130 
     131{ 
     132  my sub get_foo { try { $+foo } } 
     133  env $foo = 42; 
     134 
     135  is get_foo(), 42, '$+ is short for $CALLER::'; 
     136} 
     137 
     138=end underspecced 
     139 
     140=cut 
     141 
     142# Rebinding caller's variables -- legal? 
     143{ 
     144  my $other_var = 23; 
     145  my sub rebind_foo { $CALLER::foo := $other_var } 
     146  env $foo = 42; 
     147 
     148  lives_ok { rebind_foo() }, 'rebinding $CALLER:: variables works (1)'; 
     149  is $foo, 23,               'rebinding $CALLER:: variables works (2)'; 
     150  $other_var++; 
     151  is $foo, 24,               'rebinding $CALLER:: variables works (3)'; 
    102152} 
    103153