<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: define code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 04:39:41 GMT</pubDate>
    <description>DZone Snippets: define code</description>
    <item>
      <title>DEFINE dialect</title>
      <link>http://snippets.dzone.com/posts/show/1151</link>
      <description>&lt;code&gt;&lt;br /&gt;REBOL [&lt;br /&gt;    Title:   "#define dialected function"&lt;br /&gt;    File:    %define-dialect.r&lt;br /&gt;    Author:  "Gregg Irwin"&lt;br /&gt;    Version: 0.0.1&lt;br /&gt;    Date:    23-sep-2003&lt;br /&gt;    Purpose: {&lt;br /&gt;        Make it easier to map C #define statements. Eliminates&lt;br /&gt;        the need to manually call datatype conversion functions &lt;br /&gt;        for each item.&lt;br /&gt;    }&lt;br /&gt;    Comment: {&lt;br /&gt;        The block you pass to the function is a dialect. In the&lt;br /&gt;        dialect you can specify word-value pairs and functions&lt;br /&gt;        used to map different types of values as they are&lt;br /&gt;        processed.&lt;br /&gt;&lt;br /&gt;        MAP is used to tell the processor what function to call&lt;br /&gt;        when it encounters a particular datatype value. The&lt;br /&gt;        function specified should take a single value.&lt;br /&gt;&lt;br /&gt;            'map datatype function-name&lt;br /&gt;&lt;br /&gt;            ; call to-integer when a binary! value is found.&lt;br /&gt;            [map binary! to-integer]&lt;br /&gt;&lt;br /&gt;        Other than that, just specify a word followed by a value.&lt;br /&gt;        The resulting block will contain set-word! values for&lt;br /&gt;        each word, followed by its value - which may have been&lt;br /&gt;        converted by a function that was mapped to its original&lt;br /&gt;        datatype.&lt;br /&gt;    }&lt;br /&gt;    Example: {&lt;br /&gt;        do define [&lt;br /&gt;            map binary! to-integer&lt;br /&gt;            map issue!  to-integer&lt;br /&gt;&lt;br /&gt;            my-name "Gregg"&lt;br /&gt;            mapped-binary  #{00000001}&lt;br /&gt;            mapped-issue   #000fffff&lt;br /&gt;        ]&lt;br /&gt;        print [my-name mapped-binary mapped-issue]&lt;br /&gt;    }&lt;br /&gt;    library: [&lt;br /&gt;        level:    'intermediate&lt;br /&gt;        platform: 'all&lt;br /&gt;        type:     [function]&lt;br /&gt;        domain:   [dialect parse external-library]&lt;br /&gt;        tested-under: [view 1.2.8.3.1 on W2K]&lt;br /&gt;        support:  none&lt;br /&gt;        license:  none&lt;br /&gt;        see-also: none&lt;br /&gt;    ]&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;define: func [&lt;br /&gt;    block [any-block!]&lt;br /&gt;    /local type fn word val result map&lt;br /&gt;] [&lt;br /&gt;    result: make block length? block&lt;br /&gt;    map: copy []&lt;br /&gt;    either parse block [&lt;br /&gt;        some [&lt;br /&gt;            'map set type word! set fn word! (&lt;br /&gt;                type: do type&lt;br /&gt;                either word: find/skip map type 2 [&lt;br /&gt;                    change next word get fn&lt;br /&gt;                ][&lt;br /&gt;                    append map reduce [type get fn]&lt;br /&gt;                ]&lt;br /&gt;            )&lt;br /&gt;            | set word any-word! set val any-type! (&lt;br /&gt;                append result reduce [&lt;br /&gt;                    to set-word! word&lt;br /&gt;                    either fn: select map type? val [fn val][val]&lt;br /&gt;                ]&lt;br /&gt;            )&lt;br /&gt;        ]&lt;br /&gt;        to end&lt;br /&gt;    ] [result][none]&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;comment [ ; test examples&lt;br /&gt;    print mold defs: define [&lt;br /&gt;        map binary!  to-integer&lt;br /&gt;        map issue!   to-integer&lt;br /&gt;&lt;br /&gt;        PFD_DOUBLEBUFFER  #{00000001}&lt;br /&gt;        PFD_STEREO  #{00000002}&lt;br /&gt;        PFD_DRAW_TO_WINDOW  #00000004&lt;br /&gt;        GL_ALL_ATTRIB_BITS  #000fffff&lt;br /&gt;    ]&lt;br /&gt;    do defs&lt;br /&gt;    print [&lt;br /&gt;        PFD_DOUBLEBUFFER&lt;br /&gt;        PFD_STEREO&lt;br /&gt;        PFD_DRAW_TO_WINDOW&lt;br /&gt;        GL_ALL_ATTRIB_BITS&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    halt&lt;br /&gt;]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 13 Jan 2006 04:20:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1151</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
