Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question - reading material to learn writing macros? #61

Open
shadowcat-mst opened this issue May 11, 2019 · 4 comments
Open

Question - reading material to learn writing macros? #61

shadowcat-mst opened this issue May 11, 2019 · 4 comments
Assignees
Labels

Comments

@shadowcat-mst
Copy link

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?

@cosmos72
Copy link
Owner

cosmos72 commented May 11, 2019

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.

@cosmos72 cosmos72 self-assigned this May 11, 2019
@cosmos72 cosmos72 changed the title Stupid newbie request - what do I read to try writing macros? Question - reading material to learn writing macros? May 11, 2019
@shadowcat-mst
Copy link
Author

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

@shadowcat-mst
Copy link
Author

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)

@cosmos72
Copy link
Owner

cosmos72 commented May 12, 2019

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 :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants