config/lang: add modulo

This commit is contained in:
Mitchell Hashimoto 2015-02-26 15:17:37 -08:00
parent 3c4a036fb5
commit 5848cba6ad
5 changed files with 19 additions and 0 deletions

View File

@ -9,4 +9,5 @@ const (
ArithmeticOpSub
ArithmeticOpMul
ArithmeticOpDiv
ArithmeticOpMod
)

View File

@ -77,6 +77,8 @@ func builtinIntMath() ast.Function {
result *= arg
case ast.ArithmeticOpDiv:
result /= arg
case ast.ArithmeticOpMod:
result = result % arg
}
}

View File

@ -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)

View File

@ -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,

View File

@ -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)