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-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
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: John Cowan @ 2020-05-29  3:57 UTC (permalink / raw)
  To: Keith Wright; +Cc: guile-user

Check out the Chibi library (chibi show c). in the Chibi repo at
lib/chibi/show/c.scm and .sld.  It provides combinators that create a C
equivalent of the sexp; there is both a macro-based compiler and an
interpreter, IIRC.  Unfortunately there is no real documentation.  There's
some cleverness in it: c-if in statement context expands to an
if-statement, but in an expression context to a ?: operator.  If you import
(chibi show) and (chibi show c) then (show #t (c-if 'foo 'bar 'baz)) will
generate a statement, but (show #t (c+ 2 (c-if 'foo 'bar 'baz))) will
generate an expression.


On Thu, May 28, 2020 at 11:38 PM Keith Wright <kwright@keithdiane.us> wrote:

> 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-29  3:57 ` John Cowan
@ 2020-05-29  7:29   ` Arne Babenhauserheide
  0 siblings, 0 replies; 9+ messages in thread
From: Arne Babenhauserheide @ 2020-05-29  7:29 UTC (permalink / raw)
  To: John Cowan; +Cc: guile-user

Also have a look at https://github.com/sph-mn/sph-sc

Examples:

;; declaration
(declare
  a uint32_t
  b (array uint8_t 3)
  c (struct (id int) (name char*))
  d (enum (x y z))
  e (type uint16_t)
  f (type (struct (id int) (name char*))))

;; define with value
(define a uint32_t 1)

;; macros
(pre-define
  is-included #t
  id-size 4
  (mymacro a b) (set a 1 b 2))

;; functions
(define (myfunction a b) (int char void)
  "a description of this function"
  (return 1))


I found it when the author did the full round-trip over wisp via
c-indent: http://sph.mn/computer/guides/c/c-indent.html
This then looks much more similar to C; but fully regular:


pre-include "stdio.h"

define (main argc argv) : int int char**
  declare i int
  printf "the number of program arguments passed is %d\n" argc
  for : (set i 0) (< i argc) (set+ i 1)
    printf "argument %d is %s\n" (+ i 1) (array-get argv i)
  return 0


Best wishes,
Arne

John Cowan <cowan@ccil.org> writes:

> Check out the Chibi library (chibi show c). in the Chibi repo at
> lib/chibi/show/c.scm and .sld.  It provides combinators that create a C
> equivalent of the sexp; there is both a macro-based compiler and an
> interpreter, IIRC.  Unfortunately there is no real documentation.  There's
> some cleverness in it: c-if in statement context expands to an
> if-statement, but in an expression context to a ?: operator.  If you import
> (chibi show) and (chibi show c) then (show #t (c-if 'foo 'bar 'baz)) will
> generate a statement, but (show #t (c+ 2 (c-if 'foo 'bar 'baz))) will
> generate an expression.
>
>
> On Thu, May 28, 2020 at 11:38 PM Keith Wright <kwright@keithdiane.us> wrote:
>
>> 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
>>
>>
>>


-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken



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

* Re: C programs in Scheme syntax
  2020-05-29  3:23 C programs in Scheme syntax Keith Wright
  2020-05-29  3:57 ` John Cowan
@ 2020-05-29 12:34 ` Matt Wette
  2020-05-29 15:30 ` Zelphir Kaltstahl
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Matt Wette @ 2020-05-29 12:34 UTC (permalink / raw)
  To: guile-user

The other solutions look closer to what you want but

     https://savannah.nongnu.org/projects/nyacc

has a C parser written in Guile which outputs SXML
and a pretty-printer which converts the SXML to C code.

On 5/28/20 8:23 PM, Keith Wright wrote:
> 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.




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

* Re: C programs in Scheme syntax
  2020-05-29  3:23 C programs in Scheme syntax Keith Wright
  2020-05-29  3:57 ` John Cowan
  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
  4 siblings, 0 replies; 9+ messages in thread
From: Zelphir Kaltstahl @ 2020-05-29 15:30 UTC (permalink / raw)
  To: guile-user

Hi,

There is also Schemetran: https://gitlab.com/codetk/schemetran Perhaps
that was it?

Regards,

Zelphir

On 29.05.20 05:23, Keith Wright wrote:
> 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-29  3:23 C programs in Scheme syntax Keith Wright
                   ` (2 preceding siblings ...)
  2020-05-29 15:30 ` Zelphir Kaltstahl
@ 2020-05-29 16:27 ` Andrew Gwozdziewycz
  2020-05-29 20:01 ` Jan Wedekind
  4 siblings, 0 replies; 9+ messages in thread
From: Andrew Gwozdziewycz @ 2020-05-29 16:27 UTC (permalink / raw)
  To: Keith Wright; +Cc: guile-user

One might also take a look at PreScheme: https://en.wikipedia.org/wiki/Scheme_48 which is a lowlevel Sexp based system that can generate C or Bytecode. 

There’s also BitC, which was/is a sexp based lowlevel language. I cannot recall if it compiled directly to C, or was itself a compiler to machine code. 

These, both, are not direct answers to your question (which I think has a fairly straightforward answer), but might give you some alternative ideas to consider.

> On May 28, 2020, at 20:38, Keith Wright <kwright@keithdiane.us> wrote:
> 
> 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-29  3:23 C programs in Scheme syntax Keith Wright
                   ` (3 preceding siblings ...)
  2020-05-29 16:27 ` Andrew Gwozdziewycz
@ 2020-05-29 20:01 ` Jan Wedekind
  2020-05-30 10:13   ` Todor Kondić
  4 siblings, 1 reply; 9+ messages in thread
From: Jan Wedekind @ 2020-05-29 20:01 UTC (permalink / raw)
  To: guile-user

I have implemented a GNU Guile extension which compiles array operations to machine code using LLVM: http://wedesoft.github.io/aiscm/
I thought you might be interested since your example is an array operation.

Regards
Jan

Am 29. Mai 2020 04:23:46 GMT+01:00 schrieb Keith Wright <kwright@keithdiane.us>:
>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

-- 
Diese Nachricht wurde von meinem Android-Gerät mit K-9 Mail gesendet.


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

* Re: C programs in Scheme syntax
  2020-05-29 20:01 ` Jan Wedekind
@ 2020-05-30 10:13   ` Todor Kondić
  0 siblings, 0 replies; 9+ messages in thread
From: Todor Kondić @ 2020-05-30 10:13 UTC (permalink / raw)
  Cc: guile-user@gnu.org



> I have implemented a GNU Guile extension which compiles array operations to machine code using LLVM: http://wedesoft.github.io/aiscm/
> I thought you might be interested since your example is an array operation.
>
> Regards
> Jan
>
> Am 29. Mai 2020 04:23:46 GMT+01:00 schrieb Keith Wright kwright@keithdiane.us:
>
> > 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
>
> --
>
> Diese Nachricht wurde von meinem Android-Gerät mit K-9 Mail gesendet.

Hah, probably everyone gets to this point after using Scheme for a while.

This is my fortran code generator.
https://gitlab.com/codetk/schemetran



^ 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).