r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[rebcode] Rebcode discussion

BrianH
18-Oct-2005
[513x2]
I'll check back here later - time to sleep :)
Here's a refined and commented version:

use [dest here rule r1 r2 r3 rs0 rs1 rs2 rs3] [
    ; Initialize the new rules

    foreach rule [r1 r2 r3 rs0 rs1 rs2 rs3] [set rule make block! 10]
    ; Set the exceptions
    insert rs1 ['label word! | '?? word |]
    ; Build the new rules based on the standard rules
    parse rebcode*/opcode-rule [
        ; Note: Opcodes currently take at most 3 parameters
        some [here: lit-word! [
            '| (
                insert tail rs0 reduce [here/1 '|]
            ) |
            'word! '| (

                unless find rs1 here/1 [insert tail r1 reduce [here/1 '|]]
            ) |
            'word! [block! | word!] '| (

                unless find rs2 here/1 [insert tail r2 reduce [here/1 here/3 '|]]
            ) |
            'word! [block! | word!] [block! | word!] '| (

                unless find rs3 here/1 [insert tail r3 reduce [here/1 here/3 here/4 
                '|]]
            ) |
            into ['integer! '| 'word! | 'word! '| 'integer!] [
                '| (
                    unless find rs1 here/1 [
                        insert tail r1 reduce [here/1 '|]

                        insert tail rs1 reduce [here/1 'integer! '|]
                    ]
                ) |
                [block! | word!] '| (
                    unless find rs2 here/1 [
                        insert tail r2 reduce [here/1 here/3 '|]

                        insert tail rs2 reduce [here/1 'integer! here/3 '|]
                    ]
                )
            ]
            [block! | word!] [
                '| (
                    insert insert tail rs1 copy/part here 2 '|
                ) |
                [block! | word!] [
                    '| (

                        insert insert tail rs2 copy/part here 3 '|
                    ) |
                    [block! | word!] '| (

                        insert insert tail rs3 copy/part here 4 '|
                    )
                ]
            ]
        ]]
    ]
    ; Clear the trailing | from each new rule

    foreach rule [r1 r2 r3 rs0 rs1 rs2 rs3] [clear back tail get rule]
    ; Define the new rewrite rules and install them

    ; Note: A set-word establishes the default word parameter. The rules

    ;       convert this to an explicit parameter one opcode at a time.
    rebcode-define [

        set dest set-word! here: r1  #==> (here/1) (to-word dest) (dest) 
        .

        set dest set-word! here: r2  #==> (here/1) (to-word dest) (here/3) 
        (dest) .

        set dest set-word! here: r3  #==> (here/1) (to-word dest) (here/3) 
        (here/4) (dest) .
        set dest set-word! here: rs0 #==> (here/1) (dest) .

        set dest set-word! here: rs1 #==> (here/1) (here/2) (dest) .

        set dest set-word! here: rs2 #==> (here/1) (here/2) (here/3) (dest) 
        .

        set dest set-word! here: rs3 #==> (here/1) (here/2) (here/3) (here/4) 
        (dest) .
        set dest set-word! #==> .
    ]
]
Volker
18-Oct-2005
[515x2]
Yes Brian, you got it right. A set-word is the first argument to 
all following instructions. I am not about it. Its more like expressions, 
Makes gouping easier imho, like 
 string-char: pick string 1 eq char
instead of
 pick string-char string 1
 eq string-char char

My experimtens:  http://polly.rebol.it/test/test/rebcode/altsyn1.r
. Have to look into Gabs rewrite-rules. http://polly.rebol.it/test/test/rebcode/altsyn1.r
about
  insert [] 1
would be
  [] insert 1
a bit like smalltalk-syntax.
BrianH
18-Oct-2005
[517x2]
I think it is a bit more difficult to understand, but it made for 
a good experiment in rebuilding the standard opcode rules. That code 
framework will come in handy for me later.
By the way, I showed my father both dialects, yours and the current 
assembler. I had to explain what yours was doing; the standard dialect 
he just got, although he asked what a few of the opcodes meant. He's 
not a programmer (at least not recently) so that speaks well for 
the readability of the rebcode dialect.
Oldes
18-Oct-2005
[519x3]
How to traverse with series in rebcode? I wanted to make ucs2 encoder 
using rebcode but found out, that I'm not able to traverse the series 
by 2 chars
x: rebcode[str][copy s2 str -1 until [copy s s2 2 print s next s2 
tail? s2]] x "abcdefgh"
gives error
BrianH
18-Oct-2005
[522]
skip str 2
Oldes
18-Oct-2005
[523x6]
that's the same error
that was just a quick example
And other thing is, that I'm still missing better binary support
for example:
>> to-integer #{FF00}
== 65280
>> x: rebcode[][set b #{FF00} to-int i b print i] x
== 0
that's no good:( I cannot use rebcode to speed up any from my RSWF 
dialect functions as all of them are base on dealing with datatypes 
like UI16 UI32 SI16 SI32, FP and other which simply Rebol do not 
support:(
BrianH
18-Oct-2005
[529]
Are deciding that REBOL is bigendian? What about when it runs on 
x86 machines?
Oldes
18-Oct-2005
[530x4]
what about to add some options switch?
if I want to make binary structure, I now what binary type it is
and I want to make binaries all the time:(
event in the UTF8 you can specify if it is bigendian in the first 
bytest of the text
BrianH
18-Oct-2005
[534]
I tend to use struct! for that, when available. That is what I suggested 
struct! in REBOL/Core, and they agreed but haven't implemented it 
yet. I'm hoping for 2.7.0 :)
Oldes
18-Oct-2005
[535x5]
Me as well, but that is only for signed integers
Most of the time I have to use these ugly functions:
ints-to-sbs: func[

 ints [block!]	 "Block of integers, that I want to convert to SBs"

 /complete l-bits "Completes the bit-stream => l-bits stores the nBits 
 info of the values"
	;/maxb mb
	/local b b2 l bits sb
][
	ints: reduce ints
	max-bits: 0
	bits: make block! length? ints
	foreach i ints [
		;b: enbase/base load rejoin ["#{" to-hex i "}"] 2
		b: enbase/base head reverse int-to-ui32 i 2
		b: find b either i < 0 [#"0"][#"1"]
		b: copy either none? b [either i >= 0 ["00"]["11"]][back b]
		;insert b either i >= 0 [#"0"][#"1"]
		if max-bits < l: length? b [max-bits: l]
		append bits b
	]
	foreach b bits [
		if max-bits > l: length? b [
			insert/dup b b/1 max-bits - l
		]
	]
	either complete [
		sb: int-to-bits max-bits l-bits
		foreach b bits [insert tail sb b]
		sb
	][	
		bits
	]
]

int-to-FB: func[i /local x y fb][
	x: to integer! i
	y: to integer! (either x = 0 [i][i // x]) * 65535

 fb: rejoin [either x = 0 ["0"][first ints-to-sbs to block! x] int-to-bits 
 y 16]
	if all [x = 0 i < 0][fb/1: #"1"]
	fb
]
Unfortunately I'm not able to do it in Rebcode:(
Never mind, I will wait, maybe one day it will come.
BrianH
18-Oct-2005
[540x5]
In the meanwhile, ou can do it yourself:

pickz hi b 0
pickz lo b 1
lsl hi 8
add hi lo
skip b 2
for bigendian
Better yet, change the add to or, it should be faster.
Less likely to cause problems with large unsigned numbers too, it 
you are implementing unsigned int32s.
What are you trying to do with that function? I'm having a little 
trouble understanding its purpose, though I seem to get most of the 
parts.
Pekr
18-Oct-2005
[545x2]
Oldes - put it in RAMBO - or just guys let's decide what channel 
will we use for last-minute suggestions! Remember - if you don't 
get your desired functionality now, your wait might be quite long 
...
And posting your code here I can guarantee you, that Carl will not 
scroll thru them ;-)
BrianH
18-Oct-2005
[547]
The code I've posted here recently is for you all. I've posted the 
request code to RAMBO.
Pekr
18-Oct-2005
[548]
My suggestion goes mainly to Oldes now, who wishes to have rebcode 
supporting binary datatype and ability to switch endianness .....
Geomol
18-Oct-2005
[549x2]
In rebcode, the pick opcode can be used with both binaries and images. 
If you have an image, it's rgb values can be reached as image/rgb, 
and that is a binary serie. Each value is 0-255, and you then have 
to multiply the index by 3 and use an offset to reach the red, green 
or blue component of a pixel. It's also possible to work on the image 
as an image! datatype, and then it's not necessary to multiply the 
index by 3, as the full pixel is addressed, but the value isn't a 
tuple, as we're used to, but the binary version of a tuple as an 
integer. E.g. green (0.255.0 or #{00ff00}) is 65'280.


Now to my questions. Are there shortcuts or tricks to make it easier 
to work with pixel-valus in rebcode? Should it be possible to use 
tuples, or will that slow things down too much? What do others do?
Series in rebcode are offset-1 based like normal (except image positions 
in DRAW). Would it be a good idea to make them offset-zero based 
in rebcode? Example: if I wanna read a pixel value at a certain position 
in REBOL, I could write:
image/1
or
first skip image position
(If position was 0x0, I'll get the first pixel.)

If I pick an image in rebcode at offset 0, I get an out of range 
error. It's a tough decision, but what seems most 'correct'?
BrianH
18-Oct-2005
[551x2]
Why chose?
1-based: pick x s 1
0-based: pickz x s 0
Rebcode supports both.
Geomol
18-Oct-2005
[553]
LOL thanks! :-)
BrianH
18-Oct-2005
[554x4]
Oldes, after some testing, it seems that to-integer always acts big-endian. 
As long as that is predictable, cool!
I am writing int-to-binary conversions in rebcode that support lengths 
of binary from 1 to 4 bytes, little and big endian. I already wrote 
binary-to-int conversions in rebcode, 16 and 32 bit, little and bigendian, 
before it occured to me to check the behavior of to-integer. I'll 
speed-test the rebcode-versus-REBOL methods for comparison.
I'm writing these functions to figure out how to do Duff's Device 
efficiently in rebcode :)
Here's an example of using something like Duff's Device to do loop 
unrolling in rebcode.


binary-to-int16-be: rebcode [b [any-string!] /local x y l dest offs] 
[
    ; Get the length, bounded
    length? l b
    min l 2
    ; Set the initial value
    set x 0
    ; Switch on the length
    set offs [16 6 2]
    pickz dest offs l
    braw dest
    ; Set the low byte
    pickz x b 1
    ; Add the high byte
    pickz y b 0
    lsl y 8
    or x y
    ; Done!
    return x
]
Oldes
21-Oct-2005
[558x4]
>> binary-to-int16-be #{0001}
== none
okay, I submited to Rambo wish for better support for binary conversions, 
because I reaaly don't know why I should use image to store integer 
arrays if I want to deal with them in Rebcode
And I sould also write one, which points out, that there is probably 
no image alpha channel support in the rebcode
hmm, there is, but just with signed integers:)
Cyphre
21-Oct-2005
[562]
you can access the alpha channel using the 32bit integer vaule in 
IMAGE! array.