mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
config/lang: add modulo
This commit is contained in:
parent
3c4a036fb5
commit
5848cba6ad
@ -9,4 +9,5 @@ const (
|
|||||||
ArithmeticOpSub
|
ArithmeticOpSub
|
||||||
ArithmeticOpMul
|
ArithmeticOpMul
|
||||||
ArithmeticOpDiv
|
ArithmeticOpDiv
|
||||||
|
ArithmeticOpMod
|
||||||
)
|
)
|
||||||
|
@ -77,6 +77,8 @@ func builtinIntMath() ast.Function {
|
|||||||
result *= arg
|
result *= arg
|
||||||
case ast.ArithmeticOpDiv:
|
case ast.ArithmeticOpDiv:
|
||||||
result /= arg
|
result /= arg
|
||||||
|
case ast.ArithmeticOpMod:
|
||||||
|
result = result % arg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,6 +131,11 @@ func (tc *typeCheckArithmetic) TypeCheck(v *TypeCheck) (ast.Node, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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")
|
||||||
|
}
|
||||||
|
|
||||||
// Return type
|
// Return type
|
||||||
v.StackPush(mathType)
|
v.StackPush(mathType)
|
||||||
|
|
||||||
|
@ -71,6 +71,14 @@ func TestEval(t *testing.T) {
|
|||||||
ast.TypeString,
|
ast.TypeString,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"foo ${42%4}",
|
||||||
|
nil,
|
||||||
|
false,
|
||||||
|
"foo 2",
|
||||||
|
ast.TypeString,
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"foo ${42.0+1.0}",
|
"foo ${42.0+1.0}",
|
||||||
nil,
|
nil,
|
||||||
|
@ -180,6 +180,9 @@ func (x *parserLex) lexModeInterpolation(yylval *parserSymType) int {
|
|||||||
case '/':
|
case '/':
|
||||||
yylval.token = &parserToken{Value: ast.ArithmeticOpDiv}
|
yylval.token = &parserToken{Value: ast.ArithmeticOpDiv}
|
||||||
return ARITH_OP
|
return ARITH_OP
|
||||||
|
case '%':
|
||||||
|
yylval.token = &parserToken{Value: ast.ArithmeticOpMod}
|
||||||
|
return ARITH_OP
|
||||||
default:
|
default:
|
||||||
x.backup()
|
x.backup()
|
||||||
return x.lexId(yylval)
|
return x.lexId(yylval)
|
||||||
|
Loading…
Reference in New Issue
Block a user