()
| 56 | ) |
| 57 | |
| 58 | func init() { |
| 59 | info := &simple.Info{ |
| 60 | Pure: true, |
| 61 | Memo: true, |
| 62 | Fast: true, |
| 63 | Spec: true, |
| 64 | } |
| 65 | RegisterOperator("+", &simple.Scaffold{ |
| 66 | I: info, |
| 67 | T: types.NewType("func(?1, ?1) ?1"), |
| 68 | C: simple.TypeMatch([]string{ |
| 69 | "func(str, str) str", // concatenation |
| 70 | "func(int, int) int", // addition |
| 71 | "func(float, float) float", // floating-point addition |
| 72 | }), |
| 73 | F: func(ctx context.Context, input []types.Value) (types.Value, error) { |
| 74 | if l := len(input); l != 2 { // catch programming bugs |
| 75 | return nil, fmt.Errorf("invalid len %d", l) |
| 76 | } |
| 77 | // Both args should be the same type for this function. |
| 78 | if err := input[0].Type().Cmp(input[1].Type()); err != nil { |
| 79 | // This check is required because of speculation. |
| 80 | // Perhaps a bug in our earlier speculation code? |
| 81 | return nil, errwrap.Wrapf(err, "invalid arg types") |
| 82 | // TODO: speculation error instead? |
| 83 | //return nil, funcs.ErrCantSpeculate |
| 84 | } |
| 85 | |
| 86 | switch k := input[0].Type().Kind; k { |
| 87 | case types.KindStr: |
| 88 | return &types.StrValue{ |
| 89 | V: input[0].Str() + input[1].Str(), |
| 90 | }, nil |
| 91 | |
| 92 | case types.KindInt: |
| 93 | // FIXME: check for overflow? |
| 94 | return &types.IntValue{ |
| 95 | V: input[0].Int() + input[1].Int(), |
| 96 | }, nil |
| 97 | |
| 98 | case types.KindFloat: |
| 99 | return &types.FloatValue{ |
| 100 | V: input[0].Float() + input[1].Float(), |
| 101 | }, nil |
| 102 | |
| 103 | default: |
| 104 | return nil, fmt.Errorf("unsupported kind: %+v", k) |
| 105 | } |
| 106 | }, |
| 107 | }) |
| 108 | |
| 109 | RegisterOperator("-", &simple.Scaffold{ |
| 110 | I: info, |
| 111 | T: types.NewType("func(?1, ?1) ?1"), |
| 112 | C: simple.TypeMatch([]string{ |
| 113 | "func(int, int) int", // subtraction |
| 114 | "func(float, float) float", // floating-point subtraction |
| 115 | }), |
nothing calls this directly
no test coverage detected