8
votes

I want to write C in s-expressions and use compile-time macros. Does anybody know of anything that does this? It should translate the s-expressions into standard C.

5
Could you give an example of what s-expressions you want the code to be in? vatine gave an example, but you said that it wasn't quite what you wanted. If the format doesn't matter, then could you explain why you want to write it as s-expressions? - Chip Uni

5 Answers

2
votes

Perhaps you want something like ECL, which compiles Common Lisp to C.

2
votes

How do you mean? Something along the lines of:

(c-expression 
  (int main ((int argc) ((array (pointer char)) argv)
    (block
      (printf "%d arguments\n" argc)
      (if (argc >= 1)
        (printf "The first arg is %s\n" (ref argv 1)))
      (return 0))))
=>
"int main (int argc, char *argv[])
{
  printf("%d arguments\n", argc);
  if (argc >= 1)
    printf(The first arg is %s\n", argv[1]);
  return 0;
}

If so, yes, it's definitely doable. However, it's not entirely trivial.

1
votes

Have a look at Tasuku Hiraishi's SC. His International Lisp Conference papers from 2005 and 2007 are archived at archive.org.

See his source repo, in particular the examples.

To my knowledge that is the best of the many attempts to bring S-Expression style coding practices to bear on an otherwise vanilla C project.

0
votes

I know this is an old post, but for the record, I think this is what is being asked for.

The "lispc" project translates c programs written in an s-expressions style to C. It works great and even compiles the code for you if you set it up right.

I don't know that it produces the most readable C code output at the moment though.