2
votes

For a functional programming assignment, I am writing a scheme macro that translates scheme code to coffeescript code. I am using guile on Linux Mint 12, and have (use-syntax (ice-9 syncase)) active.

Currently, I am trying to nest let-syntax in define-syntax and I keep getting an unexpected 'invalid syntax' error. Please excuse any poor programming habits as I am a scheme newbie. :)

My code:

(define-syntax coffee
  (syntax-rules (define lambda)
    ; translate define function
    ((_ (define arg1 arg2)) 
     (begin
       (let-syntax ((temp
                     (syntax-rules ()
                       ((_) (quote-replace-list 'arg1 'arg2))))))
       (if (list? 'arg1) 
           (string-append (coffee (car 'arg1)) " = " 
                          (comma-splice (cdr 'arg1)) " -> " 
                          (coffee temp))
           (string-append (coffee 'arg1) " = " 
                          (coffee arg2)))))))

and my output:

guile> (load "hw2-retry.scm")
guile> (coffee (define (x y) (+ x y)))
ERROR: invalid syntax (let-syntax ((temp (syntax-rules () ((_)
(quote-replace-list (quote #) (quote #)))))))
ABORT: (misc-error)

After several of trying to research this error, I'm starting to think I am just not understanding something fundamental. I was sure you could nest a let-syntax inside a define-syntax. What am I doing wrong?

1
What are you trying to achieve? What output were you expecting from your sample?Chris Jester-Young
(coffee (define (x y) (+ x y)) should convert the define statement from scheme into equivalent coffeescript. Though, this particular example is kinda nonsensical. if I have my syntax right, the output should be: x = (y) -> x + ycali-spc
let-syntax is like let in the sense that it's scoped around a body. But your use of let-syntax here has no body. You can tell this because the (let-syntax ...) is indented at the same level as the (if (list? ...) ...).dyoo

1 Answers

1
votes

From the Scheme R5RS specification, let-syntax has the following syntax:

 (let-syntax <bindings> <body>) 

Syntax: should have the form

((<keyword> <transformer spec>) ...)

The problem with let-syntax in your code (or at least, one of the problems) is that the if should be enclosed in the body portion, as dyoo mentioned. In other words, you should indent the if, remove a closing parenthesis from the end of line 8, and add that closing paren at the end of line 14.