[racket] Metaprogramming with scheme

From: Valeriya Pudova (valery at digitalchile.net)
Date: Mon Jun 21 09:41:00 EDT 2010

> > name space
> > where are defined methods "define-fsm", "define-state", "on-event" and
> "go"
> >
> > But this case each of those methods will not have the syntax object of
> the
> > evaluated expression.
> > And in case if it will find and error it could not message about error's
> > location. The easiest way to do this task can be: Just evaluate the file
> "a.ss" in the
>

Noel Welsh wrote:

> Have you tried eval-syntax, instead of eval? I think that will keep
> location information.
>


How?

(eval-syntax (file->syntax "a.ss"))

lets imagine the function 'define-fsm' was called. And "some-condition?"
informs that there are syntax error in the source expression. How i can
printout the line and column number of the source expression?

(define (define-fsm params)
  (if (some-condition? params)
     (display "error message" ????? )
  )
)

Noel Welsh  wrote:
> Look up read-syntax.

Yes, In my example file->syntax was somthing like

(define (file->syntax file-name)
  ;; read the file and convert it to the syntax object
  (let* ([in (open-input-file file-name)])
    (port-count-lines! in)
    (begin0
      (let loop ([rs '()]
                 [r (read-syntax file-name in)])
        (if (eof-object? r)
            (reverse rs)
            (loop (cons r rs) (read-syntax file-name in))))
      (close-input-port in)
    )))

Noel Welsh <noelwelsh at gmail.com> wrote:
> lets imagine the function 'define-fsm' was called. And "some-condition?"
> informs that there are syntax error in the source expression. How i can
> printout the line and column number of the source expression?

> http://docs.racket-lang.org/unstable/Source_Locations.html<http://docs.racket-lang.org/unstable/Source_Locations.html>


The question is: when in the eval-synax process there will be called some
function how that function can to reffer to the current syntax-object?

Noel Welsh wrote:
> foo.ss:

> (foo 1 2)

> Code:

> (with-input-from-file "foo.ss"
> (lambda ()
>    (parameterize ([error-display-handler (lambda (name . stuff)
> (printf "Got ~a ~a\n" name stuff))])
>      (eval-syntax (read-syntax 'foo.ss)))))

> Output:

> Got foo.ss::0: compile: unbound identifier (and no #%app syntax
> transformer is bound) at: foo in: (foo 1 2) (#(struct:exn:fail:syntax
> foo.ss::0: compile: unbound identifier (and no #%app syntax
> transformer is bound) at: foo in: (foo 1 2) #<continuation-mark-set>
> (#<syntax::2 foo>)))

> So the exception structure has the syntax object you want.

There are two issues.

First:

what if the file foo.ss will have defined function foo.

(define (foo a b) (+ a b)))
(foo 1 2)

It makes error:
Got compile: unbound identifier (and no #%app syntax transformer is bound)
(#(struct:exn:fail:syntax compile: unbound identifier (and no #%app syntax
transformer is bound) #<continuation-mark-set>

Second:
When function foo is defined correctly and called from the eval process and
errors occurs in foo, how can we find the caller's location in order to
format an error message?


-- Valeriya
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100621/a352a9be/attachment.html>

Posted on the users mailing list.