Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 14 | package com.google.devtools.build.lib.syntax; |
| 15 | |
janakr | 76de1ad | 2018-03-03 11:12:36 -0800 | [diff] [blame^] | 16 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 17 | import java.io.IOException; |
Florian Weikert | 1bf1c97 | 2015-11-09 16:06:49 +0000 | [diff] [blame] | 18 | |
janakr | 76de1ad | 2018-03-03 11:12:36 -0800 | [diff] [blame^] | 19 | /** Syntax node for an if/else expression. */ |
| 20 | @AutoCodec |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 21 | public final class ConditionalExpression extends Expression { |
| 22 | |
| 23 | // Python conditional expressions: $thenCase if $condition else $elseCase |
| 24 | // https://docs.python.org/3.5/reference/expressions.html#conditional-expressions |
| 25 | private final Expression thenCase; |
| 26 | private final Expression condition; |
| 27 | private final Expression elseCase; |
| 28 | |
| 29 | public Expression getThenCase() { return thenCase; } |
| 30 | public Expression getCondition() { return condition; } |
| 31 | public Expression getElseCase() { return elseCase; } |
| 32 | |
| 33 | /** |
| 34 | * Constructor for a conditional expression |
| 35 | */ |
| 36 | public ConditionalExpression( |
| 37 | Expression thenCase, Expression condition, Expression elseCase) { |
| 38 | this.thenCase = thenCase; |
| 39 | this.condition = condition; |
| 40 | this.elseCase = elseCase; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Constructs a string representation of the if expression |
| 45 | */ |
| 46 | @Override |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 47 | public void prettyPrint(Appendable buffer) throws IOException { |
| 48 | thenCase.prettyPrint(buffer); |
| 49 | buffer.append(" if "); |
| 50 | condition.prettyPrint(buffer); |
| 51 | buffer.append(" else "); |
| 52 | elseCase.prettyPrint(buffer); |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 56 | Object doEval(Environment env) throws EvalException, InterruptedException { |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 57 | if (EvalUtils.toBoolean(condition.eval(env))) { |
| 58 | return thenCase.eval(env); |
| 59 | } else { |
| 60 | return elseCase.eval(env); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void accept(SyntaxTreeVisitor visitor) { |
| 66 | visitor.visit(this); |
| 67 | } |
laurentlb | af682d1 | 2017-08-24 20:32:02 +0200 | [diff] [blame] | 68 | |
| 69 | @Override |
| 70 | public Kind kind() { |
| 71 | return Kind.CONDITIONAL; |
| 72 | } |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 73 | } |