Open shadowcat-mst opened 5 years ago
Thanks for the question! I've never added docs about macros and code generation, and your question is a good occasion to do that.
You can start from doc/code_generation.pdf (just added): it contains the slides about code generation I presented last October at GoLab 2018 with an introduction to the topic.
After reading that, I suggest experimenting with macros and quasiquoting at gomacro prompt to get a practical feeling of how it works. The gomacro command :inspect EXPRESSION
is very handy to examine the content of complicated data structures, as the Abstract Syntax Trees produced by macros and quasiquote.
Then you should be able "use the source" i.e. to decipher actual code that uses them - for example the files fast/*.gomacro
For reference: macros and quasiquote are designed to be almost identical to Common Lisp macros and backquote/quasiquote - but Common Lisp material is very hard to read if you do not know the language.
Ironically, at this point I know lisp better than go. Cheers for the pointers, I'll return when I find some time to dereference them
Just to check, is it accurate to say:
Definitions of things preceded by ':' happen at compile time. Macros are then applied to the code being compiled that hasn't been compile time lifted, but macro arguments must be provided as separate expressions with semicolons between, ala 'macroname; arg1; arg2;' ?
(the quasiquoting stuff makes complete sense, it's everything else I'm slightly confused by)
True, those parts are not explained.
There is no syntax to invoke a macro: you use the syntax of the place where you insert the macro call.
If you invoke a macro at top level, where Go expects a declaration or statement, you use statement syntax: macroname; arg1; arg2; arg3
etc.
If you invoke a macro among the arguments of a function call or the cases of a switch, you use the expression list syntax, for example:
funname(arg1, arg2, macroname, arg3, arg4)
or
case expr1, macroname, expr2, expr3:
etc.
The prefix :
has no effect if you start gomacro normally. If instead you start it with the command-line option -m
it runs in preprocessor mode: it performs only macro expansion on the sources then outputs them, without compiling or executing them. In such case, to re-enable compiling and executing source code - for example to define a function or macro, import a package, etc, you must prefix the statement or declaration with :
The readme doesn't seem to show me how the macro stuff works and I don't understand the codebase enough to just UTSL, but would like to know what to read to figure it out. Not expecting a trivial answer, just wanting suggestions as to where to start. Ideas?