guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C programs in Scheme syntax


From: Arne Babenhauserheide
Subject: Re: C programs in Scheme syntax
Date: Fri, 29 May 2020 09:29:36 +0200
User-agent: mu4e 1.4.4; emacs 26.3

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]