From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: taylanbayirli@gmail.com (Taylan Ulrich =?utf-8?Q?Bay=C4=B1rl=C4=B1?= =?utf-8?Q?=2FKammer?=) Newsgroups: gmane.lisp.guile.user Subject: Re: ffi helper Date: Mon, 20 Mar 2017 15:49:08 +0100 Message-ID: <878to0dmvv.fsf@gmail.com> References: <96434CF4-E3F9-4410-82B6-9EB8DEE130B9@gmail.com> <95B6EEEF-5278-44C8-B640-647A16AEFD1F@gmail.com> <201FF019-FA10-4B35-A96D-BC0AD078F5DB@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1490021021 21161 195.159.176.226 (20 Mar 2017 14:43:41 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 20 Mar 2017 14:43:41 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) Cc: guile-user@gnu.org To: Matt Wette Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Mar 20 15:43:37 2017 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cpyX1-0004yw-1G for guile-user@m.gmane.org; Mon, 20 Mar 2017 15:43:35 +0100 Original-Received: from localhost ([::1]:33388 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cpyX7-0002ws-14 for guile-user@m.gmane.org; Mon, 20 Mar 2017 10:43:41 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49488) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cpyWk-0002wk-ML for guile-user@gnu.org; Mon, 20 Mar 2017 10:43:19 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cpyWj-0005xO-Pl for guile-user@gnu.org; Mon, 20 Mar 2017 10:43:18 -0400 Original-Received: from mail-wr0-x234.google.com ([2a00:1450:400c:c0c::234]:36386) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cpyWj-0005xC-JA for guile-user@gnu.org; Mon, 20 Mar 2017 10:43:17 -0400 Original-Received: by mail-wr0-x234.google.com with SMTP id u108so93903586wrb.3 for ; Mon, 20 Mar 2017 07:43:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-transfer-encoding; bh=VcI+LIUDEAWCoeIm+OQrij+dkyvLH6q6H+9I3c9i2Mw=; b=IEmNMiDgaO3HJFKcYXtbmEAvM2IYRmack2rA+qyyTzNYtSumMy9iL+wBvd3CVgbXjM 3jWYmkv9LQrYbJWRKlkFQeE1ts5AVzq/53+q+ps6/w3ydB7xR/FZ+aruVdptVl6EeX/1 iDMB7aAYLdAR2HDBbszK9EPzvLVNhNUFuWNyaWnJYqQcu2rqyPtSigui/tInSI+24ESH ICkAW3WlZ2sfS8f6sKAV1YtDi8JYJphHQGHI98CHhCh4qzE0PHug+MsyEO4lmFhxH9CA ofFF/wLk81zrIp+dkE2LYAIrEH0ncdSIbKHjdFXsI5qGE+5l0ttLKPVkyUq0mSNhl+DO PzNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-transfer-encoding; bh=VcI+LIUDEAWCoeIm+OQrij+dkyvLH6q6H+9I3c9i2Mw=; b=oNb4aXLtHZyxgqeKk3lhfPxeflrmD+2LFfHmkQ+NPIE05DXLmXMgvZhL6jr9E4ZrVt Rl9/qvOdxSJAOGvZTOirvWh2/grptL254clMCO3+Z7fep3lmrSUbP7p3gNWTh7K2rzAk fdZruM8cefWOtGZJFtkMRDTUDXm8reE0hI0F9N/pSUHEtgc0LqrpeIzG1JphkQQqD5fJ K2Id83Yv3s9xCe1aE4apss/wV9JRtO2aU2nsHfNQecyUDtwTYBq7tsKTkIz9//bMn7i5 Cvi+vse/qe+Dy4XMuhIoVc0XJK1dafZl6gFIToPs5mtFY24Hn3SXnDi4tkVis89mUTql cYNQ== X-Gm-Message-State: AFeK/H3SjE86LR0UCXYGOPluc7P20pL6qlqdV65CArLnWBsWxbKGad6sif53/Tzi7Gjedg== X-Received: by 10.223.164.83 with SMTP id e19mr25432116wra.201.1490020996174; Mon, 20 Mar 2017 07:43:16 -0700 (PDT) Original-Received: from T420 ([2a02:908:c30:3540:221:ccff:fe66:68f0]) by smtp.gmail.com with ESMTPSA id o15sm20974915wra.61.2017.03.20.07.43.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 20 Mar 2017 07:43:15 -0700 (PDT) In-Reply-To: <201FF019-FA10-4B35-A96D-BC0AD078F5DB@gmail.com> (Matt Wette's message of "Sun, 19 Mar 2017 10:23:14 -0700") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::234 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 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.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:13566 Archived-At: Matt Wette writes: >> On Mar 13, 2017, at 5:53 PM, Matt Wette wrote: >>=20 >>=20 >>> On Mar 8, 2017, at 6:06 PM, Matt Wette wrote: >>>=20 >>> I=E2=80=99m now working on a FFI helper based on the nyacc C99 parser.= =20=20=20 >>=20 > > I am making some progress with some ragged prototype code. > > Here is a top-level program, just pulling out three declarations to start: > > (let* ((file "/opt/local/include/cairo/cairo-svg.h") > (defs '()) > (incs '("/opt/local/include" "/opt/local/include/cairo" "/usr/incl= ude")) > (tree (my-parse defs incs file)) > (file-decls (reverse (c99-trans-unit->udict tree #:filter cairo-fi= lter))) > (udecl-dict (c99-trans-unit->udict/deep tree))) > > (fold > (lambda (pair type-list) > (udecl->ffi-decl (cdr pair) type-list)) > fixed-width-int-names > (filter > (lambda (e) > (member (car e) > '("cairo_matrix_t" "cairo_surface_t" "cairo_svg_surface_create"))) > file-decls))) > > And here is the autogenerated output for the declarations: > > (define-std-pointer-wrapper cairo_surface_t*) > > (define cairo_matrix_t > (bs:struct > `(,(string->symbol "xx") ,double) > `(,(string->symbol "yx") ,double) > `(,(string->symbol "xy") ,double) > `(,(string->symbol "yy") ,double) > `(,(string->symbol "x0") ,double) > `(,(string->symbol "y0") ,double))) > > (define cairo_svg_surface_create > (let ((f (pointer->procedure > '* > (libcairo-func "cairo_svg_surface_create") > (list '* double double)))) > (lambda (filename width_in_points height_in_points) > (let ((~filename (string->pointer filename)) > (~width_in_points width_in_points) > (~height_in_points height_in_points) > (~result > (f ~filename ~width_in_points ~height_in_points))) > (wrap-cairo_surface_t* ~result))))) Neat! > I was not able to come up with a procedure that would take arguments > =E2=80=9Cxx=E2=80=9D and =E2=80=9Cdouble=E2=80=9D and generate the bs:str= uct pattern `(xx ,double). > Help is welcome. You mean you get "xx" and "double" as strings? For xx, string->symbol as you already do is OK. As for double, that's a bit tricky. I see a few possibilities: - Turn the string into a symbol and eval it: (eval (string->symbol str) (resolve-module '(bytestructures guile numeric))) - Use the reflective module API: (module-ref (resolve-module '(bytestructures guile numeric)) (string->symbol str)) - Create an explicit mapping from name (string or symbol) to object: (import (bytestructures guile numeric)) (define descriptor-mapping `(("double" . ,double) ("float" . ,float) ...)) (The so-far undocumented module (bytestructures guile numeric-metadata) contains a mapping similar to this, but it only contains name with explicit bit width so e.g. 'double' isn't in it, so I guess it's not the right thing here.) While 'eval' is normally discouraged, I think it's fine in this case, since we use string->symbol. I don't see anything bad resulting from evaluating an arbitrary symbol. However, the second option could be said to be a tad bit more "correct" than using eval, so I'd go with that and only use the eval method if I had to write standards-compliant code where eval is available but not something like module-ref. The third option gives you full control but it shouldn't be necessary. Hope that helps, Taylan