Skip to content

starlark: add ** operator, **= augmented assignment, and pow() (#631)#632

Open
nickovs wants to merge 1 commit into
google:masterfrom
nickovs:power-operator
Open

starlark: add ** operator, **= augmented assignment, and pow() (#631)#632
nickovs wants to merge 1 commit into
google:masterfrom
nickovs:power-operator

Conversation

@nickovs
Copy link
Copy Markdown

@nickovs nickovs commented Mar 17, 2026

This PR implements the Python ** operator, **= augmented assignment operator, and pow() builtin function, addressing issue #631.

The implementation follows the design changes laid out in the issue.

The PR includes (hopefully) complete test cases for the new functionality and its Python compatibility, and the documentation has been updated to reflect the changes.

…built-in (google#631)

Add the ** exponentiation operator and pow() built-in function, bringing Starlark closer
to Python's numeric capabilities.

Parser: introduce parseFactor/parsePower productions (modeled on Python's grammar)
to place unary operators between multiplication and exponentiation in precedence,
and to handle right-associativity of **.

Scanner: reorder the token enum so that STARSTAR and STARSTAR_EQ are adjacent to the
PLUS..GTGT and PLUS_EQ..GTGT_EQ groups, allowing the compiler and VM offset arithmetic
to work without special cases.

Runtime: Int**Int (non-negative exp) produces exact Int via big.Int.Exp with a
4096-bit result size cap; negative or float exponents produce Float.
pow(base, exp, mod) performs modular exponentiation for integer arguments.
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Mar 17, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant