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

Use _expr in array definitions #3131

Merged
merged 1 commit into from
Oct 28, 2022

Conversation

benber86
Copy link
Contributor

What I did

Modified the lark grammar to support expressions when specifying array length on definition.
For instance trying to parse the following valid Vyper code with lark:

a: public((uint256[4 & 4]))

or

B: constant(uint256) = 2
c: DynArray[uint256, B**2]

@external
def __init__(
    initial_prices: uint256[3+B-2%1]
):
    pass

Will currently raise an UnexpectedToken exception

How I did it

Replaced (DEC_NUMBER | NAME) with _expr in grammar.lark for array_def and dyn_array_def

How to verify it

Parsing the following code:

a: public((uint256[4 & 4]))
B: constant(uint256) = 2
c: DynArray[uint256, B**2]

@external
def __init__(
    initial_prices: uint256[3+B-2%1]
):
    pass

Properly returns the following tree:

Tree(Token('RULE', 'module'), [Tree(Token('RULE', 'variable_def'), [Tree(Token('RULE', 'variable_with_getter'), [Token('NAME', 'a'), Tree(Token('RULE', 'type'), [Tree(Token('RULE', 'tuple_def'), [Tree(Token('RULE', 'array_def'), [Token('NAME', 'uint256'), Tree('bitand', [Token('DEC_NUMBER', '4'), Token('DEC_NUMBER', '4')])])])])])]), Tree(Token('RULE', 'constant_def'), [Tree(Token('RULE', 'constant_private'), [Token('NAME', 'B'), Tree(Token('RULE', 'constant'), [Tree(Token('RULE', 'type'), [Token('NAME', 'uint256')])])]), Token('DEC_NUMBER', '2')]), Tree(Token('RULE', 'variable_def'), [Tree(Token('RULE', 'variable'), [Token('NAME', 'c'), Tree(Token('RULE', 'type'), [Tree(Token('RULE', 'dyn_array_def'), [Token('NAME', 'uint256'), Tree('pow', [Tree('get_var', [Token('NAME', 'B')]), Token('DEC_NUMBER', '2')])])])])]), Tree(Token('RULE', 'function_def'), [Tree(Token('RULE', 'decorators'), [Tree(Token('RULE', 'decorator'), [Token('NAME', 'external')])]), Tree(Token('RULE', 'function_sig'), [Token('NAME', '__init__'), Tree(Token('RULE', 'parameters'), [Tree(Token('RULE', 'parameter'), [Token('NAME', 'initial_prices'), Tree(Token('RULE', 'type'), [Tree(Token('RULE', 'array_def'), [Token('NAME', 'uint256'), Tree('sub', [Tree('add', [Token('DEC_NUMBER', '3'), Tree('get_var', [Token('NAME', 'B')])]), Tree('mod', [Token('DEC_NUMBER', '2'), Token('DEC_NUMBER', '1')])])])])])])]), Tree(Token('RULE', 'body'), [Tree(Token('RULE', 'pass_stmt'), [])])])])

Commit message

Use _expr in array definitions in lark grammar

Description for the changelog

Cute Animal Picture

image

@codecov-commenter
Copy link

codecov-commenter commented Oct 27, 2022

Codecov Report

Merging #3131 (ea5c453) into master (676dc30) will decrease coverage by 0.58%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #3131      +/-   ##
==========================================
- Coverage   88.49%   87.91%   -0.59%     
==========================================
  Files          95       95              
  Lines       10760    10756       -4     
  Branches     2266     2201      -65     
==========================================
- Hits         9522     9456      -66     
- Misses        796      856      +60     
- Partials      442      444       +2     
Impacted Files Coverage Δ
vyper/codegen/arithmetic.py 70.96% <0.00%> (-13.65%) ⬇️
vyper/semantics/types/value/numeric.py 80.45% <0.00%> (-4.60%) ⬇️
vyper/ir/s_expressions.py 91.17% <0.00%> (-2.95%) ⬇️
vyper/builtin_functions/functions.py 87.84% <0.00%> (-2.70%) ⬇️
vyper/ast/nodes.py 92.72% <0.00%> (-0.50%) ⬇️
vyper/codegen/abi_encoder.py 88.46% <0.00%> (-0.11%) ⬇️
vyper/semantics/validation/module.py 85.41% <0.00%> (-0.08%) ⬇️
vyper/codegen/core.py 85.15% <0.00%> (-0.03%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@charles-cooper charles-cooper merged commit b13595f into vyperlang:master Oct 28, 2022
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.

4 participants