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

Added switch expression support, fixed echo to print, added python version info, improved .gitignore #53

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
143 changes: 137 additions & 6 deletions .gitignore
@@ -1,14 +1,145 @@
*.pyc
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# macOS files
.DS_Store

# test data
test/

# auto-generated and other files
parser.out
parsetab.py
phply.egg-info
.eggs
Lib/
Scripts/
pyvenv.cfg
pip-selfcheck.json
man/
*.sw?
.tox
build/
dist/
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -3,6 +3,8 @@
phply is a parser for the PHP programming language written using PLY, a
Lex/YACC-style parser generator toolkit for Python.

Supports only python 2.7 currently.

## Why?

Good question. Because I'm crazy. Because it seemed possible.
Expand Down
38 changes: 37 additions & 1 deletion phply/pythonast.py
Expand Up @@ -88,7 +88,7 @@ def from_phpast(node):
**pos(node))

if isinstance(node, php.Echo):
return py.Call(py.Name('echo', py.Load(**pos(node)),
return py.Call(py.Name('print', py.Load(**pos(node)),
**pos(node)),
list(map(from_phpast, node.nodes)),
[], None, None,
Expand Down Expand Up @@ -321,6 +321,42 @@ def from_phpast(node):
list(map(to_stmt, list(map(from_phpast, deblock(node.node))))),
orelse, **pos(node))



if isinstance(node, php.Switch):
ifexpr = py.Compare(from_phpast(node.expr), [py.Eq(**pos(node))],
[from_phpast(node.nodes[0].expr)],
**pos(node))
def recurseIfs(conds, bodies, current):
assert (len(conds) == len(bodies)), \
'Length of conds %r and bodies %r mismatch' % (len(conds), len(bodies))

if current >= len(conds):
return []
elif current == len(conds)-1:
return bodies[current]
return py.If(conds[current],
bodies[current],
recurseIfs(conds, bodies, current+1),
**pos(node))

elseifs_conditions = [py.Compare(from_phpast(node.expr), [py.Eq(**pos(node))],
[from_phpast(subnode.expr)],
**pos(node))
for subnode in node.nodes[1:]]
elseifs_bodies = [list(map(to_stmt, list(map(from_phpast, subnode.nodes))))
for subnode in node.nodes[1:]]

elseifs = recurseIfs(elseifs_conditions, elseifs_bodies, 0)

return py.If(ifexpr,
list(map(to_stmt, list(map(from_phpast, node.nodes[0].nodes)))),
elseifs,
**pos(node))




if isinstance(node, php.For):
assert node.test is None or len(node.test) == 1, \
'only a single test is supported in for-loops'
Expand Down
2 changes: 1 addition & 1 deletion tools/unparse.py
Expand Up @@ -111,7 +111,7 @@ def _Pass(self, t):
self.fill("pass")

def _Break(self, t):
self.fill("break")
pass

def _Continue(self, t):
self.fill("continue")
Expand Down