unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* C programs in Scheme syntax
@ 2020-05-29  3:23 Keith Wright
  2020-05-29  3:57 ` John Cowan
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Keith Wright @ 2020-05-29  3:23 UTC (permalink / raw)
  To: guile-user

I am thinkging about a project that uses Scheme macros
to generate C code.  To this end I want to encode C 
programs as S-expressions.  For example, the C program
that is encoded in Ascii as

for (j=0;j<12;++j) a[j] = j*pi/6;

might be encoded as an S-expression as

(for ((= j 0)(< j 12) (++ j)) (= (sub a j)(/ (* j pi) 6)))

Note that this is not a valid Scheme program, even with
non-standard functions defined.  It is a re-encoding
of the Ascii C syntax as an S-expression.

I think I have read about something like this, perhaps
on this list, I am not sure.  (Note to future language
inventors: a single letter name makes a horrible Google
search query.  Name things with made up but pronouncable
words---perl, fortran...)

I most need to convert S-expr encoded C, to Ascii encoded C,
but I am interested in
(a) programs to convert S-expresions to C
(b) specifications for the form of the S-expr encoding
(c) better plans; advice from those who have tried and failed.

Any pointers?

   -- Keith




^ permalink raw reply	[flat|nested] 9+ messages in thread
* Re: C programs in Scheme syntax
@ 2020-05-31 15:40 tantalum
  0 siblings, 0 replies; 9+ messages in thread
From: tantalum @ 2020-05-31 15:40 UTC (permalink / raw)
  To: Guile user

ive started https://github.com/sph-mn/sph-sc and have been using it a 
lot over the years and worked through several c edge cases. i enjoy 
using it, more and more even. the git repository page contains several 
hints and ideas on the general topic. i you have any questions, i'd be 
happy to answer them.

a few key points about sc:
* it uses scheme read to parse code but that discards scheme comments 
and other ways to insert c comments are needed. using guile-reader 
instead works, but it is a c library to be compiled by the user and i 
often faced autotools issues with it
* i guess it would be possible to do the whole generation process via 
scheme macros. it might be better than the approach currently 
implemented in sc, as the scheme macro system and scheme comments are 
immediately available. syntax checks with hints on error would be nice. 
the translation process of sc calls procedures for all elements of the 
nested list of expressions returned by scheme read. first going through 
the tree top to bottom - possibly translating whole subtrees to c 
strings or returning sc to be parsed again, then bottom to top to handle 
remaining elements - elements which might be lists with arguments that 
have been previously translated. sescript, the javascript variant of sc, 
uses hashtables that map s-expression prefixes to replacer functions, 
which is easier to extend than the case statements used by sc
* the reason why i havent implemented a scheme style macro system for sc 
yet is that the task of implementing something like syntax-rules or 
syntax-case seems quite daunting. a good pattern matcher and preserving 
hygiene seem to be the top issues. simpler macro systems are possible, 
quickly implemented even, but all in all i havent hit a clear need for 
it yet (still thinking what i would do with it) and fear that it might 
create hard to read c code. then again, it might simplify c code because 
of a reduced dependence on the preprocessor
* sc has automated tests with input/output listed, this could be helpful 
for finding edge cases even in other projects
* with s-expressions, automated code documentation for c becomes easier 
than ever before



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-05-31 15:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-29  3:23 C programs in Scheme syntax Keith Wright
2020-05-29  3:57 ` John Cowan
2020-05-29  7:29   ` Arne Babenhauserheide
2020-05-29 12:34 ` Matt Wette
2020-05-29 15:30 ` Zelphir Kaltstahl
2020-05-29 16:27 ` Andrew Gwozdziewycz
2020-05-29 20:01 ` Jan Wedekind
2020-05-30 10:13   ` Todor Kondić
  -- strict thread matches above, loose matches on Subject: below --
2020-05-31 15:40 tantalum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).