Skip to content

Commit

Permalink
Add unary plus operator, +int
Browse files Browse the repository at this point in the history
Implements: bazelbuild/starlark#20 (comment)

Closes #8890.

PiperOrigin-RevId: 258388687
  • Loading branch information
Quarz0 authored and copybara-github committed Jul 16, 2019
1 parent 1c3e9ad commit 3c00063
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,13 @@ private Expression parsePrimary() {
UnaryOperatorExpression minus = new UnaryOperatorExpression(TokenKind.MINUS, expr);
return setLocation(minus, start, expr);
}
case PLUS:
{
nextToken();
Expression expr = parsePrimaryWithSuffix();
UnaryOperatorExpression plus = new UnaryOperatorExpression(TokenKind.PLUS, expr);
return setLocation(plus, start, expr);
}
default:
{
syntaxError("expected expression");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/** A UnaryOperatorExpression represents a unary operator expression, 'op x'. */
public final class UnaryOperatorExpression extends Expression {

private final TokenKind op; // NOT or MINUS
private final TokenKind op; // NOT, MINUS or PLUS
private final Expression x;

public UnaryOperatorExpression(TokenKind op, Expression x) {
Expand Down Expand Up @@ -75,6 +75,14 @@ private static Object evaluate(TokenKind op, Object value, Location loc)
// Fails for -MIN_INT.
throw new EvalException(loc, e.getMessage());
}
case PLUS:
if (!(value instanceof Integer)) {
throw new EvalException(
loc,
String.format(
"unsupported operand type for +: '%s'", EvalUtils.getDataTypeName(value)));
}
return value;

default:
throw new AssertionError("Unsupported unary operator: " + op);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void testWithSyntaxErrorsDoesNotPrintDollarError() throws Exception {
" srcs = libs,",
" includes = [ abi + opt_level + '/include' ])");
assertThat(buildFile.containsErrors()).isTrue();
assertContainsError("syntax error at '+': expected expression");
assertContainsError("syntax error at '*': expected expression");
assertThat(buildFile.exec(env, getEventHandler())).isFalse();
MoreAsserts.assertDoesNotContainEvent(getEventCollector(), "$error$");
// This message should not be printed anymore.
Expand Down
29 changes: 15 additions & 14 deletions src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -838,22 +838,23 @@ public void testForForListComprehension() throws Exception {
@Test
public void testParserRecovery() throws Exception {
setFailFast(false);
List<Statement> statements = parseFileForSkylark(
"def foo():",
" a = 2 for 4", // parse error
" b = [3, 4]",
"",
"d = 4 ada", // parse error
"",
"def bar():",
" a = [3, 4]",
" b = 2 + + 5", // parse error
"");
List<Statement> statements =
parseFileForSkylark(
"def foo():",
" a = 2 for 4", // parse error
" b = [3, 4]",
"",
"d = 4 ada", // parse error
"",
"def bar():",
" a = [3, 4]",
" b = 2 * * 5", // parse error
"");

assertThat(getEventCollector()).hasSize(3);
assertContainsError("syntax error at 'for': expected newline");
assertContainsError("syntax error at 'ada': expected newline");
assertContainsError("syntax error at '+': expected expression");
assertContainsError("syntax error at '*': expected expression");
assertThat(statements).hasSize(3);
}

Expand All @@ -872,8 +873,8 @@ public void testParserDoesNotContainErrorsIfSuccess() throws Exception {
@Test
public void testParserContainsErrors() throws Exception {
setFailFast(false);
parseFile("+");
assertContainsError("syntax error at '+'");
parseFile("*");
assertContainsError("syntax error at '*'");
}

@Test
Expand Down
9 changes: 9 additions & 0 deletions src/test/starlark/testdata/int.sky
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def compound():

compound()


# unary operators

assert_eq(+4, 4)
assert_eq(-4, -4)
assert_eq(++4, 4)
assert_eq(+-4, -4)
assert_eq(-+-4, 4)

---
1 // 0 ### integer division by zero
---
Expand Down

0 comments on commit 3c00063

Please sign in to comment.