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

PEP572 #63

Merged
merged 3 commits into from
Oct 29, 2018
Merged
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
40 changes: 23 additions & 17 deletions snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,24 @@ def dis_code(code: types.CodeType, f):
dis_code(each, f)


def case(code, ctx, debug=False):
def case(code, ctx, debug=False, cpython_compat=True):
stmt = parse(code).result
code_obj = py_compile(stmt, is_entrypoint=False)

if debug:
code_obj2 = compile(code, "", "exec")
with open('out_yapypy_bc.log', 'w') as yapypy_bc, open(
'out_yapypy_info.log', 'w') as yapypy_info, open(
'out_cpy_bc.log', 'w') as cpy_bc, open(
'out_cpy_info.log', 'w') as cpy_info:
with open('out_yapypy_bc.log', 'w') as yapypy_bc, open('out_yapypy_info.log',
'w') as yapypy_info:

dis_code(code_obj, yapypy_bc)
show_code(code_obj, yapypy_info)
dis_code(code_obj2, cpy_bc)
show_code(code_obj2, cpy_info)

print('python:')
exec(Bytecode.from_code(code_obj2).to_code(), ctx or {})
if cpython_compat:
code_obj2 = compile(code, "", "exec")
with open('out_cpy_bc.log', 'w') as cpy_bc, open('out_cpy_info.log',
'w') as cpy_info:
dis_code(code_obj2, cpy_bc)
show_code(code_obj2, cpy_info)
print('python:')
exec(Bytecode.from_code(code_obj2).to_code(), ctx or {})
print('yapypy')
exec(Bytecode.from_code(code_obj).to_code(), ctx or {})

Expand All @@ -101,14 +101,20 @@ def case(code, ctx, debug=False):

case(
"""
async def f():
return [await a for a in b]

async def g():
return [await a async for a in b]
(a := 1)
a := 1
print(a)
def maybe_odd(i):
if i % 2:
return i
if a := maybe_odd(2):
print(a)
if a := maybe_odd(3):
print(a)
""",
ctx,
debug=True)
debug=True,
cpython_compat=False)

# case(
# """
Expand Down
39 changes: 39 additions & 0 deletions yapypy/extended_python/emit_impl/simple_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,42 @@ def py_emit(node: ast.Ellipsis, ctx: Context):
>>> assert ... is Ellipsis
"""
ctx.bc.append(LOAD_CONST(..., lineno=node.lineno))


@py_emit.case(ex_ast.AssignExpr)
def py_emit(node: ex_ast.AssignExpr, ctx: Context):
"""
https://www.python.org/dev/peps/pep-0572/
Note that some features are not implemented yet:

1. auto nonlocal detection
```
total = 0
partial_sums = [total := total + v for v in values]
```
* It could be kind of dangerous for the breakage of context.
2. some of exceptional cases
```
a := 1 # should be invalid according to PEP572
```

title: assign expr
test:
>>> (a := 1)
>>> assert a == 1
>>> b = False
>>> def maybe_odd(i):
>>> if i % 2:
>>> return i
>>> if a := maybe_odd(2):
>>> b = True
>>> assert not b
>>> if a := maybe_odd(3):
>>> b = True
>>> assert b
"""
target = node.target
value = node.value
py_emit(value, ctx)
py_emit(target, ctx)
ctx.load_name(target.id)
22 changes: 22 additions & 0 deletions yapypy/extended_python/extended_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,25 @@ def __init__(self, value, lineno=None, col_offset=None):
self.value = value
self.lineno = lineno
self.col_offset = col_offset


class AssignExpr(ast.Assign):

def __init__(self,
target: ast.expr,
value: ast.expr,
lineno: int = None,
col_offset: int = None):
super().__init__()
self.value = value
self.targets = [target]
self.lineno = lineno
self.col_offset = col_offset

@property
def target(self):
return self.targets[0]

@target.setter
def target(self, value):
self.targets[0] = value
21 changes: 11 additions & 10 deletions yapypy/extended_python/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,18 @@
atom_expr ::= [a='await'] atom=atom trailers=trailer* -> atom_expr_rewrite(a, atom, trailers)

atom ::= (is_gen ='(' [yield_expr=yield_expr|comp=testlist_comp] ')' |
is_list='[' [comp=testlist_comp] ']' |
head='{' [dict=dictorsetmaker] is_dict='}' |
name=NAME |
number=NUMBER |
strs=STRING+ |
ellipsis='...' |
namedc='None' |
namedc='True' |
is_list='[' [comp=testlist_comp] ']' |
head='{' [dict=dictorsetmaker] is_dict='}' |
name=NAME [token= ':='value=test] |
number=NUMBER |
strs=STRING+ |
ellipsis='...' |
namedc='None' |
namedc='True' |
namedc='False')
-> atom_rewrite(loc, name, number, strs, namedc, ellipsis, dict, is_dict, is_gen, is_list, comp, yield_expr)

-> atom_rewrite(loc, name, token, value, number, strs, namedc, ellipsis,
dict, is_dict, is_gen, is_list, comp, yield_expr)

testlist_comp ::= values<<(test|star_expr) ( comp=comp_for | (',' values<<(test|star_expr))* [force_tuple=','] )
->
def app(is_tuple=None, is_list=None):
Expand Down
9 changes: 6 additions & 3 deletions yapypy/extended_python/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,13 @@ def check_call_args(loc, seq: t.List[ast.expr]):
return seq


def atom_rewrite(loc, name, number, strs, namedc, ellipsis, dict, is_dict, is_gen,
is_list, comp, yield_expr):
def atom_rewrite(loc, name, token, value, number, strs, namedc, ellipsis, dict, is_dict,
is_gen, is_list, comp, yield_expr):
if name:
return ast.Name(name.value, ast.Load(), **loc @ name)
if not token:
return ast.Name(name.value, ast.Load(), **loc @ name)
return ex_ast.AssignExpr(
ast.Name(name.value, ast.Store(), **loc @ name), value=value, **loc @ token)

if number:
return ast.Num(eval(number.value), **loc @ number)
Expand Down
11 changes: 9 additions & 2 deletions yapypy/extended_python/parser.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import tokenize
from rbnf.core.Tokenizer import Tokenizer
from rbnf.core.CachingPool import ConstStrPool
from rbnf.core.State import State
from rbnf.easy import Language, build_parser, build_language, ze
from keyword import kwlist
from yapypy.extended_python.grammar import RBNF
from yapypy.extended_python import helper, extended_ast

import sys
import ast
import typing as t
import io

if sys.version_info > (3, 8): # avoid breakage at dev
import tokenize
elif sys.version_info >= (3, 7):
import yapypy.utils.yapypy_tokenize37 as tokenize
elif sys.version_info >= (3, 6):
import yapypy.utils.yapypy_tokenize36 as tokenize

cast = ConstStrPool.cast_to_const
kwlist = {*kwlist, 'async', 'await'}

Expand Down
11 changes: 6 additions & 5 deletions yapypy/extended_python/py_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ def py_compile(node, filename='<unknown>', is_entrypoint=False):
except SyntaxError as exc:
exc.filename = filename
raise exc
try:
return ctx.bc.to_code()
except Exception as e:
dump_bytecode(ctx.bc)
raise e
return ctx.bc.to_code()
# try:
# return ctx.bc.to_code()
# except Exception as e:
# dump_bytecode(ctx.bc)
# raise e
else:
tag = to_tagged_ast(node)
return py_compile(tag, filename, is_entrypoint=is_entrypoint)
1 change: 1 addition & 0 deletions yapypy/extended_python/symbol_analyzer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ast
import yapypy.extended_python.extended_ast as ex_ast
import typing
from yapypy.utils.namedlist import INamedList, as_namedlist, trait
from typing import NamedTuple, List, Optional, Union
Expand Down
Loading