Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unary plus operator, +int #8890

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,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
Original file line number Diff line number Diff line change
Expand Up @@ -847,13 +847,13 @@ public void testParserRecovery() throws Exception {
"",
"def bar():",
" a = [3, 4]",
" b = 2 + + 5", // parse error
" 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 +872,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