[racket] Macro : Reify a function body definition

From: antoine (antoine.brand at sfr.fr)
Date: Sat Aug 23 13:28:02 EDT 2014

Hello,

If i understand well, you want to have access to the source code of foo.

Here is a program that give you the source code:

#lang racket

(define-syntax (define-equation stx)
  (syntax-case stx ()
    [(_ (name . params) value ...)
     (begin
       (when (not (symbol? (syntax-e #'name)))
         (raise "not a symbol"))
       #`(begin (define #,(cons #'name #'params) value ...)
                (define-for-syntax name (list (quote params) (quote value ...)))))]))

(define-equation (foo x) (+ x (sin x)))
;; => (define (foo x) (+ x (sin x)))
;;    (define-for-syntax foo '((x) (+ x (sin x))))

(define-syntax (derivate stx)
  (define sym (syntax-e (cadr (syntax-e stx))))
  (print (eval sym))
  ;;do the stuff you want here with the code
  #'1)

(derivate foo)

Two things to mentions:

- The source code data is given as a list of symbols instead of a syntax objects
  so you can't do free-identifier=?. You should rewrite define-equation with
  syntax-parse instead of syntax-case. But know just for the beginning i think
  you can live with that.

- I use eval inside derivate to fetch the data of foo at phase 1 i suspect it is
  not the right way to do it.

Hope it helps.

Posted on the users mailing list.