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

Improve code generation with text/template #32

Closed
UserAB1236872 opened this issue Nov 4, 2014 · 4 comments
Closed

Improve code generation with text/template #32

UserAB1236872 opened this issue Nov 4, 2014 · 4 comments

Comments

@UserAB1236872
Copy link
Member

Issues #30 and #29 are basically on hold because of some of the difficulty in rewriting the code generation. I think it would just plain be easier to rewrite the incredibly ad-hoc, ugly code gen script with text/template.

The new version likely would no longer use the Makefile, and instead directly generate the float64 version due to text/template's greater flexibility.

@dmitshur
Copy link
Member

dmitshur commented Nov 4, 2014

There's also go generate to consider.

@UserAB1236872
Copy link
Member Author

That's coming in 1.4, right? I'm not too familiar with it, but the proposal I read simply allowed it to run arbitrary user-written code generation scripts, right? So doing this would still be necessary.

@dmitshur
Copy link
Member

dmitshur commented Mar 2, 2015

Yes, go generate is just a standard way for Go packages to run some commands to update their source code.

It's nice because it's a standard that's easy to follow and makes contributing to various Go packages easier. You know it's always go generate, rather than make or update_code.sh or run_regen or something else custom and non-standard.

See here for an example of replacing a bash file with go generate directives: gopherjs/websocket@c80adc1.

@alexvasi
Copy link
Contributor

alexvasi commented Mar 6, 2015

I tried to rewrite genprog using text/template. Only vectors for now. Here is the result:
alexvasi@1ed7256

The main idea was to place all crucial information directly in the template and not in the program that generates code. I know, I could have passed meta info about types and functions to template. Or I could have used sub-templates for each function for example. In the end I decided against it. Template almost looks like a go code, if you squint hard enough. And that's a good thing, I guess. Except maybe there will be float32/64 flag later.

First of all it works. It is a zero-diff change... well, not actually. The order of functions in vector.go is different now. It takes 10 or so additional lines in template to make the order the same. I dumped them. But that's how I tested new generator.

Here is some caveats:

  • text/template is sloppy with whitespaces. I abused gofmt and it's called after template rendering.
  • I changed template separators from {{ to <<. It is too many curly brackets in go code already.
  • The readability of vector.tpl is not perfect.
  • Broken diff. It is hard to validate new generator with changed functions order. I should add some tests for each vector function to vec_test.go anyway.

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

No branches or pull requests

3 participants