On Monday, April 13, 2015 at 6:26:38 PM UTC+2, Alexis King wrote:
> What exactly is the difference here between your define-opcode macro and 
> Racket’s define-struct form? For example, simply doing (define-struct A2 
> result left right relop) would generate make-A2, A2? A2-result, A2-left, etc.
> 
> What you describe is certainly possible, but I don’t think it’s currently 
> clear why exactly you’d prefer your solution over just using the struct 
> built-in. Could you elaborate on the differences between your domain-specific 
> approach and the Racket form?
> 
> > On Apr 13, 2015, at 08:11, Nils Van Geele <nils.vge...@gmail.com> wrote:
> > 
> > Hey all,
> > 
> > Bit of context: I'm writing a compiler for a course and decided to use 
> > Racket. I'm translating an AST to three address code-like instructions. 
> > Because I do not want to manually write a great deal of boilerplate code 
> > for each opcode, I figured I could use macros.
> > 
> > I have a struct `instruction' that holds the argument registers/options, 
> > result register, and the opcode. Using the macro I have in mind, I could 
> > for instance generate all boilerplate for the A2 opcode (A = B binop C):
> > 
> >  (define-opcode A2
> >    (result #t)
> >    (arg left)
> >    (arg right)
> >    (arg relop))
> > 
> > This would create the functions:
> > - (make-A2 left right relop result), which creates a new instance of the 
> > instruction struct
> > - (A2? obj), which checks if obj is an instruction struct and the opcode 
> > maches A2
> > - (A2-left obj) and so on, all accessor functions
> > 
> > In an opcode constructor such as make-A2, result is always the last 
> > argument, and all other arguments are in the order in which they are 
> > defined.
> > 
> > I already managed to write a much more simple version, which can be used as 
> > follows:
> > 
> >  (define-opcode A2 #t 3)
> > 
> > Which will just generate functions like make-A2, A2-arg1, A2-arg2, and so 
> > on. However, I prefer the version with (result ...) and (arg ...), but I 
> > just can't figure out how to write a macro for it, given that there must be 
> > only one occurence of (result ...) and zero or more occurences of (arg ...).
> > 
> > I hope someone will be able to help me or point me to some similar macros 
> > for inspiration!
> > 
> > Greetings,
> > Nils Van Geele.
> > 
> > -- 
> > You received this message because you are subscribed to the Google Groups 
> > "Racket Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an 
> > email to racket-users+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.

I could take your approach, but all structs would need to have the 
`instruction' struct as a parent. The most essential thing my macro would do is 
create a constructor that populates all fields of this struct. The instruction 
struct has a field `opcode' that will be used throughout the code for 
dispatching.

Sure, I could write a struct for each different opcode, but they would still 
need to have the `instruction' struct as a parent so `instruction-opcode' 
works. So I would need to write a custom constructor for all the new structs. 
Or I could #:auto the opcode field, and use a macro to generate all the new 
structs with an appropriate #:auto-value. But then we're back at square one :)

I hope you understand what I mean. Plus I'd also like to take this as an 
opportunity to learn more about macros.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to