A different approach that is more self-documenting might be to use
accessors instead of a boa-constructor (perhaps just a tad less efficient,
although likely not that you'd ever notice):
scData new
test "SC" get-chunk first {
[ 4 7 bsread-string >>creator ]
[ 4 7 bsread-string >>type ]
...
} cleave
Alternatively, you could make a syntax word to generate your code for you
from a format spec, parsing the spec one token at a time and making your
constructor word:
SYNTAX: FORMAT:
scan-datum [ name>> "read-" prepend create-in ] keep
[
[ scan-token ";" = ] [
scan-token {
{ "ascii" [ scan-number scan-number '[ _ _ bsread-string ]
] }
{ "integer" [ scan-number '[ _ swap bsread ] ] }
} case ,
] until
] { } make swap '[ _ cleave _ boa ] ( bitreader -- object )
define-declared ;
TUPLE: scData creator type reverb-character ;
FORMAT: scData creator ascii 4 7 type ascii 4 7 reverb-character integer
7 ;
That makes a "read-scData" word that uses a bitreader.
Personally, I'm not sure thats an improvement over inlining all the
definitions, but you might feel otherwise.
Alternatively, you can use the macro system. A macro is basically a word
that can take an argument and produces a quotation. You can use
``expand-macros`` to see what the code expands to. I made a simple printf
example that you can read about to see how to make a simple macro:
http://re-factor.blogspot.com/2011/08/printf.html
On Sun, Dec 14, 2014 at 5:19 PM, Mark Green <m...@antelope.nildram.co.uk>
wrote:
>
> Thanks very much for your help. I'm now getting into unpacking data
> structures from the bitstreams, but this is getting me some horrible code,
> to whit stuff like this:
>
> TUPLE: scData creator type ver num name reverb-character reverb-prelpf
> reverb-time reverb-delay reverb-predelay reverb-level
> reverb-selected chorus-prelpf chorus-feedback chorus-delay chorus-rate
> chorus-depth chorus-sendrev chorus-senddelay
> chorus-level chorus-selected delay-prelpf delay-timecenter
> delay-timeratioleft delay-timeratioright delay-levelcenter
> delay-levelleft delay-levelright delay-feedback delay-sendreverb
> delay-level delay-selected masterbar-recall-register
> index-icon bassoon edited junk unknown ;
>
> : parse-sc ( -- head ) test "SC" get-chunk first
> { [ 4 7 bsread-string ] ! Creator
> [ 4 7 bsread-string ] ! Type
> [ 4 7 bsread-string ] ! Ver
> [ 4 7 bsread-string ] ! Num
> [ 8 7 bsread-string ] ! Name
> [ 7 swap bsread ] ! Reverb Character
> [ 7 swap bsread ] ! Reverb Prelpf
> ... And so on for a whole bunch of lines ...
> [ 7 swap bsread ] ! Bassoon
> [ 7 swap bsread ] ! Edited
> [ 15 swap bsread ] ! Dummy
> [ 57 7 bsread-string ] ! Unknown
> } cleave scData boa ;
>
> Now, I know I could make this much shorter by not repeating the quotations
> but that has the problem that I then lose the comments and easy
> editability. But! I have dipped my toe into LISP and it would be rather
> fantastic to have a macro so that I could write something like:
>
> FORMAT: scData creator ascii 4 7 type ascii 4 7 ver ascii 4 7 num ascii 4
> 7 name ascii 8 7 reverb-character integer 8 1 ...
>
> And have that create the tuple and function above. Of course, I have no
> idea how to do that - I've tried playing with the MACRO: and SYNTAX:
> statements but I don't know how SYNTAX: takes input and MACRO: seems to
> work using quotations in a way I'm not really used to from LISP style - I'm
> guessing I need a whole new world of understanding the parser to do this.
> Any inspirations?
>
> Mark
>
>
>
>
> ------------------------------------------------------------------------------
> 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