Paste: Working with macros

Author: Jim Mack
Mode: factor
Date: Mon, 18 Jan 2010 21:37:53
Plain Text |
USING: accessors arrays kernel locals present  classes.tuple io
sequences prettyprint fry macros nested-comments combinators ;
IN: learn

! place holder words
: package ( content id name class -- html ) 4array ;

: .<> ( seq --  ) . ;
: id-for-html ( tpl -- id ) id>> present "." prepend  ;

TUPLE: testing id firstname lastname age ;
: <testing> ( id fn ln age -- tpl ) testing boa ; 
: testdata ( -- tpl ) 1 "jim" "mack" 46 <testing> ;
: testing-schema ( -- seq )
    {   { "First Name" [ firstname>> ] }
        { "Last Name" [ lastname>> ] }
        { "Age" [ age>> ] } } ; inline
        
:  manual-row ( tpl -- seq ) 
    V{ } clone swap {
    [ [ firstname>> ] [ id-for-html ] bi "firstname" dup package  suffix ]
    [ [ lastname>> ] [ id-for-html ] bi "lastname" dup package   suffix ]
    [ [ age>> ] [ id-for-html ] bi "age" dup package   suffix ] 
    } cleave  ; 
  
! these both work as expected  
 testdata manual-row  . 
: t1 ( -- ) testdata manual-row  . ;
 
 ! attempt to build a macro just from the same code
 MACRO: manual-row2 ( tpl -- seq )
    [ 
        V{ } clone swap {
        [ [ firstname>> ] [ id-for-html ] bi "firstname" dup package  suffix ]
        [ [ lastname>> ] [ id-for-html ] bi "lastname" dup package   suffix ]
        [ [ age>> ] [ id-for-html ] bi "age" dup package   suffix ] 
        } cleave
    ] ; inline

! works as expected
 testdata manual-row2 . 

! this has unbalanced branches drop/call. Don't know what that means
: t2 ( -- ) testdata manual-row2 . ; 


  

Annotation: correct stack comment for manual-row2, still problem with t2

Author: Jim Mack
Mode: factor
Date: Mon, 18 Jan 2010 21:50:25
Plain Text |
USING: accessors arrays kernel locals present  classes.tuple io
sequences prettyprint fry macros nested-comments combinators ;
IN: learn

! place holder words
: package ( content id name class -- html ) 4array ;

: .<> ( seq --  ) . ;
: id-for-html ( tpl -- id ) id>> present "." prepend  ;

TUPLE: testing id firstname lastname age ;
: <testing> ( id fn ln age -- tpl ) testing boa ; 
: testdata ( -- tpl ) 1 "jim" "mack" 46 <testing> ;
: testing-schema ( -- seq )
    {   { "First Name" [ firstname>> ] }
        { "Last Name" [ lastname>> ] }
        { "Age" [ age>> ] } } ; inline
        
:  manual-row ( tpl -- seq ) 
    V{ } clone swap {
    [ [ firstname>> ] [ id-for-html ] bi "firstname" dup package  suffix ]
    [ [ lastname>> ] [ id-for-html ] bi "lastname" dup package   suffix ]
    [ [ age>> ] [ id-for-html ] bi "age" dup package   suffix ] 
    } cleave  ; 
  
! these both work as expected  
 testdata manual-row  . 
: t1 ( -- ) testdata manual-row  . ;
 
 ! attempt to build a macro just from the same code
 MACRO: manual-row2 (  --  )
    [ 
        V{ } clone swap {
        [ [ firstname>> ] [ id-for-html ] bi "firstname" dup package  suffix ]
        [ [ lastname>> ] [ id-for-html ] bi "lastname" dup package   suffix ]
        [ [ age>> ] [ id-for-html ] bi "age" dup package   suffix ] 
        } cleave
    ] ; inline

! works as expected
 testdata manual-row2 . 

! this has unbalanced branches drop/call. Don't know what that means
: t2 ( -- ) testdata manual-row2 . ; 


  

New Annotation

Summary:
Author:
Mode:
Body: