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

Add TIME type support for conditional operators to interpreter #390

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
65 changes: 65 additions & 0 deletions interpreter/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ func Equal(left, right value.Value) (value.Value, error) {
return &value.Boolean{Value: false}, nil
}
return &value.Boolean{Value: lv.Value == rv.Value}, nil
case value.TimeType:
lv := value.Unwrap[*value.Time](left)
switch right.Type() {
case value.TimeType:
rv := value.Unwrap[*value.Time](right)
return &value.Boolean{
Value: lv.Value.Compare(rv.Value) == 0,
}, nil
default:
return value.Null, errors.WithStack(
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
)
}
}
if left.Type() != right.Type() {
return value.Null, errors.WithStack(
Expand Down Expand Up @@ -208,6 +221,19 @@ func GreaterThan(left, right value.Value) (value.Value, error) {
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
)
}
case value.TimeType:
lv := value.Unwrap[*value.Time](left)
switch right.Type() {
case value.TimeType:
rv := value.Unwrap[*value.Time](right)
return &value.Boolean{
Value: lv.Value.Compare(rv.Value) > 0,
}, nil
default:
return value.Null, errors.WithStack(
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
)
}
default:
return value.Null, errors.WithStack(
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
Expand Down Expand Up @@ -342,6 +368,19 @@ func LessThan(left, right value.Value) (value.Value, error) {
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
)
}
case value.TimeType:
lv := value.Unwrap[*value.Time](left)
switch right.Type() {
case value.TimeType:
rv := value.Unwrap[*value.Time](right)
return &value.Boolean{
Value: lv.Value.Compare(rv.Value) < 0,
}, nil
default:
return value.Null, errors.WithStack(
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
)
}
default:
return value.Null, errors.WithStack(
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
Expand Down Expand Up @@ -479,6 +518,19 @@ func GreaterThanEqual(left, right value.Value) (value.Value, error) {
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
)
}
case value.TimeType:
lv := value.Unwrap[*value.Time](left)
switch right.Type() {
case value.TimeType:
rv := value.Unwrap[*value.Time](right)
return &value.Boolean{
Value: lv.Value.Compare(rv.Value) >= 0,
}, nil
default:
return value.Null, errors.WithStack(
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
)
}
default:
return value.Null, errors.WithStack(
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
Expand Down Expand Up @@ -616,6 +668,19 @@ func LessThanEqual(left, right value.Value) (value.Value, error) {
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
)
}
case value.TimeType:
lv := value.Unwrap[*value.Time](left)
switch right.Type() {
case value.TimeType:
rv := value.Unwrap[*value.Time](right)
return &value.Boolean{
Value: lv.Value.Compare(rv.Value) <= 0,
}, nil
default:
return value.Null, errors.WithStack(
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
)
}
default:
return value.Null, errors.WithStack(
fmt.Errorf("Invalid type comparison %s and %s", left.Type(), right.Type()),
Expand Down
4 changes: 3 additions & 1 deletion interpreter/operator/operator_greater_than_equal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ func TestGreaterThanEqualOperator(t *testing.T) {
{left: &value.Time{Value: now}, right: &value.String{Value: "example", Literal: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.RTime{Value: time.Second}, isError: true},
{left: &value.Time{Value: now}, right: &value.RTime{Value: time.Second, Literal: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.Time{Value: now}, isError: true},
{left: &value.Time{Value: now.Add(1 * time.Second)}, right: &value.Time{Value: now}, expect: true},
{left: &value.Time{Value: now}, right: &value.Time{Value: now}, expect: true},
{left: &value.Time{Value: now}, right: &value.Time{Value: now.Add(1 * time.Second)}, expect: false},
{left: &value.Time{Value: now}, right: &value.Backend{Value: &ast.BackendDeclaration{Name: &ast.Ident{Value: "foo"}}}, isError: true},
{left: &value.Time{Value: now}, right: &value.Boolean{Value: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.Boolean{Value: false, Literal: true}, isError: true},
Expand Down
4 changes: 3 additions & 1 deletion interpreter/operator/operator_greater_than_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ func TestGreaterThanOperator(t *testing.T) {
{left: &value.Time{Value: now}, right: &value.String{Value: "example", Literal: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.RTime{Value: time.Second}, isError: true},
{left: &value.Time{Value: now}, right: &value.RTime{Value: time.Second, Literal: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.Time{Value: now}, isError: true},
{left: &value.Time{Value: now.Add(1 * time.Second)}, right: &value.Time{Value: now}, expect: true},
{left: &value.Time{Value: now}, right: &value.Time{Value: now}, expect: false},
{left: &value.Time{Value: now}, right: &value.Time{Value: now.Add(1 * time.Second)}, expect: false},
{left: &value.Time{Value: now}, right: &value.Backend{Value: &ast.BackendDeclaration{Name: &ast.Ident{Value: "foo"}}}, isError: true},
{left: &value.Time{Value: now}, right: &value.Boolean{Value: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.Boolean{Value: false, Literal: true}, isError: true},
Expand Down
4 changes: 3 additions & 1 deletion interpreter/operator/operator_less_than_equal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ func TestLessThanEqualOperator(t *testing.T) {
{left: &value.Time{Value: now}, right: &value.String{Value: "example", Literal: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.RTime{Value: time.Second}, isError: true},
{left: &value.Time{Value: now}, right: &value.RTime{Value: time.Second, Literal: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.Time{Value: now}, isError: true},
{left: &value.Time{Value: now.Add(1 * time.Second)}, right: &value.Time{Value: now}, expect: false},
{left: &value.Time{Value: now}, right: &value.Time{Value: now}, expect: true},
{left: &value.Time{Value: now}, right: &value.Time{Value: now.Add(1 * time.Second)}, expect: true},
{left: &value.Time{Value: now}, right: &value.Backend{Value: &ast.BackendDeclaration{Name: &ast.Ident{Value: "foo"}}}, isError: true},
{left: &value.Time{Value: now}, right: &value.Boolean{Value: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.Boolean{Value: false, Literal: true}, isError: true},
Expand Down
4 changes: 3 additions & 1 deletion interpreter/operator/operator_less_than_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ func TestLessThanOperator(t *testing.T) {
{left: &value.Time{Value: now}, right: &value.String{Value: "example", Literal: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.RTime{Value: time.Second}, isError: true},
{left: &value.Time{Value: now}, right: &value.RTime{Value: time.Second, Literal: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.Time{Value: now}, isError: true},
{left: &value.Time{Value: now.Add(1 * time.Second)}, right: &value.Time{Value: now}, expect: false},
{left: &value.Time{Value: now}, right: &value.Time{Value: now}, expect: false},
{left: &value.Time{Value: now}, right: &value.Time{Value: now.Add(1 * time.Second)}, expect: true},
{left: &value.Time{Value: now}, right: &value.Backend{Value: &ast.BackendDeclaration{Name: &ast.Ident{Value: "foo"}}}, isError: true},
{left: &value.Time{Value: now}, right: &value.Boolean{Value: true}, isError: true},
{left: &value.Time{Value: now}, right: &value.Boolean{Value: false, Literal: true}, isError: true},
Expand Down