• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[#Red] Red language group

DocKimbel
21-Dec-2012
[4755]
I have added a new function type today: routine!. It allows to write 
a Red/System function in a Red program. The compiler will marshal 
(or type-cast) the arguments back and forth automatically.

Here is the Fibonacci example rewritten as a routine:

Red [ ]

fibonacci: routine [
    n          [integer!]
    return: [integer!]
][
    either n < 2 [
        n
    ][
        (fibonacci n - 1) + (fibonacci n - 2)
    ]
]


The function body is Red/System code, so it will run at full Red/System 
speed.


Integer! and logic! values are converted automatically, other Red 
datatypes are passed boxed but type-casted as Red/System counterparts 
(as defined in the Red runtime). Hint: floats will be converted automatically 
too.

So, passing and processing a block! series would look like this:

Red [ ]

add-one: routine [
    blk       [block!]
    return: [block!]
    /local value tail int
][
    value: HEAD(blk)
    tail: TAIL(blk)
	
    while [value < tail][
        if TYPE(value) = TYPE_INTEGER [
                int: as red-integer! value
                int/value: int/value + 1
        ]
        value: value + 1
    ]
    RETURN(blk)
]


I haven't yet released the code, it needs a bit more work, it should 
be ready by tomorrow.


The purpose of routine! datatype is to provide access to ultra-fast 
and low-level code for Red program in a simple way. The design is 
not yet fully set in stone, so suggestions and comments are welcome.
Endo
21-Dec-2012
[4756]
That's cool, it's something like rebcode.

How about something like having a block of Red/System code inside 
Red code?

System [...some R/S code...]
Jerry
21-Dec-2012
[4757]
Endo, what you want is described in Section 17 in the Red/System 
Spec   http://static.red-lang.org/red-system-specs.html#section-17
Gregg
21-Dec-2012
[4758]
Very cool Doc!
Nicolas
21-Dec-2012
[4759]
awesome.
Jerry
22-Dec-2012
[4760]
In C, struct astruct { char c; int values[3]; }; How do I write this 
in Red/System? Thanks. It's the integer array which confuses me.
Endo
22-Dec-2012
[4761]
Thanks Jerry, shame on me :)
PeterWood
22-Dec-2012
[4762x3]
There are no arrays in this bootstrap version of Red/System. So you 
have to manage this with a pointer:

Red/System []

s: declare struct! [
  c  [byte!]
  i1 [integer!]
  i2 [integer!]
  i3 [integer!]
]

; intialise the structure
s/c: #"a"
s/i1: 1
s/i2: 2
s/i3: 3

;create a pointer and point it at the first integer
i: as integer! s

i: i + 4                          ;; set i to address of first integer
ip: as pointer! [integer!] i

;; add the integers
sum: 0
count: 3
until [
  sum: sum + ip/value
  ip: ip + 1
  count: count - 1
  count = 0
]

print ["The sum of the integers is " sum lf]
There is a probable issue with value alignment as explained in http://static.red-lang.org/red-system-specs.html#section-4.7

I'm not sure how to handle "packed" stucts in Red/System.
Also, the docs state that the value alignment may vary by processor 
so the line
	i:  i  + 4


may not be valid on all platforms (though I think it is safe with 
IA-32 and ARM).
DocKimbel
22-Dec-2012
[4765]
That's cool, it's something like rebcode.

It has a broader range of usage and it's faster than rebcode.


How about something like having a block of Red/System code inside 
Red code?

System [...some R/S code...]


This is already implemented since a while in form of a Red compiler 
directive: #system [...]
Jerry
22-Dec-2012
[4766]
Thanks. Peter. It's very helpful.
Kaj
22-Dec-2012
[4767x2]
To have a tightly packed semi-array of dynamcic length, use allocate 
and free
Doc, thanks very much for routine!. It's brilliant and exactly what 
I need to make the bindings available from Red. However, #define 
not being context aware is still a blocker
DocKimbel
22-Dec-2012
[4769]
There's not much that can be done currently for #define, you can 
use #enum instead to workaround it in such cases. 


Anyway, we will need to rethink the preprocessor in Red/System v2, 
and if possible, remove it. It has been added at the beginning of 
Red/System because Red was not yet there to provide a higher-level 
macro system. Now that Red is alive, we could start thinking of a 
better alternative to the current Red/System preprocessor.
Kaj
22-Dec-2012
[4770x2]
That would be good; I still have mixed feelings about it
It would have to be something that can be processed by Red but still 
able to generate stand-alone Red/System
DocKimbel
22-Dec-2012
[4772x2]
I would like to get rid of it, for many reasons. Firstly because 
it was not meant to be part of Red/System, but added later for practical 
needs, secondly because Red/System code could be built/composed more 
efficiently from Red.
In other words, we need some kind of Red wrapper (or maybe dialect) 
on top of Red/System to make it easier to construct programmatically.
Kaj
22-Dec-2012
[4774]
Sounds right
DocKimbel
22-Dec-2012
[4775x3]
I haven't had time yet to think about such replacement solution, 
so ideas are welcome.
Routine preliminary support pushed. See commit log for examples. 
The routine! type still needs to be added.
Routine! datatype added, reflection is supported.
NickA
22-Dec-2012
[4778]
Doc, I'm very glad to see you're still working hard on Red!  I was 
concerned that the psychological effect of everyone paying attention 
to R3 source, plus your own curiousity about it, might derail you 
for a while.  I'm still eagerly awaiting every advance you make!
DocKimbel
23-Dec-2012
[4779]
Actually, I still haven't found time to read the whole R3 sources 
base. I was too busy this week designing some new parts of Red.
Pekr
23-Dec-2012
[4780x2]
doc, congrats you were accepted to the dev program of Leap motion 
:-)
... and yes, please do continue with Red development, I believe that 
in the end it is good to have rebol like alternative :-)
DocKimbel
23-Dec-2012
[4782]
Pekr: thanks, I hope to get the device + SDK in January. My application 
to their developer program was based on an innovative IDE for Red 
powered by Leap Motion device. ;-)
Pekr
23-Dec-2012
[4783]
sounds cool :-) Lately I was wondering about the possible benefits 
of Red and R3. Difficult to judge, but could there be any overlapping 
ground, where twose two projects could cooperate? e.g your IDE for 
R3 to, simply a language would be a plugin, or - both projects want 
to address Android - could one bridge to JNI be used for both?
DocKimbel
23-Dec-2012
[4784]
Pekr: I don't know. We'll see how things evolve in the next month. 
In order to cooperate with R3, it would be first necessary to determine 
who's really in charge of R3 or what R3 fork should Red cooperate 
with... :-)
Pekr
23-Dec-2012
[4785]
I thought it is already coordinated here on Altme :-) Well, my typical 
what's next for Red? Objects? IO? dyn lib emmiter? Android? :-)
DocKimbel
23-Dec-2012
[4786x2]
I'm working on objects and ports currently.
I might release some other features for end of year though.
Janko
23-Dec-2012
[4788]
Yes, go Doc! I wish I was better at low-level programming so I could 
help a little. If there would be any examples of simple bindings 
or base of TCP that we could extend to different protocols I would 
try to participate a little.
DocKimbel
23-Dec-2012
[4789]
Kaj, just a remark about Red and bindings: there is still  an additional 
feature to come that will allow to import both Red/System and external 
libs functions directly in Red in a declarative way. It will use 
almost the same syntax as #import in Red/System but will convert 
the datatypes automatically (in the same way R2 does with routines 
and structs). A struct! datatype would then be added also to Red. 
The routine! datatype primary purpose is provide a way to write ultra-fast 
code and enable system programming, even if it can be also used to 
wrap Red/System bindings. I'm still unsure which approach would work 
the best for building bindings to C libraries. I guess you'll be 
the first to find out. ;-)
Jerry
23-Dec-2012
[4790]
Red/System Question: In a function, a local variable v1 is declared 
as struct! [ v2 [integer!]  ]. Once the function is called, v1 is 
in stack, v2 is in heap. When the function call is over, v1 is gone, 
but v2 is still in heap, right?
Kaj
23-Dec-2012
[4791x4]
Yes, structs are currently always on the heap
Well, static memory, actually, if you didn't create it with allocate
Doc, thanks for the heads-up. I'm also unsure which route to go, 
but I want to have the option to write Red/System only programs, 
so I guess I will usually build a Red binding on a Red/System binding
On the other hand, if a binding is mostly the #import section, the 
future format sounds more concise than writing all wrappers as routines
Jerry
23-Dec-2012
[4795x3]
Thanks. Kaj
### Red/System Question ###
To get cpu count, I can do this in C:
   
   sysctlbyname("hw.ncpu", &cpuCount, &len, ((void *)0), 0);


But Red/System doesn't support &. How can I do this in Red/System? 
Thanks.
&cpuCount, &len are the two parameters that I don't know how to express 
in R/S.
Kaj
23-Dec-2012
[4798x3]
It does support &, but it's written : like in REBOL :-)
Use :cpu-count :len
What does len specify?
Jerry
23-Dec-2012
[4801]
len ... the length of buffer in the next parameter??? not sure.
DocKimbel
24-Dec-2012
[4802]
From Announce: Great Kaj! The optimizer should be able to do a pretty 
good job on simple cases like Fibonacci function. Still, you'll have 
the Red stack management overhead, which is currently unavoidable, 
but in the future, we might found ways to optimize it too. Actually, 
I have two options to reduce stack overhead: stack multiple openings/closing 
compression and inlining stack calls, but we'll see that in Red v2.
Jerry
25-Dec-2012
[4803]
Red/System Questions:


I am translating a piece of code from C to Red/System, I don't know 
how to translate the following C code:


success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,  (void 
* volatile *)&_zone_); // C

my problems are: 

1. In Red/Sys, _sqliteZone_ is a struct! (which is a pointer in Red/Sys), 
so I cannot use :_zone_ to get it's address.
2. "(void * volatile *)" in C => "as byte-ptr!" in Red/Sys ??

Thanks!
DocKimbel
25-Dec-2012
[4804]
1) Is _zone_ a struct or a struct pointer? If it is a struct, then 
in Red/System, you just pass the struct! variable.

2) Correct, use "as byte-ptr!" in such case.