2

I'm working in Guile Scheme. I'm making macros but I'm finding that I'm repeating a lot of boilerplate in my output. I'm fairly new to Scheme so if there's a better way to approach this than syntax-case I'm open to advice.

In this macro there's only one difference in the input, "set" vs. "setconst", and one difference in the output, "elementReference" vs. "stringValue". I've tried using #` with #, to unquote but I get error messages like " syntax: missing ellipsis in form (syntax set)". Ideally I'd like to put some scheme code inside my macro to distinguish between set and setconst, and change the output accordingly, but as I said this is new and if there's a better way to get my result I'm open.

Here's my code:

  (define-syntax assign
    (lambda (stx)
      (syntax-case stx (then)
        ((assign aname then target (set to from) ...) #'(assignments (name aname) 
                                                                     (label aname) 
                                                                     (locationX 50) 
                                                                     (locationY 50) 
                                                                     ((assignmentItems 
                                                                       (operator Assign) 
                                                                       (assignToReference to) 
                                                                       (value (elementReference from))) ...)
                                                                     (connector (targetReference target))))
        ((assign aname then target (setconst to from) ...) #'(assignments (name aname) 
                                                                     (label aname) 
                                                                     (locationX 50) 
                                                                     (locationY 50) 
                                                                     ((assignmentItems 
                                                                       (operator Assign) 
                                                                       (assignToReference to) 
                                                                       (value (stringValue from))) ...)
                                                                     (connector (targetReference target))))
        )))

rr_cook
  • 136
  • 4

1 Answers1

0

I've come up with one solution but I still welcome input for better ways to do this. Instead of if-fing or unquoting I've made a simpler macro just for the difference and am calling it in my bigger macro. This should work as I expand my DSL.

(define-syntax assign-from
  (lambda (stx)
    (syntax-case stx (set setconst)
      ((assign-operator set to from) #'(elementReference from))
      ((assign-operator setconst to from) #'(stringValue from)))))

(define-syntax assign
  (lambda (stx)
    (syntax-case stx (then)
      ((assign aname then target (set to from) ...) #'(assignments (name aname) 
                                                                   (label aname) 
                                                                   (locationX 50) 
                                                                   (locationY 50)
                                                                   ((assignmentItems 
                                                                     (assignToReference to) 
                                                                     (operator Assign) 
                                                                     (value (assign-from set to from))) ...)
                                                                   (connector (targetReference target))))
      )))
rr_cook
  • 136
  • 4