Skip to content

Commit

Permalink
add string comparison support (#294)
Browse files Browse the repository at this point in the history
* enable relative imports

* update per 1st review

* remove symlink stuffs

* fix cli run in make file

* make resolving import path explicit

* fix importDir

* add string comparison operators

* fix duplicates
  • Loading branch information
Ozan Hacıbekiroğlu authored May 25, 2020
1 parent d1dd014 commit 366c699
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
8 changes: 7 additions & 1 deletion docs/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@
- `(string) + (string) = (string)`: concatenation
- `(string) + (other types) = (string)`: concatenation (after string-converted)

### Comparison Operators

- `(string) < (string) = (bool)`: less than
- `(string) > (string) = (bool)`: greater than
- `(string) <= (string) = (bool)`: less than or equal to
- `(string) >= (string) = (bool)`: greater than or equal to

## Char

### Equality
Expand Down Expand Up @@ -184,4 +191,3 @@ Tests whether two _(immutable)_ maps contain the same key-objects.
- `(immutable-map) != (immutable-map) = (bool)`: inequality
- `(immutable-map) == (map) = (bool)`: equality
- `(immutable-map) != (map) = (bool)`: inequality

10 changes: 5 additions & 5 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,12 @@ _In Tengo, all values can be either
| `&^` | bitclear (AND NOT) | int |
| `<<` | shift left | int |
| `>>` | shift right | int |
| `<` | less than | int, float, char, time |
| `<=` | less than or equal to | int, float, char, time |
| `>` | greater than | int, float, char, time |
| `>=` | greater than or equal to | int, float, char, time |
| `<` | less than | int, float, char, time, string |
| `<=` | less than or equal to | int, float, char, time, string |
| `>` | greater than | int, float, char, time, string |
| `>=` | greater than or equal to | int, float, char, time, string |

_See [Operators](https://github.com/d5/tengo/blob/d5-patch-1/docs/operators.md)
_See [Operators](https://github.com/d5/tengo/blob/master/docs/operators.md)
for more details._

### Ternary Operators
Expand Down
32 changes: 32 additions & 0 deletions objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,38 @@ func (o *String) BinaryOp(op token.Token, rhs Object) (Object, error) {
}
return &String{Value: o.Value + rhsStr}, nil
}
case token.Less:
switch rhs := rhs.(type) {
case *String:
if o.Value < rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
}
case token.LessEq:
switch rhs := rhs.(type) {
case *String:
if o.Value <= rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
}
case token.Greater:
switch rhs := rhs.(type) {
case *String:
if o.Value > rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
}
case token.GreaterEq:
switch rhs := rhs.(type) {
case *String:
if o.Value >= rhs.Value {
return TrueValue, nil
}
return FalseValue, nil
}
}
return nil, ErrInvalidOperator
}
Expand Down
9 changes: 9 additions & 0 deletions vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3273,6 +3273,15 @@ func TestString(t *testing.T) {
expectRun(t, `out = "Hello" != "Hello"`, nil, false)
expectRun(t, `out = "Hello" != "World"`, nil, true)

expectRun(t, `out = "Hello" > "World"`, nil, false)
expectRun(t, `out = "World" < "Hello"`, nil, false)
expectRun(t, `out = "Hello" < "World"`, nil, true)
expectRun(t, `out = "World" > "Hello"`, nil, true)
expectRun(t, `out = "Hello" >= "World"`, nil, false)
expectRun(t, `out = "Hello" <= "World"`, nil, true)
expectRun(t, `out = "Hello" >= "Hello"`, nil, true)
expectRun(t, `out = "World" <= "World"`, nil, true)

// index operator
str := "abcdef"
strStr := `"abcdef"`
Expand Down

0 comments on commit 366c699

Please sign in to comment.