MCPcopy Index your code
hub / github.com/bazelbuild/bazel / binaryOp

Method binaryOp

src/main/java/net/starlark/java/eval/EvalUtils.java:78–418  ·  view source on GitHub ↗

Evaluates an eager binary operation, x op y. (Excludes AND and OR.)

(TokenKind op, Object x, Object y, StarlarkThread starlarkThread)

Source from the content-addressed store, hash-verified

76
77 /** Evaluates an eager binary operation, {@code x op y}. (Excludes AND and OR.) */
78 static Object binaryOp(TokenKind op, Object x, Object y, StarlarkThread starlarkThread)
79 throws EvalException {
80 StarlarkSemantics semantics = starlarkThread.getSemantics();
81 Mutability mu = starlarkThread.mutability();
82 switch (op) {
83 case PLUS:
84 if (x instanceof StarlarkInt) {
85 if (y instanceof StarlarkInt) {
86 // int + int
87 return StarlarkInt.add((StarlarkInt) x, (StarlarkInt) y);
88 } else if (y instanceof StarlarkFloat) {
89 // int + float
90 double z = ((StarlarkInt) x).toFiniteDouble() + ((StarlarkFloat) y).toDouble();
91 return StarlarkFloat.of(z);
92 }
93
94 } else if (x instanceof String) {
95 if (y instanceof String) {
96 // string + string
97 return (String) x + (String) y;
98 }
99
100 } else if (x instanceof Tuple) {
101 if (y instanceof Tuple) {
102 // tuple + tuple
103 return Tuple.concat((Tuple) x, (Tuple) y);
104 }
105
106 } else if (x instanceof StarlarkList) {
107 if (y instanceof StarlarkList) {
108 // list + list
109 return StarlarkList.concat((StarlarkList<?>) x, (StarlarkList<?>) y, mu);
110 }
111
112 } else if (x instanceof StarlarkFloat) {
113 double xf = ((StarlarkFloat) x).toDouble();
114 if (y instanceof StarlarkFloat) {
115 // float + float
116 double z = xf + ((StarlarkFloat) y).toDouble();
117 return StarlarkFloat.of(z);
118 } else if (y instanceof StarlarkInt) {
119 // float + int
120 double z = xf + ((StarlarkInt) y).toFiniteDouble();
121 return StarlarkFloat.of(z);
122 }
123 }
124 break;
125
126 case PIPE:
127 if (x instanceof StarlarkInt) {
128 if (y instanceof StarlarkInt) {
129 // int | int
130 return StarlarkInt.or((StarlarkInt) x, (StarlarkInt) y);
131 }
132 } else if (x instanceof Map) {
133 if (y instanceof Map) {
134 // map | map (usually dicts)
135 return Dict.builder().putAll((Map<?, ?>) x).putAll((Map<?, ?>) y).build(mu);

Callers 2

inplaceBinaryOpMethod · 0.95
evalBinaryOperatorMethod · 0.95

Calls 15

addMethod · 0.95
ofMethod · 0.95
concatMethod · 0.95
concatMethod · 0.95
orMethod · 0.95
builderMethod · 0.95
getBoolMethod · 0.95
emptyMethod · 0.95
ofMethod · 0.95
andMethod · 0.95
checkedCopyOfMethod · 0.95
xorMethod · 0.95

Tested by

no test coverage detected