BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.
(op token.Token, rhs Object)
| 1327 | // BinaryOp returns another object that is the result of a given binary |
| 1328 | // operator and a right-hand side object. |
| 1329 | func (o *String) BinaryOp(op token.Token, rhs Object) (Object, error) { |
| 1330 | switch op { |
| 1331 | case token.Add: |
| 1332 | switch rhs := rhs.(type) { |
| 1333 | case *String: |
| 1334 | if len(o.Value)+len(rhs.Value) > MaxStringLen { |
| 1335 | return nil, ErrStringLimit |
| 1336 | } |
| 1337 | return &String{Value: o.Value + rhs.Value}, nil |
| 1338 | default: |
| 1339 | rhsStr := rhs.String() |
| 1340 | if len(o.Value)+len(rhsStr) > MaxStringLen { |
| 1341 | return nil, ErrStringLimit |
| 1342 | } |
| 1343 | return &String{Value: o.Value + rhsStr}, nil |
| 1344 | } |
| 1345 | case token.Less: |
| 1346 | switch rhs := rhs.(type) { |
| 1347 | case *String: |
| 1348 | if o.Value < rhs.Value { |
| 1349 | return TrueValue, nil |
| 1350 | } |
| 1351 | return FalseValue, nil |
| 1352 | } |
| 1353 | case token.LessEq: |
| 1354 | switch rhs := rhs.(type) { |
| 1355 | case *String: |
| 1356 | if o.Value <= rhs.Value { |
| 1357 | return TrueValue, nil |
| 1358 | } |
| 1359 | return FalseValue, nil |
| 1360 | } |
| 1361 | case token.Greater: |
| 1362 | switch rhs := rhs.(type) { |
| 1363 | case *String: |
| 1364 | if o.Value > rhs.Value { |
| 1365 | return TrueValue, nil |
| 1366 | } |
| 1367 | return FalseValue, nil |
| 1368 | } |
| 1369 | case token.GreaterEq: |
| 1370 | switch rhs := rhs.(type) { |
| 1371 | case *String: |
| 1372 | if o.Value >= rhs.Value { |
| 1373 | return TrueValue, nil |
| 1374 | } |
| 1375 | return FalseValue, nil |
| 1376 | } |
| 1377 | } |
| 1378 | return nil, ErrInvalidOperator |
| 1379 | } |
| 1380 | |
| 1381 | // IsFalsy returns true if the value of the type is falsy. |
| 1382 | func (o *String) IsFalsy() bool { |