forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turn unary minus into a proper AST node
This unifies unary minus with NotExpression, and moves the minus logic from a fake builtin function into the interpreter proper. RELNOTES: None PiperOrigin-RevId: 160266266
- Loading branch information
Showing
11 changed files
with
163 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
src/main/java/com/google/devtools/build/lib/syntax/NotExpression.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/google/devtools/build/lib/syntax/UnaryOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2017 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// 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; | ||
|
||
/** Unary operators. */ | ||
public enum UnaryOperator { | ||
|
||
// Include trailing whitespace in name for non-symbolic operators (for pretty printing). | ||
NOT("not "), | ||
MINUS("-"); | ||
|
||
private final String name; | ||
|
||
UnaryOperator(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/com/google/devtools/build/lib/syntax/UnaryOperatorExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2017 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// 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 com.google.devtools.build.lib.events.Location; | ||
|
||
/** Syntax node for a unary operator expression. */ | ||
public final class UnaryOperatorExpression extends Expression { | ||
|
||
private final UnaryOperator operator; | ||
|
||
private final Expression operand; | ||
|
||
public UnaryOperatorExpression(UnaryOperator operator, Expression operand) { | ||
this.operator = operator; | ||
this.operand = operand; | ||
} | ||
|
||
public UnaryOperator getOperator() { | ||
return operator; | ||
} | ||
|
||
public Expression getOperand() { | ||
return operand; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
// All current and planned unary operators happen to be prefix operators. | ||
// Non-symbolic operators have trailing whitespace built into their name. | ||
return operator.toString() + operand; | ||
} | ||
|
||
private static Object evaluate( | ||
UnaryOperator operator, | ||
Object value, | ||
Environment env, | ||
Location loc) | ||
throws EvalException, InterruptedException { | ||
switch (operator) { | ||
case NOT: | ||
return !EvalUtils.toBoolean(value); | ||
|
||
case MINUS: | ||
if (!(value instanceof Integer)) { | ||
throw new EvalException( | ||
loc, | ||
String.format("unsupported operand type for -: '%s'", | ||
EvalUtils.getDataTypeName(value))); | ||
} | ||
return -((Integer) value); | ||
|
||
default: | ||
throw new AssertionError("Unsupported unary operator: " + operator); | ||
} | ||
} | ||
|
||
@Override | ||
Object doEval(Environment env) throws EvalException, InterruptedException { | ||
return evaluate(operator, operand.eval(env), env, getLocation()); | ||
} | ||
|
||
@Override | ||
public void accept(SyntaxTreeVisitor visitor) { | ||
visitor.visit(this); | ||
} | ||
|
||
@Override | ||
void validate(ValidationEnvironment env) throws EvalException { | ||
operand.validate(env); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.