Subject: Re: S-expr form of C
From: rpw3@rpw3.org (Rob Warnock)
Date: Thu, 30 Oct 2008 23:22:55 -0500
Newsgroups: comp.lang.lisp
Message-ID: <0rednXhrnKKCFZfUnZ2dnUVZ_uednZ2d@speakeasy.net>
Bakul Shah  <bakul+usenet@bitblocks.com> wrote:
+---------------
| Rob Warnock wrote:
| > ...is there any generally-accepted "best style" in the Lisp
| > community for representing C source code in s-expr form?
| 
| You mean something like a C->s-expr converter? Why bother?
| Anyway this is easy; just have your parser spit out an
| abstract syntax tree as an S-expr.
+---------------

There's no "parser"; I'm writing original C code from scratch,
but want to be able to use DEFMACRO to make it easier [since
a rather large part of the code can be generated with macros].

Note that I have done stuff like this in the past by simply
pasting strings together [e.g., it's *amazing* how neatly
FORMAT can emit large C array initializations!], but I'd like
to be able to write in pure s-expr form, sort of like using
CL-SQL forms for queries instead of bare SQL strings (as with PG).

The canonical example, with one possible syntax:

    (c-defun (main int) ((int count) ((array nil (array nil char)) args))
      (unless (= count 2)
	(warn "~a: argument required" (aref args 0))
	(exit 1))
      (c-let ((((array nil char) p) (aref args 1)))
	(unless (string= p "foo")
	  (error "Try again!"))
      (exit 0))

Another variant would be to stick closer to CL declaration style, e.g.:

    (c-declaim (ftype (function (int (array * (array * char))) int) main))
    (c-defun main (count args)
      ...)

or:

    (c-defun main (count args)
      (c-declare (ftype (function (t t) int) main)
                 (type int count)
		 (type (array * (array * char)) args))
      ...)

or something of that ilk...


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607