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 | |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 16 | import java.io.IOException; |
Florian Weikert | 1bf1c97 | 2015-11-09 16:06:49 +0000 | [diff] [blame] | 17 | |
janakr | 76de1ad | 2018-03-03 11:12:36 -0800 | [diff] [blame] | 18 | /** Syntax node for an if/else expression. */ |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 19 | public final class ConditionalExpression extends Expression { |
| 20 | |
| 21 | // Python conditional expressions: $thenCase if $condition else $elseCase |
| 22 | // https://docs.python.org/3.5/reference/expressions.html#conditional-expressions |
| 23 | private final Expression thenCase; |
| 24 | private final Expression condition; |
| 25 | private final Expression elseCase; |
| 26 | |
| 27 | public Expression getThenCase() { return thenCase; } |
| 28 | public Expression getCondition() { return condition; } |
| 29 | public Expression getElseCase() { return elseCase; } |
| 30 | |
| 31 | /** |
| 32 | * Constructor for a conditional expression |
| 33 | */ |
| 34 | public ConditionalExpression( |
| 35 | Expression thenCase, Expression condition, Expression elseCase) { |
| 36 | this.thenCase = thenCase; |
| 37 | this.condition = condition; |
| 38 | this.elseCase = elseCase; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Constructs a string representation of the if expression |
| 43 | */ |
| 44 | @Override |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 45 | public void prettyPrint(Appendable buffer) throws IOException { |
| 46 | thenCase.prettyPrint(buffer); |
| 47 | buffer.append(" if "); |
| 48 | condition.prettyPrint(buffer); |
| 49 | buffer.append(" else "); |
| 50 | elseCase.prettyPrint(buffer); |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 54 | Object doEval(Environment env) throws EvalException, InterruptedException { |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 55 | if (EvalUtils.toBoolean(condition.eval(env))) { |
| 56 | return thenCase.eval(env); |
| 57 | } else { |
| 58 | return elseCase.eval(env); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void accept(SyntaxTreeVisitor visitor) { |
| 64 | visitor.visit(this); |
| 65 | } |
laurentlb | af682d1 | 2017-08-24 20:32:02 +0200 | [diff] [blame] | 66 | |
| 67 | @Override |
| 68 | public Kind kind() { |
| 69 | return Kind.CONDITIONAL; |
| 70 | } |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 71 | } |