Multi-line macros for domain specific language

macros
metaprogramming

#1

Hi,

I just wondered whether it would be feasible to implement something like

@problem begin
    minimize: f(x)
    subject to:
      g1(x) =  a
      g2(x) <= b   
end

or similar to work properly. Essentially this is nothing but fancy syntax for a constructor of a ‘problem’ struct, is it not?
Are you aware of docs/tutorials of how to achieve this kind of domain specific language?
Also, I was thinking that this would be nice to have in JuMP as a more ‘literate’ way of specifying models.


#2

First step is to find something which parses. Your example does not parse:

julia> quote
           minimize: f(x)
           subject to:
             g1(x) =  a
             g2(x) <= b   
       end                                                                                                                                            
ERROR: syntax: extra token "to" after end of expression                                                                                               

Then de-construct the AST and turn it into what you want. Best have a read of https://julialang.org/blog/2017/08/dsl


#3

Thanks for the link!
So, there is no way of actually extending the language syntax via macros - I can only modify valid Julia expressions? I could thus only manually parse strings directly?


#4

A macro takes an AST and returns a new AST so yes, it needs to be valid syntax. If you are parsing strings you can, of course, do whatever you want.


#5

If you have some flexibility in what exactly your DSL should look like, this has no syntax errors:

quote
  minimize: f(x)
  @constraints begin
    g1(x) =  a
    g2(x) <= b
  end
end

#6

If you want a DSL that doesn’t have to be parsable Julia code then you can write it using string macros. One big example for this is MATLAB.jl where it uses string macros instead of “standard macros” since not all MATLAB code parses as Julia code.

The advantage of a standard macro is that it parses as a Julia AST which makes it easy to handle. The disadvantage is that it has to be able to parse into an AST. The advantage of a string macro is that it can literally be applied to any string. The disadvantage is that then you have to parse it all yourself.