Changeset 5008

Show
Ignore:
Timestamp:
06/26/05 19:19:20 (4 years ago)
Author:
iblech
svk:copy_cache_prev:
6856
Message:

Syntax but ($obj but {...}).
* t/oo/syntax-but.t -- Fixed test.
* Prelude -- Added Pugs::Internals helper sub.
* Pugs.Eval -- Implemented syntax but in the interpreter core...
* Pugs.Compile -- ...and in the compiler part of Pugs.

Files:
4 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Compile.hs

    r4955 r5008  
    342342        let op = "&infix:" ++ init syn 
    343343        compile $ Syn "=" [lhs, App (Var op) Nothing [lhs, exp]] 
     344    compile (Syn "but" [obj, block]) = 
     345        compile $ App (Var "&Pugs::Internals::but_block") Nothing [obj, block] 
    344346    compile exp = compError exp 
    345347 
  • src/Pugs/Eval.hs

    r4992 r5008  
    339339        , subCont = cont 
    340340        } 
     341 
     342reduceSyn "but" [obj, block] = do 
     343    evalExp $ App (Var "&Pugs::Internals::but_block") Nothing [obj, block] 
    341344 
    342345reduceSyn name [cond, bodyIf, bodyElse] 
  • src/perl6/Prelude.pm

    r5006 r5008  
    181181    $ret; 
    182182} 
     183 
     184sub Pugs::Internals::but_block ($obj, Code $code) { 
     185    $code($obj); 
     186    $obj; 
     187} 
  • t/oo/syntax-but.t

    r5006 r5008  
    1717#   }; 
    1818 
    19 plan 4; 
     19plan 7; 
    2020 
    21 class SampleClass { has $.var } 
    22  
     21# Without an own class 
    2322{ 
    2423  my $was_in_but_block; 
    2524  my $topic_in_but_block; 
    2625 
    27   my $obj = eval 'SampleClass.new but { 
     26  my $num = 3 but { 
    2827    $was_in_but_block++; 
    29     $topic_in_but_block++; 
     28    $topic_in_but_block = $_; 
     29    23; 
     30  }; 
     31 
     32  is $num,                3, "syntax but worked on a literal"; 
     33  ok $was_in_but_block,      "syntax but on a literal was executed"; 
     34  is $topic_in_but_block, 3, "topic in syntax but on a literal was correct"; 
     35} 
     36 
     37# With an own class 
     38{ 
     39  class SampleClass { has $.attr } 
     40 
     41  my $was_in_but_block; 
     42  my $topic_in_but_block; 
     43 
     44  my $obj = SampleClass.new but { 
     45    $was_in_but_block++; 
     46    $topic_in_but_block = $_; 
    3047    .attr = 42; 
    3148    23; 
    32   }'; 
     49  }; 
    3350 
    3451  ok $was_in_but_block, 'syntax but ($obj but {...}) was executed'; 
    3552  cmp_ok $topic_in_but_block, &infix:<=:=>, $obj, 
    3653    'topic in syntax but ($obj but {...}) was correct'; 
    37   is try { $obj.attr }, 42, "attribute setting worked correctly in syntax but"; 
    38   cmp_ok $obj, &infix:<!=>, 23, "syntax but returned the original object"; 
     54  my $attr = try { $obj.attr }; 
     55  is $attr, 42, "attribute setting worked correctly in syntax but"; 
     56  cmp_ok $obj, &infix:<~~>, SampleClass, "syntax but returned the original object"; 
    3957}