From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Olivier Dion via General Guile related discussions Newsgroups: gmane.lisp.guile.user Subject: Re: C extensions Date: Sun, 21 Feb 2021 11:00:06 -0500 Message-ID: <87zgzx5s3d.fsf@clara> References: Reply-To: Olivier Dion Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17831"; mail-complaints-to="usenet@ciao.gmane.io" To: Tim Meehan , guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Feb 21 16:59:50 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lDr9A-0004Vb-9O for guile-user@m.gmane-mx.org; Sun, 21 Feb 2021 16:59:48 +0100 Original-Received: from localhost ([::1]:41688 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lDr99-0001sk-9E for guile-user@m.gmane-mx.org; Sun, 21 Feb 2021 10:59:47 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47694) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lDr8h-0001rg-Qe for guile-user@gnu.org; Sun, 21 Feb 2021 10:59:19 -0500 Original-Received: from smtp.polymtl.ca ([132.207.4.11]:59931) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lDr8f-0005Lf-RO for guile-user@gnu.org; Sun, 21 Feb 2021 10:59:19 -0500 Original-Received: from localhost (static-198-54-132-116.cust.tzulo.com [198.54.132.116]) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 11LFx55W030324 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 21 Feb 2021 10:59:10 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 11LFx55W030324 In-Reply-To: X-Poly-FromMTA: (static-198-54-132-116.cust.tzulo.com [198.54.132.116]) at Sun, 21 Feb 2021 15:59:05 +0000 Received-SPF: pass client-ip=132.207.4.11; envelope-from=olivier.dion@polymtl.ca; helo=smtp.polymtl.ca X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17294 Archived-At: On Sat, 20 Feb 2021, Tim Meehan 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