#lang scheme/base

;; C code generator
;; generates a small subset of C language constructs

;; for C BNF see
;; http://lists.canonical.org/pipermail/kragen-hacks/1999-October/000201.html

;; since C code is nested, it doesn't fit the flat assembler code
;; generation framework in BADNOP. however, it is easy to use just a
;; small subset on top of this abstraction that is flat, and can be
;; used as an assembler target language.

;; both EXPRESSION and STATEMENT sublanguages are organized as:
;; - a minimal set of syntax -> string formatting primitives
;; - a symbolic set of syntax -> syntax transformers on top of that

;; the formatter uses an s-expression interpreter interpreter defined
;; in the 'interpreter' macro.

;; note that the extension mechanism (s-expr only) is different from
;; the definition mechanism 'c-code-generators'.

;; This uses syntax objects for the convenience of 'syntax-case' over
;; 'match' only: lexical information is not used.

;; (set-selective-display 3)


 ;; default functionality

 ;; util
 ;; extensions


;; FMT


(define (spaces->string spaces)
  (let loop ((n spaces) (l '()))
    (if (< n 1)
        (list->string l)
        (loop (- n 1) (cons #\space l)))))

(define (no-tab x) x)
(define (default-tab x) (+ 2 x))

(define tab   (make-parameter default-tab))
(define depth (make-parameter 0))

(define (indent . args)
   (apply indent/n args)

(define (indent/n . args)
  (string-append (spaces->string (depth))
                 (apply format-stx args)))

(define (indented e)
      ((depth ((tab) (depth))))
    (statement->string e)))

(define (not-indented e)
  (statement->string e))

(define (expression-statement exp)
  (indent "~a;" (e exp)))
(define (expand-statements stx)
  (apply string-append
         (map-stx not-indented stx)))


(define (expression->string s-exp)
  (transform expression s-exp simple-expression))
(define (statement->string s-exp)
  (transform statement s-exp expression-statement))

;; The namespaces are managed in a hash table. Note: this should be such
;; that later extension to prefixed namespaces is easy to do.

(define expression (cons 'expression (make-hash)))
(define statement  (cons 'statement  (make-hash)))
(define table-name car)
(define table-hash cdr)

(define (resolve table name
                   (lambda ()
                    (error 'undefined
                           (format-stx "~a ~a undefined"
                                       (table-name table)
  (hash-ref (table-hash table)
            (->sexp name)

(define (register table name value)
  (when (resolve table name (lambda () #f))
    (error 'already-defined
           (format-stx "~a ~a already defined"
                       (table-name table) name)))
  (hash-set! (table-hash table) name value))

(define (transform table expr
                     (lambda (stx)
                       (error 'invalid-syntax
                        (format-stx "~a in ~a"
                                    (table-name table) stx)))])
   (if (not (syntax? expr))
       expr ;; done
         (syntax-case expr ()
           ((id . args)
            ((resolve table
                      (->sexp #'id)
                      (lambda ()
                        (abort (default expr))))
           (e (abort (default #'e)))))

(define-sr (transformer . clauses)
  (lambda (stx) (syntax-case stx () . clauses)))

(define-sr (transformers table
            ((name . formals) body ...) ...)
    (register table 'name
               (formals body ...)))

(define-sr (statements . args)  (transformers statement . args))
(define-sr (expressions . args) (transformers expression . args))


;; using short names since they are used a lot. expressions are not
;; indented: they are nested on one line.

;; 2 entry points:

;; e  : expression
;; pe : parenthesized expression if infix
;; for symbol classes

(define-sr (member-tests (thing? lst) ...)
    (define thing?
      (let ((symbols (map string->symbol lst)))
        (lambda (x)
          (and (member
                (syntax->datum x)
                symbols) #t))))

  '("return" "goto" "break" "continue"))
  '("->" "."))
  '("+" "-" "*" "/" "&" "|" "&&" "||" "<<" ">>"
    ">" "<" "<=" ">=" "!=" "=="
    "=" "+=" "-=" "|=" "&=" "<<=" ">>="
    "->" "."

(define (e exp) (expression->string exp))
(define (pe exp)
      ((paren do-wrap-paren))
    (e exp)))

(define (join separator args)
  (if (null? args) ""
      (apply string-append
              (format-stx "~a" (car args))
               (lambda (arg)
                 (format-stx "~a~a" separator arg))
               (cdr args))))))

;; default expression formatter
;; need to wrap infix operators in parens if they are not toplevel.

(define (do-wrap-paren x)    (format-stx "(~a)" x))
(define (dont-wrap-paren x) x)
(define paren (make-parameter dont-wrap-paren))

(define (simple-expression exp)
  (syntax-case exp ()
    ((op left right)
     (infix? #'op)
     (let ((space (if (nospace? #'op) "" " ")))
        (format-stx "~a~a~a~a~a"
                    (pe #'left)
                    space #'op space
                    (pe #'right)))))
    ((op . args)
     (format-stx "~a~a" #'op
                   ((c-keyword? #'op)    " ~a")
                   (else                 "(~a)"))
                  (join ", " (map-stx e #'args)))))
     ;; not a list -> literal/variable
     ;; this needs to be '~s' instead of '~a' to enable literal strings
     (format-stx "~s" exp))))

 ((post  op arg) (format-stx "~a~a"   (e #'arg) #'op))
 ((pre   op arg) (format-stx "~a~a"   #'op (e #'arg)))
 ((index name i) (format-stx "~a[~a]" (pe #'name) (e #'i)))
 ((if test yes no)
  (format-stx "~a ? ~a : ~a"
              (pe #'test) (pe #'yes) (pe #'no)))
 ;; downward let using gcc extension "Statements and
 ;; Declarations in Exressions".
 ((let (decls ...) body ...)
  (format-stx "({\n~a~a"
               #`(statements (vars decls ...) body ...))
              (indent/n "})")))


;; statements are indented.

(define (null->void lst) (if (null? lst) '("void") lst))
(define (declaration d)
  (syntax-case d ()
    ((type name)
     (format-stx "~a ~a" #'type #'name))))
(define (declarations lst)
  (null->void (map-stx declaration lst)))

 ((statements . body) (expand-statements #'body))
 ;;    ((append str) #'str)
 ((label-head name) (indent "~a:" #'name))
 ((comment str) (indent "// ~a" #'str))
 ((line str) (indent "~a" #'str))        
 ((fun-head terminator fn . args)
  (format-stx "~a(~a)~a"
              (declaration #'fn)
              (join ", " (declarations #'args))
 ((indented . statements)
  (apply string-append
         (map-stx indented #'statements)))
 ((for-head . exp-lst)
  (indent "for (~a)" (join "; " (map-stx e #'exp-lst))))
 ((var type name . vallist)
  (indent "~a ~a~a;" (e #'type) #'name
          (syntax-case #'vallist ()
            (() "")
            ((v) (format-stx " = ~a" (e #'v)))))))

;; highlevel statement/expression transformers


 ((def (decls ...) . body) 
     (fun-head "\n" decls ...)
     (block . body)))

 ((decl decls ...)
  #`(fun-head ";\n" decls ...))

 ((label name . body)
  #`((label-head ,name)
     (indented ,@body)))
 ((vars decl ...) 
  #`(statements (var . decl) ...))
 ((for (exp ...) . body)
     (for-head exp ...)
     (block . body)))
 ((bind (decls ...) . body)
     (vars decls ...) . body))
 ((block statement ...)
     (line "{")
     (indented statement ...)
     (line "}"))))

(define (pstat expr)
    (datum->syntax #f expr))))