config/lang: add modulo
This commit is contained in:
parent
3c4a036fb5
commit
5848cba6ad
|
@ -9,4 +9,5 @@ const (
|
|||
ArithmeticOpSub
|
||||
ArithmeticOpMul
|
||||
ArithmeticOpDiv
|
||||
ArithmeticOpMod
|
||||
)
|
||||
|
|
|
@ -77,6 +77,8 @@ func builtinIntMath() ast.Function {
|
|||
result *= arg
|
||||
case ast.ArithmeticOpDiv:
|
||||
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
|
||||
v.StackPush(mathType)
|
||||
|
||||
|
|
|
@ -71,6 +71,14 @@ func TestEval(t *testing.T) {
|
|||
ast.TypeString,
|
||||
},
|
||||
|
||||
{
|
||||
"foo ${42%4}",
|
||||
nil,
|
||||
false,
|
||||
"foo 2",
|
||||
ast.TypeString,
|
||||
},
|
||||
|
||||
{
|
||||
"foo ${42.0+1.0}",
|
||||
nil,
|
||||
|
|
|
@ -180,6 +180,9 @@ func (x *parserLex) lexModeInterpolation(yylval *parserSymType) int {
|
|||
case '/':
|
||||
yylval.token = &parserToken{Value: ast.ArithmeticOpDiv}
|
||||
return ARITH_OP
|
||||
case '%':
|
||||
yylval.token = &parserToken{Value: ast.ArithmeticOpMod}
|
||||
return ARITH_OP
|
||||
default:
|
||||
x.backup()
|
||||
return x.lexId(yylval)
|
||||
|
|
Loading…
Reference in New Issue