-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
684228e
config/lang: add math operations for ints
mitchellh 07b78fd
config/lang: math operations
mitchellh 9ddcaf1
config/lang: fix shift/reduce conflict
mitchellh 6750318
config/lang: add test with expressions
mitchellh 3c4a036
config/lang: add (expr) for order of ops
mitchellh 5848cba
config/lang: add modulo
mitchellh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ast | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
// Arithmetic represents a node where the result is arithmetic of | ||
// two or more operands in the order given. | ||
type Arithmetic struct { | ||
Op ArithmeticOp | ||
Exprs []Node | ||
Posx Pos | ||
} | ||
|
||
func (n *Arithmetic) Accept(v Visitor) Node { | ||
for i, expr := range n.Exprs { | ||
n.Exprs[i] = expr.Accept(v) | ||
} | ||
|
||
return v(n) | ||
} | ||
|
||
func (n *Arithmetic) Pos() Pos { | ||
return n.Posx | ||
} | ||
|
||
func (n *Arithmetic) GoString() string { | ||
return fmt.Sprintf("*%#v", *n) | ||
} | ||
|
||
func (n *Arithmetic) String() string { | ||
var b bytes.Buffer | ||
for _, expr := range n.Exprs { | ||
b.WriteString(fmt.Sprintf("%s", expr)) | ||
} | ||
|
||
return b.String() | ||
} | ||
|
||
func (n *Arithmetic) Type(Scope) (Type, error) { | ||
return TypeInt, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ast | ||
|
||
// ArithmeticOp is the operation to use for the math. | ||
type ArithmeticOp int | ||
|
||
const ( | ||
ArithmeticOpInvalid ArithmeticOp = 0 | ||
ArithmeticOpAdd ArithmeticOp = iota | ||
ArithmeticOpSub | ||
ArithmeticOpMul | ||
ArithmeticOpDiv | ||
ArithmeticOpMod | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,9 @@ func (v *TypeCheck) visit(raw ast.Node) ast.Node { | |
var result ast.Node | ||
var err error | ||
switch n := raw.(type) { | ||
case *ast.Arithmetic: | ||
tc := &typeCheckArithmetic{n} | ||
result, err = tc.TypeCheck(v) | ||
case *ast.Call: | ||
tc := &typeCheckCall{n} | ||
result, err = tc.TypeCheck(v) | ||
|
@@ -70,7 +73,7 @@ func (v *TypeCheck) visit(raw ast.Node) ast.Node { | |
default: | ||
tc, ok := raw.(TypeCheckNode) | ||
if !ok { | ||
err = fmt.Errorf("unknown node: %#v", raw) | ||
err = fmt.Errorf("unknown node for type check: %#v", raw) | ||
break | ||
} | ||
|
||
|
@@ -86,6 +89,72 @@ func (v *TypeCheck) visit(raw ast.Node) ast.Node { | |
return result | ||
} | ||
|
||
type typeCheckArithmetic struct { | ||
n *ast.Arithmetic | ||
} | ||
|
||
func (tc *typeCheckArithmetic) TypeCheck(v *TypeCheck) (ast.Node, error) { | ||
// The arguments are on the stack in reverse order, so pop them off. | ||
exprs := make([]ast.Type, len(tc.n.Exprs)) | ||
for i, _ := range tc.n.Exprs { | ||
exprs[len(tc.n.Exprs)-1-i] = v.StackPop() | ||
} | ||
|
||
// Determine the resulting type we want | ||
mathFunc := "__builtin_IntMath" | ||
mathType := ast.TypeInt | ||
switch v := exprs[0]; v { | ||
case ast.TypeInt: | ||
mathFunc = "__builtin_IntMath" | ||
mathType = v | ||
case ast.TypeFloat: | ||
mathFunc = "__builtin_FloatMath" | ||
mathType = v | ||
default: | ||
return nil, fmt.Errorf( | ||
"Math operations can only be done with ints and floats, got %s", | ||
v) | ||
} | ||
|
||
// Verify the args | ||
for i, arg := range exprs { | ||
if arg != mathType { | ||
cn := v.ImplicitConversion(exprs[i], mathType, tc.n.Exprs[i]) | ||
if cn != nil { | ||
tc.n.Exprs[i] = cn | ||
continue | ||
} | ||
|
||
return nil, fmt.Errorf( | ||
"operand %d should be %s, got %s", | ||
i+1, mathType, arg) | ||
} | ||
} | ||
|
||
// Modulo doesn't work for floats | ||
if mathType == ast.TypeFloat && tc.n.Op == ast.ArithmeticOpMod { | ||
return nil, fmt.Errorf("modulo cannot be used with floats") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah here we go - very explcit |
||
|
||
// Return type | ||
v.StackPush(mathType) | ||
|
||
// Replace our node with a call to the proper function. This isn't | ||
// type checked but we already verified types. | ||
args := make([]ast.Node, len(tc.n.Exprs)+1) | ||
args[0] = &ast.LiteralNode{ | ||
Value: tc.n.Op, | ||
Typex: ast.TypeInt, | ||
Posx: tc.n.Pos(), | ||
} | ||
copy(args[1:], tc.n.Exprs) | ||
return &ast.Call{ | ||
Func: mathFunc, | ||
Args: args, | ||
Posx: tc.n.Pos(), | ||
}, nil | ||
} | ||
|
||
type typeCheckCall struct { | ||
n *ast.Call | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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. 😏
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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