Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-06 Thread Alexander Burger
Hi Thorsten,

>  (de evalInR ("Cmd")
>   (native `*RinC "evalInR" 'P "Cmd"))
> 
> ##  SEXP evalInR(char * cmd);
> 
> rinc: (evalInR "print(6*4)")
> [1] 24
> -> 65814160

OK, so now we have a pointer to a structure filled by evalInR().


> rinc: (evalInR "6*4")
> -> 65814160
> 
> how can I extract the result from that Pointer in PicoLisp (lack of C
> skills ... ;-)?

You can use the 'struct' function to access it.

I could not find the exact definition of SEXP. but as it is a pointer to a
structure, you can do

   (struct (evalInR "6*4") ...)

The arguments depend on the exact layout of the data in the C struct. I think
this helps: https://software-lab.de/doc/native.html

☺/ A!ex


-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-06 Thread Thorsten Jolitz
Hi Cesar,
it works both ways, and there is an old R C API  and more modern C++
implementations (with C headers too) for the use case here:
- call R from C(++).
or better:
- call R from PicoLisp via (native) C (calls)
See my last answer to Alex to see that the call to R actually works.
Cheers
Thorsten.


Am Di., 6. Apr. 2021 um 22:20 Uhr schrieb Cesar Rabak :

> Hi Thorsten,
>
> The quote you copied here (coming from the ref. of yours R's C Interface),
> describes IIUC a structure, more accurately the model of, for calling
> "foreing" C functions in R and not the converse.
>
> The way to call R functions in picolisp would be to call the functions
> made available through the API, in case of R a process similar to FFI using
> as reference the include files, wouldn't?
>
> HTH
> --
> Cesar Rabak
>
>
> On Mon, Apr 5, 2021 at 4:30 PM Thorsten Jolitz  wrote:
>
>> Hello List,
>> I wonder how to deal with the R SEXP Data structure in native calls.
>>
>> *"Technically, [a SEXP] is a pointer to a structure with typedef SEXPREC

Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-06 Thread Thorsten Jolitz
Hi Alex,
thanks for the hints, I tried both, T and 'P as result values , the first
gives a segment fault, but the second actually works:

 (de evalInR ("Cmd")
  (native `*RinC "evalInR" 'P "Cmd"))


##  SEXP evalInR(char * cmd);

rinc: (evalInR "print(6*4)")
[1] 24
-> 65814160

If I use "print" in the R command, I actually see the R output.
And with the 'P, I actually get a return value.
But what can I do in PicoLisp with that pointer? If I skip the "print" and
just call R for the return value:

rinc: (evalInR "6*4")
-> 65814160

how can I extract the result from that Pointer in PicoLisp (lack of C
skills ... ;-)?
Cheers
Thorsten

Am Di., 6. Apr. 2021 um 10:23 Uhr schrieb Alexander Burger <
a...@software-lab.de>:

> Hi Thorsten,
>
> > I wonder how to deal with the R SEXP Data structure in native calls.
> >
> > *"Technically, [a SEXP] is a pointer to a structure with typedef SEXPREC.
> > A SEXP is a variant type, with subtypes for all R’s data structures"*
> >
> > E.g.
> >
> >- INTSXP: integer vector
> >- LGLSXP: logical vector
> >- STRSXP: character vector
> >- ... (and a dozen more)
>
> I do not really understand the implications in the context of R, but Pil21
> has a
> new 'T' result specification for raw Lisp data.
>
> It allows to pass a pointer to any Lisp data item to a native function,
> and/or
> to return such data.
>
>
> > When I have an imaginary generic C function like this:
> >
> > SEXP fun(SEXP x, char * cmd)
>
> As an example, we might call the standard 'prog1' function in the Pil21
> executable (which has the internal label "_prog1", try (vi 'prog1)). It
> takes a
> list for the body, and returns the resulting value:
>
>: (%@ "_prog1" T '(T prog1 7 (println 1 2 3)))
>1 2 3
>-> 7
>
> This is equivalent to
>
>: (prog1 7 (println 1 2 3))
>1 2 3
>-> 7
>
>
> > how can I specify Return Value/Arguments in a 'native' call, if I cannot
> > know in advance to which R subtype the  SEXP Points?
>
> If the above is not what you intended, the 'P' result specification can be
> used
> as an unspecific pointer (void*, an unsigned 64 bit number).
>
> ☺/ A!ex
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-06 Thread Cesar Rabak
Hi Thorsten,

The quote you copied here (coming from the ref. of yours R's C Interface),
describes IIUC a structure, more accurately the model of, for calling
"foreing" C functions in R and not the converse.

The way to call R functions in picolisp would be to call the functions made
available through the API, in case of R a process similar to FFI using as
reference the include files, wouldn't?

HTH
--
Cesar Rabak


On Mon, Apr 5, 2021 at 4:30 PM Thorsten Jolitz  wrote:

> Hello List,
> I wonder how to deal with the R SEXP Data structure in native calls.
>
> *"Technically, [a SEXP] is a pointer to a structure with typedef SEXPREC.
> A SEXP is a variant type, with subtypes for all R’s data structures."*
>
> E.g.
>
>- INTSXP: integer vector
>- LGLSXP: logical vector
>- STRSXP: character vector
>- ... (and a dozen more)
>
> When I have an imaginary generic C function like this:
>
> SEXP fun(SEXP x, char * cmd)
>
> how can I specify Return Value/Arguments in a 'native' call, if I cannot
> know in advance to which R subtype the  SEXP Points?
>
> On the one hand, a SEXP is like a C Datatype for a Lisp list, so one could
> think that's ideal for calling C from Pil with list args and list return
> values. On the other hand, how can one give the primitive specifications in
> the specification lists for 'native' args/return vals, without knowing the
> concrete subtype (R data structure), i.e. when CMD can be any R command
> that may take/return any of the subtypes (char, logical, int, vector, list
> .)?
>
> Is there maybe a generic solution on the 'native' side too, without the
> primitive specifications?
> Thanks in advance and Cheers
> Thorsten
>
> PS
> For those interested here two links:
> Rinternals.h source code:
> r-source/Rinternals.h at a1425adea54bcc98eef86081522b5dbb3e149cdc ·
> wch/r-source (github.com)
> 
>
> R's C Interface (blog post explaining the code/data structures)
> R's C interface · Advanced R. (had.co.nz)
> .
>


Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-06 Thread Alexander Burger
Hi Thorsten,

> I wonder how to deal with the R SEXP Data structure in native calls.
> 
> *"Technically, [a SEXP] is a pointer to a structure with typedef SEXPREC.
> A SEXP is a variant type, with subtypes for all R’s data structures"*
> 
> E.g.
> 
>- INTSXP: integer vector
>- LGLSXP: logical vector
>- STRSXP: character vector
>- ... (and a dozen more)

I do not really understand the implications in the context of R, but Pil21 has a
new 'T' result specification for raw Lisp data.

It allows to pass a pointer to any Lisp data item to a native function, and/or
to return such data.


> When I have an imaginary generic C function like this:
> 
> SEXP fun(SEXP x, char * cmd)

As an example, we might call the standard 'prog1' function in the Pil21
executable (which has the internal label "_prog1", try (vi 'prog1)). It takes a
list for the body, and returns the resulting value:

   : (%@ "_prog1" T '(T prog1 7 (println 1 2 3)))
   1 2 3
   -> 7

This is equivalent to

   : (prog1 7 (println 1 2 3))
   1 2 3
   -> 7


> how can I specify Return Value/Arguments in a 'native' call, if I cannot
> know in advance to which R subtype the  SEXP Points?

If the above is not what you intended, the 'P' result specification can be used
as an unspecific pointer (void*, an unsigned 64 bit number).

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe