Hi Mark,

You can pretty much do anything you want, defining a tuple class is
relatively simple.  I'm not a huge fan of making magical words (meaning
creating "read-scData" is a side effect and you can't grep the source tree
for it, but that might be okay for your use case).

Below is one way where your FORMAT word scans the name "scData", make a
word "read-scData" and a class "scData", then parses the slots and reader
words, then makes the tuple class and define the bitreader word for it.

    SYNTAX: FORMAT:
        scan-token [ "read-" prepend create-in ] [ create-class-in ] bi

        [
            [ scan-token dup ";" = ] [
                scan-token {
                    { "ascii" [ scan-number scan-number '[ _ _
bsread-string ] ] }
                    { "integer" [ scan-number '[ _ swap bsread ] ] }
                } case 2array ,
            ] until drop
        ] { } make

        [ tuple swap keys define-tuple-class ]
        [
            values swap '[ _ cleave _ boa ]
            ( bitreader -- object ) define-declared
        ] 2bi ;

Using it is like this:

    IN: scratchpad  FORMAT: scData creator ascii 4 7 type ascii 4 7
reverb-character integer 7 ;

    IN: scratchpad \ scData see
    IN: scratchpad
    TUPLE: scData creator type reverb-character ;

    IN: scratchpad \ read-scData see
    USING: combinators kernel ;
    IN: scratchpad
    : read-scData ( bitreader -- object )
        {
            [ 4 7 bsread-string ]
            [ 4 7 bsread-string ]
            [ 7 swap bsread ]
        } cleave \ scData boa ;

Everything in Factor is kind of a DSL, so you can get as fancy with this as
you want.  You also might try and build something that parses a bit more
like our other definitions if you wanted to:

    FORMAT: scData
        { creator ascii 4 7 }
        { type ascii 4 7 }
        { reverb-character integer 7 } ;

I leave doing that up to you though.  ;-)

Also, you might want to factor (ahem!) some of the logic out into a
parse-format-definition word, as in most languages, we try and keep the
length of word definitions down.







On Sun, Dec 14, 2014 at 6:31 PM, Mark Green <m...@antelope.nildram.co.uk>
wrote:
>
> That is amazing and does actually make sense to me.. Thanks!  Can the
> syntax expression define the tuple too? I see there is a compiler word for
> that but I'm not sure how to fit it in.
>
> ------------------------------------------------------------------------------
> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
> from Actuate! Instantly Supercharge Your Business Reports and Dashboards
> with Interactivity, Sharing, Native Excel Exports, App Integration & more
> Get technology previously reserved for billion-dollar corporations, FREE
>
> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to