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

feat(shield): add arithmetic operations #330

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* (shield) Add new operators: `<`, `>`, `<=`, `>=`, `==`, `!=` for comparing integers to each others.
* (shield) Add support for `string` objects
* (shield) Add `contains(elem, array)` builtin function for checking if `elem` is contained in the `array`
* (shield) Add `+`, `-`, `*`, `/` math operators for basic integer arithmetic
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure consistent punctuation in list items.

Consider adding a period at the end of the entry for consistency with other entries in the list.

- * (shield) Add `+`, `-`, `*`, `/` math operators for basic integer arithmetic
+ * (shield) Add `+`, `-`, `*`, `/` math  operators for basic integer arithmetic.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
* (shield) Add `+`, `-`, `*`, `/` math operators for basic integer arithmetic
* (shield) Add `+`, `-`, `*`, `/` math operators for basic integer arithmetic.


### Features

Expand Down
55 changes: 35 additions & 20 deletions api/shield/token/token.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions proto/shield/token/token.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ enum Type {
GTE = 17;
LTE = 18;

TRUE = 19;
FALSE = 20;
ADD = 19;
SUB = 20;
MUL = 21;
DIV = 22;

TRUE = 23;
FALSE = 24;
}

message Token {
Expand Down
10 changes: 10 additions & 0 deletions shield/internal/evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ func evalInfixExpression(exp *ast.InfixExpression, env env.Environment) object.O
return err
}
return nativeBoolToBooleanObject(sign <= 0)

// arithmetic operators
case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ && exp.Operator == "+":
return &object.Integer{Value: left.(*object.Integer).Value + right.(*object.Integer).Value}
case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ && exp.Operator == "-":
return &object.Integer{Value: left.(*object.Integer).Value - right.(*object.Integer).Value}
case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ && exp.Operator == "*":
return &object.Integer{Value: left.(*object.Integer).Value * right.(*object.Integer).Value}
case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ && exp.Operator == "/":
return &object.Integer{Value: left.(*object.Integer).Value / right.(*object.Integer).Value}
}

return newError("unknown operator: %s %s %s", left.Type(), exp.Operator, right.Type())
Expand Down
23 changes: 23 additions & 0 deletions shield/internal/evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func TestEvalBooleanExpression(t *testing.T) {
{`"10" >= "999"`, false},
{`"999" <= "999"`, true},
{`"999" <= "10"`, false},
{"2 + 2 == 2 * 2", true},
{"20 / 2 > 3 + 7", false},
// disabled for now as we don't support mixed string to int comparisons
// {`"10" > 1`, true},
// {`"1" < 10`, true},
Expand All @@ -62,6 +64,27 @@ func TestEvalBooleanExpression(t *testing.T) {
}
}

func TestArithmeticInteger(t *testing.T) {
tests := []struct {
input string
expected int64
}{
{"1 + 2", 3},
{"10 - 4", 6},
{"2 * 5", 10},
{"8 / 2", 4},
{"2 + 2 * 2", 6},
{"2 * 2 + 2", 6},
{"(2 + 2) * 2", 8},
{"(10 + 2) / (6 - 3) * 4", 16},
{"5 / 2 * 2", 4},
}
for _, tt := range tests {
evaluated := testEval(tt.input, nil)
testIntegerObject(t, evaluated, tt.expected)
}
}

func testEval(input string, envMap map[string]bool) object.Object {
l := lexer.New(input)
p := parser.New(l)
Expand Down
8 changes: 8 additions & 0 deletions shield/internal/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ func (l *Lexer) NextToken() token.Token {
case '"':
l.readChar()
tok = l.readString()
case '+':
tok = newToken(token.Type_ADD, l.ch)
case '-':
tok = newToken(token.Type_SUB, l.ch)
case '*':
tok = newToken(token.Type_MUL, l.ch)
case '/':
tok = newToken(token.Type_DIV, l.ch)
case 0:
tok.Literal = ""
tok.Type = token.Type_EOF
Expand Down
6 changes: 5 additions & 1 deletion shield/internal/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestNextToken(t *testing.T) {
input := `any(2, [warden123, wardenXXX]) true false && || 1 > 1 < 1 >= 1 <= 1 == 1 != 1 "some string""";`
input := `any(2, [warden123, wardenXXX]) true false && || 1 > 1 < 1 >= 1 <= 1 == 1 != 1 "some string""" + - * /;`

tests := []struct {
expectedType token.Type
Expand Down Expand Up @@ -43,6 +43,10 @@ func TestNextToken(t *testing.T) {
{token.Type_INT, "1"},
{token.Type_STRING, "some string"},
{token.Type_STRING, ""},
{token.Type_ADD, "+"},
{token.Type_SUB, "-"},
{token.Type_MUL, "*"},
{token.Type_DIV, "/"},
{token.Type_SEMICOLON, ";"},
{token.Type_EOF, ""},
}
Expand Down
10 changes: 10 additions & 0 deletions shield/internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
AND
EQ
LT_GT
ADD_SUB
MUL_DIV
CALL
)

Expand All @@ -28,6 +30,10 @@ var precedences = map[token.Type]int{
token.Type_GTE: LT_GT,
token.Type_LT: LT_GT,
token.Type_LTE: LT_GT,
token.Type_ADD: ADD_SUB,
token.Type_SUB: ADD_SUB,
token.Type_MUL: MUL_DIV,
token.Type_DIV: MUL_DIV,
token.Type_LPAREN: CALL,
}

Expand Down Expand Up @@ -70,6 +76,10 @@ func New(l *lexer.Lexer) *Parser {
p.registerInfix(token.Type_LTE, p.parseInfixExpression)
p.registerInfix(token.Type_EQ, p.parseInfixExpression)
p.registerInfix(token.Type_NEQ, p.parseInfixExpression)
p.registerInfix(token.Type_ADD, p.parseInfixExpression)
p.registerInfix(token.Type_SUB, p.parseInfixExpression)
p.registerInfix(token.Type_MUL, p.parseInfixExpression)
p.registerInfix(token.Type_DIV, p.parseInfixExpression)
p.registerInfix(token.Type_LPAREN, p.parseCallExpression)

// Read two tokens, so curToken and peekToken are both set
Expand Down
24 changes: 24 additions & 0 deletions shield/internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,30 @@ func TestParser(t *testing.T) {
`foo > "foo"`,
"(foo > \"foo\")",
},
{
`2 + 2 * 2`,
"(2 + (2 * 2))",
},
{
`2 - 2 * 2`,
"(2 - (2 * 2))",
},
{
`2 + 2 / 2`,
"(2 + (2 / 2))",
},
{
`2 - 2 / 2`,
"(2 - (2 / 2))",
},
{
`2 + 2 <= 2 * 2`,
"((2 + 2) <= (2 * 2))",
},
{
`6 / 3 != 7 - 4`,
"((6 / 3) != (7 - 4))",
},
}

for _, tt := range tests {
Expand Down
71 changes: 42 additions & 29 deletions shield/token/token.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion warden/x/warden/types/v1beta2/signature.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading