guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C extensions


From: Olivier Dion
Subject: Re: C extensions
Date: Sun, 21 Feb 2021 11:00:06 -0500

On Sat, 20 Feb 2021, Tim Meehan <btmeehan@gmail.com> wrote:
> I'm trying my hand a writing C extensions.
> I've done this for stuff like Matlab before, and was wondering how you do
> the usual checking of the arguments that are passed in from Guile.
>
> In the manual, 6.13.13.1 "C Support" has a few functions.
> libguile/numbers.h has a bunch more ...
>
> What I have is an extension function, sort of like the bessel function in
> the tutorial:
> https://www.gnu.org/software/guile/manual/html_node/A-Sample-Guile-Extension.html

Usually I do this in 2 or 3 steps.

  1. Define the primitive in C with raw arguments.
  
  2. Make a wrapper of this primitive for Guile to use. Optionnaly do
  the arguments checking here or go to step 3.

  3. Make a second wrapper in Scheme and check the arguments there.
  This wrapper will call wrapper in made in 2.

Here's a full example:
----------------------------------------------------------------------
float c_sin(double x)
{
   return sin(x);
}

SCM_DEFINE(c_sin_wrapper, "sin", 1, 0, 0,
           (SCM x),
           "My sine")
{
        if (scm_is_number(x)) {
                return c_sin(scm_to_double(x));
        }

        return SCM_BOOL_F;
}
----------------------------------------------------------------------

> What I would like to do is verify that the first argument is an inexact
> number, larger than 0. How would I go about that? Perhaps some of it
> could

For your example, use the following predicates:
----------------------------------------------------------------------
scm_is_inexact(x) && scm_is_true(scm_positive_p(x))
----------------------------------------------------------------------

> be:
> SCM_REALP
> scm_misc_error
>
> Any tips?
-- 
Olivier Dion
PolyMtl



reply via email to

[Prev in Thread] Current Thread [Next in Thread]