Re: How to parse a C string like "(+ 2 3)" as a GUILE s-expr

<[email protected]> Sun, 4 May 2025 14:20:59 +0200
Newsgroups gmane.lisp.guile.user
Message-ID <aBdbq/[email protected]>
On Sun, May 04, 2025 at 01:50:00PM +0200, Basile Starynkevitch wrote:
> Hello all,
> 
> I'm using GNU guile 3.0 packaged in Debian in some C++ (GPL licensed) utility
> https://github.com/RefPerSys/RefPerSys/blob/master/do-build-refpersys-plugin.cc
> 
> (in commit e2351bafcef, near line 354). Encoding is of course UTF-8.
> 
> I want to find the GNU Guile C functions to transform a string into a GUILE s-
> expression (so SCM type in C) but that string has to be parsed.
> 
> For example the C string "(+ 2 3)" should be parsed as a GUILE list which I
> would later evaluate to 5.

Hi, Basile

If I'm understanding you correctly, this would be the Scheme function
`read' (and its C counterpart scm_read) which take a port and return
an S-expression.

Now you have to make a port from your string, e.g. with `call-with-input-string'.

In Scheme:

  (let ((str "(+ 4 5)"))
    (call-with-input-string str
       (lambda (p) (read p))))

=>

  (+ 4 5)

I know, the lambda around read is somewhat redundant, but it seemed
clearer like that.

Cheers
-- 
t
signature.asc (application/pgp-signature, 195 B)
-----BEGIN PGP SIGNATURE-----

iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCaBdbpAAKCRAFyCz1etHa
RvUmAJ9lo+qa4em/FJnHL1SCv+LJcdjm6wCfQwyXXO/DTCkgcqXCpvFFYSPZZzM=
=3NYW
-----END PGP SIGNATURE-----