add string comparison support (#294)

* 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
This commit is contained in:
Ozan Hacıbekiroğlu 2020-05-25 20:53:52 +03:00 committed by GitHub
parent d1dd01499f
commit 366c69902f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 6 deletions

View file

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

View file

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

View file

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

View file

@ -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"`