Compile conditional statement and expressions.
--
MOS_MIGRATED_REVID=107385205
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/ConditionalExpression.java b/src/main/java/com/google/devtools/build/lib/syntax/ConditionalExpression.java
index 652baa65..be83c31 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/ConditionalExpression.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/ConditionalExpression.java
@@ -11,9 +11,22 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
-
package com.google.devtools.build.lib.syntax;
+import static com.google.devtools.build.lib.syntax.compiler.ByteCodeUtils.append;
+
+import com.google.devtools.build.lib.syntax.compiler.ByteCodeUtils;
+import com.google.devtools.build.lib.syntax.compiler.DebugInfo;
+import com.google.devtools.build.lib.syntax.compiler.Jump;
+import com.google.devtools.build.lib.syntax.compiler.Jump.PrimitiveComparison;
+import com.google.devtools.build.lib.syntax.compiler.LabelAdder;
+import com.google.devtools.build.lib.syntax.compiler.VariableScope;
+
+import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
+
+import java.util.ArrayList;
+import java.util.List;
+
/**
* Syntax node for an if/else expression.
*/
@@ -67,4 +80,29 @@
thenCase.validate(env);
elseCase.validate(env);
}
+
+ @Override
+ ByteCodeAppender compile(VariableScope scope, DebugInfo debugInfo) throws EvalException {
+ List<ByteCodeAppender> code = new ArrayList<>();
+ LabelAdder afterLabel = new LabelAdder();
+ LabelAdder elseLabel = new LabelAdder();
+ // compile condition and convert to boolean
+ code.add(condition.compile(scope, debugInfo));
+ append(
+ code,
+ EvalUtils.toBoolean,
+ // jump to else block if false
+ Jump.ifIntOperandToZero(PrimitiveComparison.EQUAL).to(elseLabel));
+ // otherwise evaluate the expression for "then" and jump to end
+ code.add(thenCase.compile(scope, debugInfo));
+ append(
+ code,
+ Jump.to(afterLabel),
+ // add label for "else" and evaluate the expression
+ elseLabel);
+ code.add(elseCase.compile(scope, debugInfo));
+ append(code, afterLabel);
+
+ return ByteCodeUtils.compoundAppender(code);
+ }
}