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

Add math support to embedded interpolation language #1068

Merged
merged 6 commits into from
Feb 27, 2015
Merged

Conversation

mitchellh
Copy link
Contributor

Fixes #686

This adds basic arithmetic to the embedded interpolation language: add, subtract, multiply, divide, modulo for both ints and floats (except mod for floats, of course). This lets you do fancy things like ${count.index+1}. The changes made in this PR:

  • Add math operations to lexer (+, -, *, /, %)
  • Parse math in parser: expr OP expr and '(' expr ')' (for orders of operations).
  • Arithmetic operation node into the AST
  • Type check for arithmetic
  • AST transformation: replace arithmetic operations with function calls to builtins to do the actual math. This lets us do it cleanly in Go vs. creating a special case evaluable node.
  • Added a few implicit conversions to make this more usable: int to float, float to int, and float to string.

@mitchellh
Copy link
Contributor Author

Ready for review as best as possible @phinze. I don't expect you'll have to see this area of the code much, but could be useful to see how something like this is added. :)

result *= arg
case ast.ArithmeticOpDiv:
result /= arg
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"You don't get fload mod, because if you need float mod in your terraform config, you're doing something very wrong."

^^ This the logic? Sounds about right to me. 😏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, float modulus doesn't make sense or even work (syntax error in Go).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well there is http://golang.org/pkg/math/#Mod but the logic still stands

@phinze
Copy link
Contributor

phinze commented Feb 27, 2015

LGTM!

Obligatory:

mitchellh added a commit that referenced this pull request Feb 27, 2015
Add math support to embedded interpolation language
@mitchellh mitchellh merged commit d01c764 into master Feb 27, 2015
@mitchellh mitchellh deleted the f-lang-math branch February 27, 2015 21:44
bobtfish added a commit to bobtfish/terraform that referenced this pull request Apr 3, 2015
phinze added a commit that referenced this pull request Apr 6, 2015
@ghost
Copy link

ghost commented May 4, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators May 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Need way to interpolate count index as other than zero-based (for host names)
3 participants