├── .gitignore ├── .travis.yml ├── AUTHORS.rst ├── CONTRIBUTING.rst ├── HISTORY.rst ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.rst ├── docs ├── Makefile ├── authors.rst ├── conf.py ├── contributing.rst ├── history.rst ├── index.rst ├── installation.rst ├── make.bat ├── readme.rst └── usage.rst ├── nosy.cfg ├── py65c ├── __init__.py ├── astpp.py ├── compiler.py └── symbols.py ├── requirements.txt ├── setup.cfg ├── setup.py ├── tests ├── __init__.py ├── files │ ├── addsub.py │ ├── bool.py │ ├── fib.py │ ├── func.py │ ├── gt.py │ ├── gte.py │ ├── if.py │ ├── list.py │ ├── lte.py │ ├── mul.py │ ├── recur.py │ └── while.py └── test_py65c.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | 21 | # Installer logs 22 | pip-log.txt 23 | 24 | # Unit test / coverage reports 25 | .coverage 26 | .tox 27 | nosetests.xml 28 | htmlcov 29 | 30 | # Translations 31 | *.mo 32 | 33 | # Mr Developer 34 | .mr.developer.cfg 35 | .project 36 | .pydevproject 37 | 38 | # Complexity 39 | output/*.html 40 | output/*/index.html 41 | 42 | # Sphinx 43 | docs/_build -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Config file for automatic testing at travis-ci.org 2 | 3 | language: python 4 | 5 | python: 6 | - "2.7" 7 | - "pypy" 8 | 9 | # command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors 10 | install: pip install -r requirements.txt 11 | 12 | # command to run tests, e.g. python setup.py test 13 | script: python setup.py test -------------------------------------------------------------------------------- /AUTHORS.rst: -------------------------------------------------------------------------------- 1 | ======= 2 | Credits 3 | ======= 4 | 5 | Development Lead 6 | ---------------- 7 | 8 | * Jeremy Neiman 9 | 10 | Contributors 11 | ------------ 12 | 13 | None yet. Why not be the first? -------------------------------------------------------------------------------- /CONTRIBUTING.rst: -------------------------------------------------------------------------------- 1 | ============ 2 | Contributing 3 | ============ 4 | 5 | Contributions are welcome, and they are greatly appreciated! Every 6 | little bit helps, and credit will always be given. 7 | 8 | You can contribute in many ways: 9 | 10 | Types of Contributions 11 | ---------------------- 12 | 13 | Report Bugs 14 | ~~~~~~~~~~~ 15 | 16 | Report bugs at https://github.com/docmarionum1/py65c/issues. 17 | 18 | If you are reporting a bug, please include: 19 | 20 | * Your operating system name and version. 21 | * Any details about your local setup that might be helpful in troubleshooting. 22 | * Detailed steps to reproduce the bug. 23 | 24 | Fix Bugs 25 | ~~~~~~~~ 26 | 27 | Look through the GitHub issues for bugs. Anything tagged with "bug" 28 | is open to whoever wants to implement it. 29 | 30 | Implement Features 31 | ~~~~~~~~~~~~~~~~~~ 32 | 33 | Look through the GitHub issues for features. Anything tagged with "feature" 34 | is open to whoever wants to implement it. 35 | 36 | Write Documentation 37 | ~~~~~~~~~~~~~~~~~~~ 38 | 39 | Python to 6502 Compiler could always use more documentation, whether as part of the 40 | official Python to 6502 Compiler docs, in docstrings, or even on the web in blog posts, 41 | articles, and such. 42 | 43 | Submit Feedback 44 | ~~~~~~~~~~~~~~~ 45 | 46 | The best way to send feedback is to file an issue at https://github.com/docmarionum1/py65c/issues. 47 | 48 | If you are proposing a feature: 49 | 50 | * Explain in detail how it would work. 51 | * Keep the scope as narrow as possible, to make it easier to implement. 52 | * Remember that this is a volunteer-driven project, and that contributions 53 | are welcome :) 54 | 55 | Get Started! 56 | ------------ 57 | 58 | Ready to contribute? Here's how to set up `py65c` for local development. 59 | 60 | 1. Fork the `py65c` repo on GitHub. 61 | 2. Clone your fork locally:: 62 | 63 | $ git clone git@github.com:your_name_here/py65c.git 64 | 65 | 3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:: 66 | 67 | $ mkvirtualenv py65c 68 | $ cd py65c/ 69 | $ python setup.py develop 70 | 71 | 4. Create a branch for local development:: 72 | 73 | $ git checkout -b name-of-your-bugfix-or-feature 74 | 75 | Now you can make your changes locally. 76 | 77 | 5. When you're done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:: 78 | 79 | $ flake8 py65c tests 80 | $ python setup.py test 81 | $ tox 82 | 83 | To get flake8 and tox, just pip install them into your virtualenv. 84 | 85 | 6. Commit your changes and push your branch to GitHub:: 86 | 87 | $ git add . 88 | $ git commit -m "Your detailed description of your changes." 89 | $ git push origin name-of-your-bugfix-or-feature 90 | 91 | 7. Submit a pull request through the GitHub website. 92 | 93 | Pull Request Guidelines 94 | ----------------------- 95 | 96 | Before you submit a pull request, check that it meets these guidelines: 97 | 98 | 1. The pull request should include tests. 99 | 2. If the pull request adds functionality, the docs should be updated. Put 100 | your new functionality into a function with a docstring, and add the 101 | feature to the list in README.rst. 102 | 3. The pull request should work for Python 2.6, 2.7, and 3.3, and for PyPy. Check 103 | https://travis-ci.org/docmarionum1/py65c/pull_requests 104 | and make sure that the tests pass for all supported Python versions. 105 | 106 | Tips 107 | ---- 108 | 109 | To run a subset of tests:: 110 | 111 | $ python -m unittest tests.test_py65c -------------------------------------------------------------------------------- /HISTORY.rst: -------------------------------------------------------------------------------- 1 | .. :changelog: 2 | 3 | History 4 | ------- 5 | 6 | 0.0 (2014-02-27) 7 | ++++++++++++++++++ 8 | 9 | * First release on PyPI. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include AUTHORS.rst 2 | include CONTRIBUTING.rst 3 | include HISTORY.rst 4 | include LICENSE 5 | include README.rst -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean-pyc clean-build docs clean 2 | 3 | help: 4 | @echo "clean-build - remove build artifacts" 5 | @echo "clean-pyc - remove Python file artifacts" 6 | @echo "lint - check style with flake8" 7 | @echo "test - run tests quickly with the default Python" 8 | @echo "test-all - run tests on every Python version with tox" 9 | @echo "coverage - check code coverage quickly with the default Python" 10 | @echo "docs - generate Sphinx HTML documentation, including API docs" 11 | @echo "release - package and upload a release" 12 | @echo "sdist - package" 13 | 14 | clean: clean-build clean-pyc 15 | rm -fr htmlcov/ 16 | 17 | clean-build: 18 | rm -fr build/ 19 | rm -fr dist/ 20 | rm -fr *.egg-info 21 | 22 | clean-pyc: 23 | find . -name '*.pyc' -exec rm -f {} + 24 | find . -name '*.pyo' -exec rm -f {} + 25 | find . -name '*~' -exec rm -f {} + 26 | 27 | lint: 28 | flake8 py65c tests 29 | 30 | test: 31 | python setup.py test 32 | 33 | test-all: 34 | tox 35 | 36 | coverage: 37 | coverage run --source py65c setup.py test 38 | coverage report -m 39 | coverage html 40 | open htmlcov/index.html 41 | 42 | docs: 43 | rm -f docs/py65c.rst 44 | rm -f docs/modules.rst 45 | sphinx-apidoc -o docs/ py65c 46 | $(MAKE) -C docs clean 47 | $(MAKE) -C docs html 48 | open docs/_build/html/index.html 49 | 50 | release: clean 51 | python setup.py sdist upload 52 | python setup.py bdist_wheel upload 53 | 54 | dist: clean 55 | python setup.py sdist 56 | python setup.py bdist_wheel 57 | ls -l dist -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | =============================== 2 | Python to 6502 Compiler 3 | =============================== 4 | 5 | .. image:: https://badge.fury.io/py/py65c.png 6 | :target: http://badge.fury.io/py/py65c 7 | 8 | .. image:: https://travis-ci.org/docmarionum1/py65c.png?branch=master 9 | :target: https://travis-ci.org/docmarionum1/py65c 10 | 11 | .. image:: https://pypip.in/d/py65c/badge.png 12 | :target: https://crate.io/packages/py65c?version=latest 13 | 14 | 15 | A Python to 6502 Assembly Compiler 16 | 17 | * Free software: BSD license 18 | * Documentation: http://py65c.rtfd.org. 19 | 20 | Features 21 | -------- 22 | 23 | * TODO -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " devhelp to make HTML files and a Devhelp project" 34 | @echo " epub to make an epub" 35 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 36 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 37 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 38 | @echo " text to make text files" 39 | @echo " man to make manual pages" 40 | @echo " texinfo to make Texinfo files" 41 | @echo " info to make Texinfo files and run them through makeinfo" 42 | @echo " gettext to make PO message catalogs" 43 | @echo " changes to make an overview of all changed/added/deprecated items" 44 | @echo " xml to make Docutils-native XML files" 45 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 46 | @echo " linkcheck to check all external links for integrity" 47 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 48 | 49 | clean: 50 | rm -rf $(BUILDDIR)/* 51 | 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | dirhtml: 58 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 59 | @echo 60 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 61 | 62 | singlehtml: 63 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 64 | @echo 65 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 66 | 67 | pickle: 68 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 69 | @echo 70 | @echo "Build finished; now you can process the pickle files." 71 | 72 | json: 73 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 74 | @echo 75 | @echo "Build finished; now you can process the JSON files." 76 | 77 | htmlhelp: 78 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 79 | @echo 80 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 81 | ".hhp project file in $(BUILDDIR)/htmlhelp." 82 | 83 | qthelp: 84 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 85 | @echo 86 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 87 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 88 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/complexity.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/complexity.qhc" 91 | 92 | devhelp: 93 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 94 | @echo 95 | @echo "Build finished." 96 | @echo "To view the help file:" 97 | @echo "# mkdir -p $$HOME/.local/share/devhelp/complexity" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/complexity" 99 | @echo "# devhelp" 100 | 101 | epub: 102 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 103 | @echo 104 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 105 | 106 | latex: 107 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 108 | @echo 109 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 110 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 111 | "(use \`make latexpdf' here to do that automatically)." 112 | 113 | latexpdf: 114 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 115 | @echo "Running LaTeX files through pdflatex..." 116 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 117 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 118 | 119 | latexpdfja: 120 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 121 | @echo "Running LaTeX files through platex and dvipdfmx..." 122 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 123 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 124 | 125 | text: 126 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 127 | @echo 128 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 129 | 130 | man: 131 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 132 | @echo 133 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 134 | 135 | texinfo: 136 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 137 | @echo 138 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 139 | @echo "Run \`make' in that directory to run these through makeinfo" \ 140 | "(use \`make info' here to do that automatically)." 141 | 142 | info: 143 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 144 | @echo "Running Texinfo files through makeinfo..." 145 | make -C $(BUILDDIR)/texinfo info 146 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 147 | 148 | gettext: 149 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 150 | @echo 151 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 152 | 153 | changes: 154 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 155 | @echo 156 | @echo "The overview file is in $(BUILDDIR)/changes." 157 | 158 | linkcheck: 159 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 160 | @echo 161 | @echo "Link check complete; look for any errors in the above output " \ 162 | "or in $(BUILDDIR)/linkcheck/output.txt." 163 | 164 | doctest: 165 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 166 | @echo "Testing of doctests in the sources finished, look at the " \ 167 | "results in $(BUILDDIR)/doctest/output.txt." 168 | 169 | xml: 170 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 171 | @echo 172 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 173 | 174 | pseudoxml: 175 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 176 | @echo 177 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." -------------------------------------------------------------------------------- /docs/authors.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../AUTHORS.rst -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # complexity documentation build configuration file, created by 5 | # sphinx-quickstart on Tue Jul 9 22:26:36 2013. 6 | # 7 | # This file is execfile()d with the current directory set to its containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | import sys, os 16 | 17 | # If extensions (or modules to document with autodoc) are in another directory, 18 | # add these directories to sys.path here. If the directory is relative to the 19 | # documentation root, use os.path.abspath to make it absolute, like shown here. 20 | #sys.path.insert(0, os.path.abspath('.')) 21 | 22 | # Get the project root dir, which is the parent dir of this 23 | cwd = os.getcwd() 24 | project_root = os.path.dirname(cwd) 25 | 26 | # Insert the project root dir as the first element in the PYTHONPATH. 27 | # This lets us ensure that the source package is imported, and that its 28 | # version is used. 29 | sys.path.insert(0, project_root) 30 | 31 | import py65c 32 | 33 | # -- General configuration ----------------------------------------------------- 34 | 35 | # If your documentation needs a minimal Sphinx version, state it here. 36 | #needs_sphinx = '1.0' 37 | 38 | # Add any Sphinx extension module names here, as strings. They can be extensions 39 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 40 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] 41 | 42 | # Add any paths that contain templates here, relative to this directory. 43 | templates_path = ['_templates'] 44 | 45 | # The suffix of source filenames. 46 | source_suffix = '.rst' 47 | 48 | # The encoding of source files. 49 | #source_encoding = 'utf-8-sig' 50 | 51 | # The master toctree document. 52 | master_doc = 'index' 53 | 54 | # General information about the project. 55 | project = u'Python to 6502 Compiler' 56 | copyright = u'2014, Jeremy Neiman' 57 | 58 | # The version info for the project you're documenting, acts as replacement for 59 | # |version| and |release|, also used in various other places throughout the 60 | # built documents. 61 | # 62 | # The short X.Y version. 63 | version = py65c.__version__ 64 | # The full version, including alpha/beta/rc tags. 65 | release = py65c.__version__ 66 | 67 | # The language for content autogenerated by Sphinx. Refer to documentation 68 | # for a list of supported languages. 69 | #language = None 70 | 71 | # There are two options for replacing |today|: either, you set today to some 72 | # non-false value, then it is used: 73 | #today = '' 74 | # Else, today_fmt is used as the format for a strftime call. 75 | #today_fmt = '%B %d, %Y' 76 | 77 | # List of patterns, relative to source directory, that match files and 78 | # directories to ignore when looking for source files. 79 | exclude_patterns = ['_build'] 80 | 81 | # The reST default role (used for this markup: `text`) to use for all documents. 82 | #default_role = None 83 | 84 | # If true, '()' will be appended to :func: etc. cross-reference text. 85 | #add_function_parentheses = True 86 | 87 | # If true, the current module name will be prepended to all description 88 | # unit titles (such as .. function::). 89 | #add_module_names = True 90 | 91 | # If true, sectionauthor and moduleauthor directives will be shown in the 92 | # output. They are ignored by default. 93 | #show_authors = False 94 | 95 | # The name of the Pygments (syntax highlighting) style to use. 96 | pygments_style = 'sphinx' 97 | 98 | # A list of ignored prefixes for module index sorting. 99 | #modindex_common_prefix = [] 100 | 101 | # If true, keep warnings as "system message" paragraphs in the built documents. 102 | #keep_warnings = False 103 | 104 | 105 | # -- Options for HTML output --------------------------------------------------- 106 | 107 | # The theme to use for HTML and HTML Help pages. See the documentation for 108 | # a list of builtin themes. 109 | html_theme = 'default' 110 | 111 | # Theme options are theme-specific and customize the look and feel of a theme 112 | # further. For a list of options available for each theme, see the 113 | # documentation. 114 | #html_theme_options = {} 115 | 116 | # Add any paths that contain custom themes here, relative to this directory. 117 | #html_theme_path = [] 118 | 119 | # The name for this set of Sphinx documents. If None, it defaults to 120 | # " v documentation". 121 | #html_title = None 122 | 123 | # A shorter title for the navigation bar. Default is the same as html_title. 124 | #html_short_title = None 125 | 126 | # The name of an image file (relative to this directory) to place at the top 127 | # of the sidebar. 128 | #html_logo = None 129 | 130 | # The name of an image file (within the static path) to use as favicon of the 131 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 132 | # pixels large. 133 | #html_favicon = None 134 | 135 | # Add any paths that contain custom static files (such as style sheets) here, 136 | # relative to this directory. They are copied after the builtin static files, 137 | # so a file named "default.css" will overwrite the builtin "default.css". 138 | html_static_path = ['_static'] 139 | 140 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 141 | # using the given strftime format. 142 | #html_last_updated_fmt = '%b %d, %Y' 143 | 144 | # If true, SmartyPants will be used to convert quotes and dashes to 145 | # typographically correct entities. 146 | #html_use_smartypants = True 147 | 148 | # Custom sidebar templates, maps document names to template names. 149 | #html_sidebars = {} 150 | 151 | # Additional templates that should be rendered to pages, maps page names to 152 | # template names. 153 | #html_additional_pages = {} 154 | 155 | # If false, no module index is generated. 156 | #html_domain_indices = True 157 | 158 | # If false, no index is generated. 159 | #html_use_index = True 160 | 161 | # If true, the index is split into individual pages for each letter. 162 | #html_split_index = False 163 | 164 | # If true, links to the reST sources are added to the pages. 165 | #html_show_sourcelink = True 166 | 167 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 168 | #html_show_sphinx = True 169 | 170 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 171 | #html_show_copyright = True 172 | 173 | # If true, an OpenSearch description file will be output, and all pages will 174 | # contain a tag referring to it. The value of this option must be the 175 | # base URL from which the finished HTML is served. 176 | #html_use_opensearch = '' 177 | 178 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 179 | #html_file_suffix = None 180 | 181 | # Output file base name for HTML help builder. 182 | htmlhelp_basename = 'py65cdoc' 183 | 184 | 185 | # -- Options for LaTeX output -------------------------------------------------- 186 | 187 | latex_elements = { 188 | # The paper size ('letterpaper' or 'a4paper'). 189 | #'papersize': 'letterpaper', 190 | 191 | # The font size ('10pt', '11pt' or '12pt'). 192 | #'pointsize': '10pt', 193 | 194 | # Additional stuff for the LaTeX preamble. 195 | #'preamble': '', 196 | } 197 | 198 | # Grouping the document tree into LaTeX files. List of tuples 199 | # (source start file, target name, title, author, documentclass [howto/manual]). 200 | latex_documents = [ 201 | ('index', 'py65c.tex', u'Python to 6502 Compiler Documentation', 202 | u'Jeremy Neiman', 'manual'), 203 | ] 204 | 205 | # The name of an image file (relative to this directory) to place at the top of 206 | # the title page. 207 | #latex_logo = None 208 | 209 | # For "manual" documents, if this is true, then toplevel headings are parts, 210 | # not chapters. 211 | #latex_use_parts = False 212 | 213 | # If true, show page references after internal links. 214 | #latex_show_pagerefs = False 215 | 216 | # If true, show URL addresses after external links. 217 | #latex_show_urls = False 218 | 219 | # Documents to append as an appendix to all manuals. 220 | #latex_appendices = [] 221 | 222 | # If false, no module index is generated. 223 | #latex_domain_indices = True 224 | 225 | 226 | # -- Options for manual page output -------------------------------------------- 227 | 228 | # One entry per manual page. List of tuples 229 | # (source start file, name, description, authors, manual section). 230 | man_pages = [ 231 | ('index', 'py65c', u'Python to 6502 Compiler Documentation', 232 | [u'Jeremy Neiman'], 1) 233 | ] 234 | 235 | # If true, show URL addresses after external links. 236 | #man_show_urls = False 237 | 238 | 239 | # -- Options for Texinfo output ------------------------------------------------ 240 | 241 | # Grouping the document tree into Texinfo files. List of tuples 242 | # (source start file, target name, title, author, 243 | # dir menu entry, description, category) 244 | texinfo_documents = [ 245 | ('index', 'py65c', u'Python to 6502 Compiler Documentation', 246 | u'Jeremy Neiman', 'py65c', 'One line description of project.', 247 | 'Miscellaneous'), 248 | ] 249 | 250 | # Documents to append as an appendix to all manuals. 251 | #texinfo_appendices = [] 252 | 253 | # If false, no module index is generated. 254 | #texinfo_domain_indices = True 255 | 256 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 257 | #texinfo_show_urls = 'footnote' 258 | 259 | # If true, do not generate a @detailmenu in the "Top" node's menu. 260 | #texinfo_no_detailmenu = False -------------------------------------------------------------------------------- /docs/contributing.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../CONTRIBUTING.rst -------------------------------------------------------------------------------- /docs/history.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../HISTORY.rst -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. complexity documentation master file, created by 2 | sphinx-quickstart on Tue Jul 9 22:26:36 2013. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to Python to 6502 Compiler's documentation! 7 | ====================================== 8 | 9 | Contents: 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | readme 15 | installation 16 | usage 17 | contributing 18 | authors 19 | history 20 | 21 | Indices and tables 22 | ================== 23 | 24 | * :ref:`genindex` 25 | * :ref:`modindex` 26 | * :ref:`search` 27 | -------------------------------------------------------------------------------- /docs/installation.rst: -------------------------------------------------------------------------------- 1 | ============ 2 | Installation 3 | ============ 4 | 5 | At the command line:: 6 | 7 | $ easy_install py65c 8 | 9 | Or, if you have virtualenvwrapper installed:: 10 | 11 | $ mkvirtualenv py65c 12 | $ pip install py65c -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | set I18NSPHINXOPTS=%SPHINXOPTS% . 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 31 | echo. text to make text files 32 | echo. man to make manual pages 33 | echo. texinfo to make Texinfo files 34 | echo. gettext to make PO message catalogs 35 | echo. changes to make an overview over all changed/added/deprecated items 36 | echo. xml to make Docutils-native XML files 37 | echo. pseudoxml to make pseudoxml-XML files for display purposes 38 | echo. linkcheck to check all external links for integrity 39 | echo. doctest to run all doctests embedded in the documentation if enabled 40 | goto end 41 | ) 42 | 43 | if "%1" == "clean" ( 44 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 45 | del /q /s %BUILDDIR%\* 46 | goto end 47 | ) 48 | 49 | 50 | %SPHINXBUILD% 2> nul 51 | if errorlevel 9009 ( 52 | echo. 53 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 54 | echo.installed, then set the SPHINXBUILD environment variable to point 55 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 56 | echo.may add the Sphinx directory to PATH. 57 | echo. 58 | echo.If you don't have Sphinx installed, grab it from 59 | echo.http://sphinx-doc.org/ 60 | exit /b 1 61 | ) 62 | 63 | if "%1" == "html" ( 64 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 65 | if errorlevel 1 exit /b 1 66 | echo. 67 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 68 | goto end 69 | ) 70 | 71 | if "%1" == "dirhtml" ( 72 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 73 | if errorlevel 1 exit /b 1 74 | echo. 75 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 76 | goto end 77 | ) 78 | 79 | if "%1" == "singlehtml" ( 80 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 81 | if errorlevel 1 exit /b 1 82 | echo. 83 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 84 | goto end 85 | ) 86 | 87 | if "%1" == "pickle" ( 88 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 89 | if errorlevel 1 exit /b 1 90 | echo. 91 | echo.Build finished; now you can process the pickle files. 92 | goto end 93 | ) 94 | 95 | if "%1" == "json" ( 96 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 97 | if errorlevel 1 exit /b 1 98 | echo. 99 | echo.Build finished; now you can process the JSON files. 100 | goto end 101 | ) 102 | 103 | if "%1" == "htmlhelp" ( 104 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 105 | if errorlevel 1 exit /b 1 106 | echo. 107 | echo.Build finished; now you can run HTML Help Workshop with the ^ 108 | .hhp project file in %BUILDDIR%/htmlhelp. 109 | goto end 110 | ) 111 | 112 | if "%1" == "qthelp" ( 113 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 114 | if errorlevel 1 exit /b 1 115 | echo. 116 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 117 | .qhcp project file in %BUILDDIR%/qthelp, like this: 118 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\complexity.qhcp 119 | echo.To view the help file: 120 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\complexity.ghc 121 | goto end 122 | ) 123 | 124 | if "%1" == "devhelp" ( 125 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 126 | if errorlevel 1 exit /b 1 127 | echo. 128 | echo.Build finished. 129 | goto end 130 | ) 131 | 132 | if "%1" == "epub" ( 133 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 134 | if errorlevel 1 exit /b 1 135 | echo. 136 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 137 | goto end 138 | ) 139 | 140 | if "%1" == "latex" ( 141 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 142 | if errorlevel 1 exit /b 1 143 | echo. 144 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 145 | goto end 146 | ) 147 | 148 | if "%1" == "latexpdf" ( 149 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 150 | cd %BUILDDIR%/latex 151 | make all-pdf 152 | cd %BUILDDIR%/.. 153 | echo. 154 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 155 | goto end 156 | ) 157 | 158 | if "%1" == "latexpdfja" ( 159 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 160 | cd %BUILDDIR%/latex 161 | make all-pdf-ja 162 | cd %BUILDDIR%/.. 163 | echo. 164 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 165 | goto end 166 | ) 167 | 168 | if "%1" == "text" ( 169 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 170 | if errorlevel 1 exit /b 1 171 | echo. 172 | echo.Build finished. The text files are in %BUILDDIR%/text. 173 | goto end 174 | ) 175 | 176 | if "%1" == "man" ( 177 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 178 | if errorlevel 1 exit /b 1 179 | echo. 180 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 181 | goto end 182 | ) 183 | 184 | if "%1" == "texinfo" ( 185 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 186 | if errorlevel 1 exit /b 1 187 | echo. 188 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 189 | goto end 190 | ) 191 | 192 | if "%1" == "gettext" ( 193 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 194 | if errorlevel 1 exit /b 1 195 | echo. 196 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 197 | goto end 198 | ) 199 | 200 | if "%1" == "changes" ( 201 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 202 | if errorlevel 1 exit /b 1 203 | echo. 204 | echo.The overview file is in %BUILDDIR%/changes. 205 | goto end 206 | ) 207 | 208 | if "%1" == "linkcheck" ( 209 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 210 | if errorlevel 1 exit /b 1 211 | echo. 212 | echo.Link check complete; look for any errors in the above output ^ 213 | or in %BUILDDIR%/linkcheck/output.txt. 214 | goto end 215 | ) 216 | 217 | if "%1" == "doctest" ( 218 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 219 | if errorlevel 1 exit /b 1 220 | echo. 221 | echo.Testing of doctests in the sources finished, look at the ^ 222 | results in %BUILDDIR%/doctest/output.txt. 223 | goto end 224 | ) 225 | 226 | if "%1" == "xml" ( 227 | %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml 228 | if errorlevel 1 exit /b 1 229 | echo. 230 | echo.Build finished. The XML files are in %BUILDDIR%/xml. 231 | goto end 232 | ) 233 | 234 | if "%1" == "pseudoxml" ( 235 | %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml 236 | if errorlevel 1 exit /b 1 237 | echo. 238 | echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. 239 | goto end 240 | ) 241 | 242 | :end -------------------------------------------------------------------------------- /docs/readme.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../README.rst -------------------------------------------------------------------------------- /docs/usage.rst: -------------------------------------------------------------------------------- 1 | ======== 2 | Usage 3 | ======== 4 | 5 | To use Python to 6502 Compiler in a project:: 6 | 7 | import py65c -------------------------------------------------------------------------------- /nosy.cfg: -------------------------------------------------------------------------------- 1 | #run with nosy -c nosy.cfg 2 | [nosy] 3 | base_path = ./ 4 | glob_patterns = *.py 5 | options = -w tests --exe -------------------------------------------------------------------------------- /py65c/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | __author__ = 'Jeremy Neiman' 5 | __email__ = 'docmarionum1@gmail.com' 6 | __version__ = '0.0' -------------------------------------------------------------------------------- /py65c/astpp.py: -------------------------------------------------------------------------------- 1 | """ 2 | A pretty-printing dump function for the ast module. The code was copied from 3 | the ast.dump function and modified slightly to pretty-print. 4 | 5 | Alex Leone (acleone ~AT~ gmail.com), 2010-01-30 6 | """ 7 | 8 | from ast import * 9 | 10 | def dump(node, annotate_fields=True, include_attributes=False, indent=' '): 11 | """ 12 | Return a formatted dump of the tree in *node*. This is mainly useful for 13 | debugging purposes. The returned string will show the names and the values 14 | for fields. This makes the code impossible to evaluate, so if evaluation is 15 | wanted *annotate_fields* must be set to False. Attributes such as line 16 | numbers and column offsets are not dumped by default. If this is wanted, 17 | *include_attributes* can be set to True. 18 | """ 19 | def _format(node, level=0): 20 | if isinstance(node, AST): 21 | fields = [(a, _format(b, level)) for a, b in iter_fields(node)] 22 | if include_attributes and node._attributes: 23 | fields.extend([(a, _format(getattr(node, a), level)) 24 | for a in node._attributes]) 25 | return ''.join([ 26 | node.__class__.__name__, 27 | '(', 28 | ', '.join(('%s=%s' % field for field in fields) 29 | if annotate_fields else 30 | (b for a, b in fields)), 31 | ')']) 32 | elif isinstance(node, list): 33 | lines = ['['] 34 | lines.extend((indent * (level + 2) + _format(x, level + 2) + ',' 35 | for x in node)) 36 | if len(lines) > 1: 37 | lines.append(indent * (level + 1) + ']') 38 | else: 39 | lines[-1] += ']' 40 | return '\n'.join(lines) 41 | return repr(node) 42 | if not isinstance(node, AST): 43 | raise TypeError('expected AST, got %r' % node.__class__.__name__) 44 | return _format(node) 45 | 46 | if __name__ == '__main__': 47 | import sys 48 | for filename in sys.argv[1:]: 49 | print '=' * 50 50 | print 'AST tree for', filename 51 | print '=' * 50 52 | f = open(filename, 'r') 53 | fstr = f.read() 54 | f.close() 55 | print dump(parse(fstr, filename=filename), include_attributes=True) 56 | print 57 | -------------------------------------------------------------------------------- /py65c/compiler.py: -------------------------------------------------------------------------------- 1 | import tokenize, StringIO, token, parser, symbol, compiler, ast, astpp, sys, random, string 2 | 3 | from py65asm.assembler import Assembler 4 | from py65emu.mmu import MMU 5 | from symbols import SymbolTable 6 | 7 | labels = { 8 | "and": 0, 9 | "or": 0, 10 | "if": 0, 11 | "while": 0, 12 | "gt": 0, 13 | "gte": 0, 14 | "lt": 0, 15 | "lte": 0, 16 | "mult": 0 17 | } 18 | 19 | AND = """ 20 | clc 21 | adc #0 22 | beq and_{1} 23 | lda {0} 24 | and_{1}: 25 | """ 26 | 27 | OR = """ 28 | clc 29 | adc #0 30 | bne or_{1} 31 | lda {0} 32 | or_{1}: 33 | """ 34 | 35 | GT = """ 36 | clc 37 | sbc {0} 38 | lda #0 39 | bcc end_gt_{1} 40 | adc #0 41 | end_gt_{1}: 42 | """ 43 | 44 | GTE = """ 45 | sec 46 | sbc {0} 47 | lda #0 48 | bcc end_gte_{1} 49 | adc #0 50 | end_gte_{1}: 51 | """ 52 | 53 | LT = """ 54 | sec 55 | sbc {0} 56 | clc 57 | bpl end_lt_{1} 58 | sec 59 | end_lt_{1}: 60 | lda #0 61 | adc #0 62 | """ 63 | 64 | LTE = """ 65 | clc 66 | sbc {0} 67 | clc 68 | bpl end_lte_{1} 69 | sec 70 | end_lte_{1}: 71 | lda #0 72 | adc #0 73 | """ 74 | 75 | MULT = """ 76 | tay 77 | beq mult_zero_{1} 78 | lda #0 79 | clc 80 | mult_{1}: 81 | adc {0} 82 | dey 83 | bne mult_{1} 84 | beq mult_not_zero_{1} 85 | mult_zero_{1}: 86 | lda #0 87 | mult_not_zero_{1} 88 | """ 89 | 90 | 91 | 92 | class Compiler(ast.NodeVisitor): 93 | def __init__(self, mmu=None, debug=False): 94 | """ 95 | mmu used for allocating memory to variables. The MMU should 96 | be representative of the target device. 97 | """ 98 | self.op = "null" 99 | self.debug = debug 100 | self.output = [] 101 | self.output_end = [] 102 | 103 | if mmu: 104 | self.mmu = mmu 105 | else: 106 | self.mmu = MMU([(0, 0x1000)]) 107 | 108 | self.symbol_table = SymbolTable() 109 | self.symbol_table.put('True', {'addr': None, 'value': True}) 110 | self.symbol_table.put('False', {'addr': None, 'value': False}) 111 | 112 | super(Compiler, self).__init__() 113 | 114 | def generic_visit(self, node): 115 | #print node 116 | super(Compiler, self).generic_visit(node) 117 | 118 | def visit(self, node): 119 | if self.debug: 120 | print "node:", node 121 | print "dir(node):", dir(node) 122 | for a in ["test", "body", "orelse", "targets", "value", "ops", "comparators", "ctx", "elts"]: 123 | if hasattr(node, a): 124 | print "%s:" % a, getattr(node, a) 125 | 126 | super(Compiler, self).visit(node) 127 | 128 | def _do_op(self, op, n): 129 | if op == "lda": 130 | self.output.append("lda %s" % n) 131 | elif op == "sta": 132 | self.output.append("sta %s" % n) 133 | 134 | elif op == "add": 135 | self.output.append("clc\nadc %s" % n) 136 | elif op == "sub": 137 | self.output.append("sec\nsbc %s" % n) 138 | 139 | elif op == "mult": 140 | self.output.append(MULT.format(n, labels["mult"])) 141 | labels["mult"] += 1 142 | 143 | elif op == "gt": 144 | self.output.append(GT.format(n, labels["gt"])) 145 | labels["gt"] += 1 146 | elif op == "gte": 147 | self.output.append(GTE.format(n, labels["gte"])) 148 | labels["gte"] += 1 149 | 150 | elif op == "lt": 151 | self.output.append(LT.format(n, labels["lt"])) 152 | labels["lt"] += 1 153 | elif op == "lte": 154 | self.output.append(LTE.format(n, labels["lte"])) 155 | labels["lte"] += 1 156 | 157 | elif op == "and": 158 | # A and B = 0 if A or B is 0 else B 159 | # 1 and 2 = 2 160 | # 0 and 2 = 0 161 | # 1 and 0 = 0 162 | # A is the value in the accumulator and B is the value on top of 163 | # the stack. 164 | self.output.append(AND.format(n, labels["and"])) 165 | labels["and"] += 1 166 | elif op == "or": 167 | # A or B = 0 if A and B is 0 else A 168 | # 1 or 2 = 1 169 | # 0 or 2 = 2 170 | # 1 or 0 = 1 171 | # A is the value in the accumulator and B is the value on top of 172 | # the stack. 173 | self.output.append(OR.format(n, labels["or"])) 174 | labels["or"] += 1 175 | else: #debug 176 | print op 177 | 178 | def _set_op(self, op): 179 | if type(op) == str: 180 | self.op = op 181 | 182 | elif type(op) == ast.Add: 183 | self.op = "add" 184 | elif type(op) == ast.Sub: 185 | self.op = "sub" 186 | 187 | elif type(op) == ast.Mult: 188 | self.op = "mult" 189 | 190 | elif type(op) == ast.Gt: 191 | self.op = "gt" 192 | elif type(op) == ast.GtE: 193 | self.op = "gte" 194 | 195 | elif type(op) == ast.Lt: 196 | self.op = "lt" 197 | elif type(op) == ast.LtE: 198 | self.op = "lte" 199 | 200 | elif type(op) == ast.And: 201 | self.op = "and" 202 | elif type(op) == ast.Or: 203 | self.op = "or" 204 | 205 | else: 206 | print op 207 | 208 | return self.op 209 | 210 | def _malloc(self, size=1, location="zero_page"): 211 | if location == "zero_page": 212 | start = 0x0 213 | elif location == "stack": 214 | start = 0x100 215 | else: 216 | start = 0x200 217 | 218 | count = 0 219 | for i in range(start, 0x10000): 220 | try: 221 | if not self.mmu.read(i): 222 | count += 1 223 | else: 224 | count = 0 225 | except: 226 | count = 0 227 | 228 | if count == size: 229 | for j in range(i - count + 1, i - count + size + 1): 230 | self.mmu.write(j, 1) 231 | return i - count + 1 232 | 233 | raise Exception("Out of memory") 234 | 235 | 236 | def _free(self, location, size=1): 237 | if type(location) == int: 238 | for i in range(size): 239 | self.mmu.write(location+i, 0) 240 | 241 | def _freeVar(self, name, size=1): 242 | a = self.symbol_table.getAddr(name) 243 | if type(location) == int: 244 | self._free(a, size) 245 | 246 | def _addr(self, name=None, location="heap"): 247 | if not name: 248 | name = self._id() 249 | if not self.symbol_table.getLocal(name): 250 | if self.symbol_table.scopeIsGlobal(): 251 | a = self._malloc(location=location) 252 | else: 253 | a = '$01%0*x,X' % (2, 3+self.symbol_table.numNonGlobalVars()) 254 | 255 | 256 | self.symbol_table.put(name, {'addr': a}) 257 | 258 | return self.symbol_table.getAddr(name) 259 | 260 | def _id(self, size=32, chars=string.ascii_uppercase + string.digits): 261 | return ''.join(random.choice(chars) for _ in range(size)) 262 | 263 | def visit_BinOp(self, node): 264 | self._set_op("lda") 265 | super(Compiler, self).visit(node.right) 266 | a = self._addr(name=str(hash(node)), location="zero_page") 267 | self._do_op("sta", a) 268 | 269 | self._set_op("lda") 270 | super(Compiler, self).visit(node.left) 271 | 272 | self._set_op(node.op) 273 | self._do_op(self.op, a) 274 | self._free(a) 275 | 276 | def visit_BoolOp(self, node): 277 | """ 278 | Resolve 'and' and 'or' expressions. 279 | each value is computing in reverse order and then combined. 280 | """ 281 | 282 | #Load first value 283 | self.op = "lda" 284 | super(Compiler, self).visit(node.values[-1]) 285 | a = self._addr(name=str(hash(node)), location="zero_page") 286 | self._do_op("sta", a) 287 | 288 | for i in range(len(node.values)-2, -1, -1): 289 | #Compute the next value, leaving it in A 290 | self.op = "lda" 291 | super(Compiler, self).visit(node.values[i]) 292 | 293 | #Combine the two values and store on the stack 294 | self._set_op(node.op) 295 | self._do_op(self.op, a) 296 | 297 | #Don't need to store if it's the last one 298 | if i > 0: 299 | #self.output.append("pha") 300 | self._do_op("sta", a) 301 | 302 | self._free(a) 303 | 304 | def visit_Num(self, node): 305 | self._do_op(self.op, "#%s" % node.n) 306 | 307 | def visit_Name(self, node): 308 | if node.id == "True": 309 | v = "#1" 310 | elif node.id == "False": 311 | v = "#0" 312 | else: 313 | v = self._addr(node.id) 314 | 315 | self._do_op(self.op, v) 316 | 317 | def visit_List(self, node): 318 | a = self._malloc(len(node.elts), location="heap") 319 | for i in range(len(node.elts)): 320 | self._set_op("lda") 321 | super(Compiler, self).visit(node.elts[i]) 322 | self.output.append("sta ${0}".format(hex(a + i)[2:])) 323 | return a, len(node.elts) 324 | 325 | def visit_Subscript(self, node): 326 | self.output.append("pha") 327 | self._set_op("lda") 328 | super(Compiler, self).visit(node.slice) 329 | self.output.append("tay") 330 | self.output.append("pla") 331 | 332 | if type(node.ctx) == ast.Load: 333 | self.output.append("lda ({0}),y".format(self._addr(node.value.id))) 334 | elif type(node.ctx) == ast.Store: 335 | self.output.append("sta ({0}),y".format(self._addr(node.value.id))) 336 | 337 | 338 | def visit_Assign(self, node): 339 | if type(node.value) == ast.List: 340 | a = self._malloc(2) 341 | h, l = super(Compiler, self).visit(node.value) 342 | self._do_op("lda", "#{0}".format(h & 0xff)) 343 | self._do_op("sta", a) 344 | self._do_op("lda", "#{0}".format(h >> 8)) 345 | self._do_op("sta", a + 1) 346 | self.symbol_table.put(node.targets[0].id, {'addr': a, 'list_addr': h, 'len': l}) 347 | else: 348 | self._set_op("lda") 349 | super(Compiler, self).visit(node.value) 350 | self._set_op("sta") 351 | super(Compiler, self).visit(node.targets[0]) 352 | 353 | def visit_If(self, node): 354 | self._set_op("lda") 355 | super(Compiler, self).visit(node.test) 356 | label = "if_not_%s" % labels["if"] 357 | label_2 = "end_if_%s" % labels["if"] 358 | labels["if"] += 1 359 | self.output.append("clc\nadc #0\nbeq %s" % label) 360 | 361 | for n in node.body: 362 | super(Compiler, self).visit(n) 363 | 364 | if node.orelse: 365 | self.output.append("jmp %s\n%s:" % (label_2, label)) 366 | 367 | for n in node.orelse: 368 | super(Compiler, self).visit(n) 369 | self.output.append("%s:" % label_2) 370 | else: 371 | self.output.append("%s:" % label) 372 | 373 | def visit_While(self, node): 374 | self.output.append("while_{0}:".format(labels["while"])) 375 | self._set_op("lda") 376 | super(Compiler, self).visit(node.test) 377 | self.output.append("clc\nadc #0\nbne while_body_{0}\njmp end_while_{0}\nwhile_body_{0}:".format(labels["while"])) 378 | 379 | for n in node.body: 380 | super(Compiler, self).visit(n) 381 | 382 | self.output.append("jmp while_{0}\nend_while_{0}:".format(labels["while"])) 383 | 384 | labels["while"] += 1 385 | 386 | 387 | def visit_Compare(self, node): 388 | # Compute right branch and store it on the stack 389 | self.op = "lda" 390 | super(Compiler, self).visit(node.comparators[0]) 391 | a = self._addr(name=str(hash(node)), location="zero_page") 392 | self._do_op("sta", a) 393 | 394 | #Compute left branch 395 | self.op = "lda" 396 | super(Compiler, self).visit(node.left) 397 | 398 | #Do op 399 | self._set_op(node.ops[0]) 400 | self._do_op(self.op, a) 401 | self._free(a) 402 | 403 | def visit_FunctionDef(self, node): 404 | # Create a placeholder for the function definition in the outer scope. 405 | self.symbol_table.put(node.name, {"PROCESSING": True}) 406 | 407 | # Create new scope for function 408 | self.symbol_table.push() 409 | 410 | # Process the arguments to create local variables for them and store defaults. 411 | args_dict = {} 412 | args_list = [] 413 | for i in range(len(node.args.args)): 414 | arg = {'i': i, 'name': node.args.args[i].id} 415 | 416 | if (len(node.args.args) - i) <= len(node.args.defaults): 417 | arg['default'] = node.args.defaults[i + len(node.args.defaults) - len(node.args.args)] 418 | else: 419 | arg['default'] = None 420 | 421 | args_dict[node.args.args[i].id] = arg 422 | args_list.append(arg) 423 | 424 | #register stack location on symtable 425 | self._addr(node.args.args[i].id) 426 | 427 | 428 | # Process the function body and store the output at the end. 429 | temp = self.output 430 | self.output = self.output_end 431 | 432 | self.output.append(node.name + ":") 433 | self.output.append("tsx") 434 | for n in node.body: 435 | super(Compiler, self).visit(n) 436 | 437 | self.output = temp 438 | 439 | # Update the function definition 440 | num_locals = self.symbol_table.numLocalVars() - len(args_list) 441 | self.symbol_table.set(node.name, 442 | {'addr': node.name, 'args': [args_list, args_dict], 'num_locals': num_locals}) 443 | 444 | #Check for Recursive Calls 445 | while True: 446 | try: 447 | i = self.output_end.index("RECURSION_%s" % node.name) 448 | temp = self.output 449 | self.output = [] 450 | super(Compiler, self).visit(self.output_end[i+1]) 451 | self.output_end = self.output_end[:i] + self.output + self.output_end[i+2:] 452 | self.output = temp 453 | except: 454 | break 455 | 456 | # Resolve Return Addresses 457 | a = self._addr("return_value") 458 | for i in range(len(self.output_end)): 459 | self.output_end[i] = self.output_end[i].replace('return_value', a) 460 | 461 | 462 | self.symbol_table.pop() 463 | 464 | 465 | def visit_Return(self, node): 466 | self.op = "lda" 467 | super(Compiler, self).visit(node.value) 468 | self.output.append("sta %s" % "return_value") 469 | self.output.append("rts") 470 | 471 | def visit_Call(self, node): 472 | #Check if this is a recursive call 473 | if "PROCESSING" in self.symbol_table.get(node.func.id): 474 | #Put a placeholder for the call. 475 | self.output.append("RECURSION_%s" % node.func.id) 476 | self.output.append(node) 477 | 478 | #Allocate space for locals in the call arguments 479 | temp = self.output 480 | self.output = [] 481 | for i in range(len(node.args)): 482 | super(Compiler, self).visit(node.args[i]) 483 | self.output = temp 484 | return 485 | 486 | #Allocate space for return value and local variables 487 | for i in range(self.symbol_table.get(node.func.id)['num_locals'] + 1): 488 | self.output.append("pha") 489 | 490 | #Push args onto stack 491 | keywords = dict([(i.arg, i.value) for i in node.keywords]) 492 | defaults = self.symbol_table.get(node.func.id)['args'] 493 | 494 | for i in range(len(defaults[0]) - 1, -1, -1): 495 | if i < len(node.args): 496 | super(Compiler, self).visit(node.args[i]) 497 | elif defaults[0][i]['name'] in keywords: 498 | super(Compiler, self).visit(keywords[defaults[0][i]['name']]) 499 | else: 500 | super(Compiler, self).visit(defaults[0][i]['default']) 501 | 502 | self.output.append("pha") 503 | 504 | #Call function 505 | self.output.append("jsr %s" % node.func.id) 506 | 507 | #Pull off all arguments and the return value 508 | self.output.append("tsx") 509 | for i in range(len(defaults[0])+self.symbol_table.get(node.func.id)['num_locals']): 510 | self.output.append("inx") 511 | self.output.append("txs") 512 | self.output.append("pla") 513 | self.output.append("tsx") 514 | 515 | 516 | 517 | def compile(inp, debug=True, org=0x1000): 518 | print inp, type(inp) 519 | if type(inp) == file: 520 | s = inp.read() 521 | elif type(inp) == str: 522 | try: 523 | with open(inp, 'r') as f: 524 | s = f.read() 525 | except: 526 | s = inp 527 | 528 | c = Compiler(debug=debug) 529 | t = ast.parse(s) 530 | c.visit(t) 531 | 532 | if debug: 533 | print s 534 | print astpp.dump(t, False) 535 | 536 | asm = "\n".join(c.output) + "\nbrk\n" + "\n".join(c.output_end) 537 | print "**********\n", asm 538 | a = Assembler(org=org) 539 | bin = a.assemble(asm) 540 | return bin 541 | 542 | 543 | if __name__ == "__main__": 544 | compile(sys.argv[1]) 545 | -------------------------------------------------------------------------------- /py65c/symbols.py: -------------------------------------------------------------------------------- 1 | class scope: 2 | def __init__(self, subscope): 3 | self.subscope = subscope 4 | self.curscope = {} 5 | self.localVarLoc = 0 6 | 7 | def get(self, var): 8 | if var in self.curscope: 9 | return self.curscope[var] 10 | else: 11 | if self.subscope: 12 | v = self.subscope.get(var) 13 | if v: 14 | return v 15 | 16 | def getLocal(self, var): 17 | if var in self.curscope: 18 | return self.curscope[var] 19 | 20 | def set(self, var, value): 21 | if var in self.curscope: 22 | self.curscope[var] = value 23 | return True 24 | else: 25 | if self.subscope: 26 | return self.subscope.set(var, value) 27 | else: 28 | return False 29 | 30 | def put(self, var, val): 31 | self.curscope[var] = val 32 | 33 | def delete(self, var): 34 | del self.curscope[var] 35 | 36 | def pop(self): 37 | return self.subscope 38 | 39 | def numNonGlobalVars(self): 40 | if self.subscope: 41 | return len(self.curscope) + self.subscope.numNonGlobalVars() 42 | else: 43 | return 0 44 | 45 | def numLocalVars(self): 46 | return len(self.curscope) 47 | 48 | 49 | #Push new scope 50 | # curscope = scope(curscope) 51 | #get value, go down scopes if necessary 52 | # v = curscope.get(var) 53 | #put new value in current scope 54 | # curscope.put(var, val) 55 | #pop off old scope 56 | # curscope = curscope.pop() 57 | 58 | class SymbolTable: 59 | def __init__(self): 60 | """ 61 | mmu - the mmu representing the memory layout of the target device. 62 | used for allocating memory to variables. 63 | """ 64 | self.scope = scope(None) 65 | 66 | def get(self, name): 67 | return self.scope.get(name) 68 | 69 | def getAddr(self, name): 70 | return self.scope.get(name)['addr'] 71 | 72 | def set(self, name, value): 73 | self.scope.set(name, value) 74 | 75 | def put(self, name, value): 76 | self.scope.put(name, value) 77 | 78 | def delete(self, name): 79 | self.scope.delete(name) 80 | 81 | def push(self): 82 | self.scope = scope(self.scope) 83 | 84 | def pop(self): 85 | self.scope = self.scope.pop() 86 | 87 | def scopeIsGlobal(self): 88 | return self.scope.subscope == None 89 | 90 | def getLocal(self, name): 91 | return self.scope.getLocal(name) 92 | 93 | def numNonGlobalVars(self): 94 | return self.scope.numNonGlobalVars() 95 | 96 | def numLocalVars(self): 97 | return self.scope.numLocalVars() 98 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | wheel==0.22.0 2 | py65asm==0.0 3 | py65emu==0.0 -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [wheel] 2 | universal = 1 -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import sys 6 | 7 | 8 | try: 9 | from setuptools import setup 10 | except ImportError: 11 | from distutils.core import setup 12 | 13 | if sys.argv[-1] == 'publish': 14 | os.system('python setup.py sdist upload') 15 | sys.exit() 16 | 17 | readme = open('README.rst').read() 18 | history = open('HISTORY.rst').read().replace('.. :changelog:', '') 19 | 20 | setup( 21 | name='py65c', 22 | version='0.0', 23 | description='A Python to 6502 Assembly Compiler', 24 | long_description=readme + '\n\n' + history, 25 | author='Jeremy Neiman', 26 | author_email='docmarionum1@gmail.com', 27 | url='https://github.com/docmarionum1/py65c', 28 | packages=[ 29 | 'py65c', 30 | ], 31 | package_dir={'py65c': 'py65c'}, 32 | include_package_data=True, 33 | install_requires=[ 34 | ], 35 | license="BSD", 36 | zip_safe=False, 37 | keywords='py65c', 38 | classifiers=[ 39 | 'Development Status :: 2 - Pre-Alpha', 40 | 'Intended Audience :: Developers', 41 | 'License :: OSI Approved :: BSD License', 42 | 'Natural Language :: English', 43 | "Programming Language :: Python :: 2", 44 | 'Programming Language :: Python :: 2.6', 45 | 'Programming Language :: Python :: 2.7', 46 | 'Programming Language :: Python :: 3', 47 | 'Programming Language :: Python :: 3.3', 48 | ], 49 | test_suite='tests', 50 | ) -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | -------------------------------------------------------------------------------- /tests/files/addsub.py: -------------------------------------------------------------------------------- 1 | a = 1 + 1 2 | b = 5 - 4 3 | c = 4 - 5 4 | d = -1 + 1 5 | e = 1 + 1 - 1 -------------------------------------------------------------------------------- /tests/files/bool.py: -------------------------------------------------------------------------------- 1 | a = True and True #True 2 | b = True and False #False 3 | c = False and False #False 4 | 5 | d = True or True #True 6 | e = False or False #False 7 | 8 | f = 4 and 5 #5 9 | g = 5 and 4 #4 10 | h = 5 and 0 #0 11 | 12 | i = 5 or 4 #5 13 | j = 4 or 5 #4 14 | k = 0 or 4 #4 15 | l = 4 or 0 #4 -------------------------------------------------------------------------------- /tests/files/fib.py: -------------------------------------------------------------------------------- 1 | #Allocate list for output 0-9 2 | o = [1, 1, 0, 0, 0, 0, 0, 0, 0, 0] 3 | 4 | i = 2 5 | 6 | while i < 10: 7 | o[i] = o[i-1] + o[i-2] 8 | i = i + 1 9 | 10 | #TODO: Add More implemnations of fib (i.e. o = list(10), i+=1 and function for fib) 11 | 12 | def fib(n): 13 | if n < 2: 14 | return 1 15 | else: 16 | return fib(n-1) + fib(n-2) 17 | 18 | 19 | a = fib(5) -------------------------------------------------------------------------------- /tests/files/func.py: -------------------------------------------------------------------------------- 1 | def f(x, y=1): 2 | return x + y 3 | 4 | a = f(1, 2) 5 | b = 2 + f(1, 2) 6 | c = f(1, 2) + f(1, 2) 7 | 8 | d = f(1) 9 | e = f(4) 10 | g = f(1, y=3) 11 | 12 | def h(x, y, z): 13 | return x*y-z 14 | 15 | i = h(5, 3, 6) 16 | j = h(8, 4, 12) 17 | 18 | def k(r, s=4, t=f(5,5)): 19 | return t-s-r 20 | 21 | l = k(1, s=2) 22 | m = k(2, t=20) 23 | -------------------------------------------------------------------------------- /tests/files/gt.py: -------------------------------------------------------------------------------- 1 | a = 5 > 3 2 | b = 5 > 4 3 | c = 5 > 5 4 | d = 5 > 6 5 | e = 5 > 7 6 | -------------------------------------------------------------------------------- /tests/files/gte.py: -------------------------------------------------------------------------------- 1 | a = 5 >= 3 2 | b = 5 >= 4 3 | c = 5 >= 5 4 | d = 5 >= 6 5 | e = 5 >= 7 6 | -------------------------------------------------------------------------------- /tests/files/if.py: -------------------------------------------------------------------------------- 1 | if 1 - 2 + True: 2 | a = 32 3 | else: 4 | a = 64 5 | 6 | if True: 7 | b = 10 8 | 9 | b = b + b -------------------------------------------------------------------------------- /tests/files/list.py: -------------------------------------------------------------------------------- 1 | s = [1, 2, 3, 4, 5, 0] 2 | a = 0 3 | i = 5 4 | 5 | while i > 0: 6 | i = i - 1 7 | a = a + s[i] 8 | 9 | s[5] = a 10 | -------------------------------------------------------------------------------- /tests/files/lte.py: -------------------------------------------------------------------------------- 1 | a = 5 < 3 2 | b = 5 < 4 3 | c = 5 < 5 4 | d = 5 < 6 5 | e = 5 < 7 6 | 7 | f = 5 <= 3 8 | g = 5 <= 4 9 | h = 5 <= 5 10 | i = 5 <= 6 11 | j = 5 <= 7 12 | 13 | k = -1 < 5 14 | l = 5 < -1 15 | m = -1 <= 5 16 | n = 5 <= -1 17 | o = -1 <= -1 -------------------------------------------------------------------------------- /tests/files/mul.py: -------------------------------------------------------------------------------- 1 | a = 5 * 5 2 | b = 4 * 6 3 | c = 10 * 0 4 | d = 0 * 10 5 | e = 0x10 * 0x11 6 | f = 10 * (5 + 5) 7 | g = 5 + 10*10 -------------------------------------------------------------------------------- /tests/files/recur.py: -------------------------------------------------------------------------------- 1 | def f(i): 2 | if i < 1: 3 | return 0 4 | else: 5 | return i + f(i-1) 6 | 7 | a = f(2) 8 | b = f(5) 9 | c = f(f(2)) -------------------------------------------------------------------------------- /tests/files/while.py: -------------------------------------------------------------------------------- 1 | i = 10 2 | j = 0 3 | while i > 2: 4 | i = i - 1 5 | j = j + 8 -------------------------------------------------------------------------------- /tests/test_py65c.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | test_py65c 6 | ---------------------------------- 7 | 8 | Tests for `py65c` module. 9 | """ 10 | 11 | import unittest, os 12 | 13 | from py65c.compiler import compile, Compiler 14 | from py65emu.cpu import CPU 15 | from py65emu.mmu import MMU 16 | 17 | 18 | 19 | class TestPy65c(unittest.TestCase): 20 | 21 | def _loadFile(self, filename): 22 | return open(os.path.join( 23 | os.path.dirname(os.path.realpath(__file__)), 24 | "files", filename 25 | )) 26 | 27 | def _generic_cpu(self, rom): 28 | mmu = MMU([ 29 | (0x0000, 0x400), #RAM 30 | (0x1000, 0x1000, True, rom) #ROM 31 | ]) 32 | c = CPU(mmu, 0x1000) 33 | return c 34 | 35 | def test_malloc_free(self): 36 | c = Compiler(mmu=MMU([(0, 0x10), (0x20, 0x10)])) 37 | l1 = c._malloc(0x1) 38 | self.assertEqual(l1, 0) 39 | 40 | l2 = c._malloc(0x1) 41 | self.assertEqual(l2, 1) 42 | 43 | l3 = c._malloc(0x10) 44 | self.assertEqual(l3, 0x20) 45 | 46 | c._free(l1) 47 | 48 | l4 = c._malloc(0x1) 49 | self.assertEqual(l4, 0) 50 | 51 | with self.assertRaises(Exception): 52 | c._malloc(0x10) 53 | 54 | 55 | def _compile_and_run(self, filename): 56 | bin = compile(self._loadFile(filename)) 57 | #print bin 58 | c = self._generic_cpu(bin) 59 | while True: 60 | try: 61 | #print hex(c.r.pc), ":", hex(c.mmu.read(c.r.pc)), c.r 62 | c.step() 63 | except: 64 | break 65 | 66 | return c 67 | 68 | def setUp(self): 69 | pass 70 | 71 | def test_gt(self): 72 | c = self._compile_and_run("gt.py") 73 | 74 | self.assertEqual(c.mmu.read(0x200), True) 75 | self.assertEqual(c.mmu.read(0x201), True) 76 | self.assertEqual(c.mmu.read(0x202), False) 77 | self.assertEqual(c.mmu.read(0x203), False) 78 | self.assertEqual(c.mmu.read(0x204), False) 79 | 80 | def test_gte(self): 81 | c = self._compile_and_run("gte.py") 82 | 83 | self.assertEqual(c.mmu.read(0x200), True) 84 | self.assertEqual(c.mmu.read(0x201), True) 85 | self.assertEqual(c.mmu.read(0x202), True) 86 | self.assertEqual(c.mmu.read(0x203), False) 87 | self.assertEqual(c.mmu.read(0x204), False) 88 | 89 | def test_lte(self): 90 | c = self._compile_and_run("lte.py") 91 | 92 | self.assertEqual(c.mmu.read(0x200), False) 93 | self.assertEqual(c.mmu.read(0x201), False) 94 | self.assertEqual(c.mmu.read(0x202), False) 95 | self.assertEqual(c.mmu.read(0x203), True) 96 | self.assertEqual(c.mmu.read(0x204), True) 97 | 98 | self.assertEqual(c.mmu.read(0x205), False) 99 | self.assertEqual(c.mmu.read(0x206), False) 100 | self.assertEqual(c.mmu.read(0x207), True) 101 | self.assertEqual(c.mmu.read(0x208), True) 102 | self.assertEqual(c.mmu.read(0x209), True) 103 | 104 | self.assertEqual(c.mmu.read(0x20a), True) 105 | self.assertEqual(c.mmu.read(0x20b), False) 106 | self.assertEqual(c.mmu.read(0x20c), True) 107 | self.assertEqual(c.mmu.read(0x20d), False) 108 | self.assertEqual(c.mmu.read(0x20e), True) 109 | 110 | 111 | def test_list(self): 112 | c = self._compile_and_run("list.py") 113 | print c.mmu.read(0x200) 114 | print c.mmu.read(0x201) 115 | print c.mmu.read(0x202) 116 | print c.mmu.read(0x203) 117 | print c.mmu.read(0x204) 118 | print c.mmu.read(0x205) 119 | print c.mmu.read(0x206) 120 | print c.mmu.read(0x207) 121 | print c.mmu.read(0x200) 122 | 123 | self.assertEqual(c.mmu.read(0x200), 1) 124 | self.assertEqual(c.mmu.read(0x201), 2) 125 | self.assertEqual(c.mmu.read(0x202), 3) 126 | self.assertEqual(c.mmu.read(0x203), 4) 127 | self.assertEqual(c.mmu.read(0x204), 5) 128 | 129 | #a, i 130 | self.assertEqual(c.mmu.read(0x206), 15) 131 | self.assertEqual(c.mmu.read(0x207), 0) 132 | 133 | self.assertEqual(c.mmu.read(0x205), 15) 134 | 135 | 136 | def test_while(self): 137 | c = self._compile_and_run("while.py") 138 | 139 | self.assertEqual(c.mmu.read(0x200), 2) 140 | self.assertEqual(c.mmu.read(0x201), 64) 141 | 142 | def test_if(self): 143 | c = self._compile_and_run("if.py") 144 | 145 | self.assertEqual(c.mmu.read(0x200), 64) 146 | self.assertEqual(c.mmu.read(0x201), 20) 147 | 148 | def test_addsub(self): 149 | c = self._compile_and_run("addsub.py") 150 | 151 | self.assertEqual(c.mmu.read(0x200), 2) 152 | self.assertEqual(c.mmu.read(0x201), 1) 153 | self.assertEqual(c.mmu.read(0x202), 0xff) 154 | self.assertEqual(c.mmu.read(0x203), 0) 155 | self.assertEqual(c.mmu.read(0x204), 1) 156 | 157 | def test_bool(self): 158 | c = self._compile_and_run("bool.py") 159 | 160 | self.assertEqual(c.mmu.read(0x200), True) 161 | self.assertEqual(c.mmu.read(0x201), False) 162 | self.assertEqual(c.mmu.read(0x202), False) 163 | self.assertEqual(c.mmu.read(0x203), True) 164 | self.assertEqual(c.mmu.read(0x204), False) 165 | self.assertEqual(c.mmu.read(0x205), 5) 166 | self.assertEqual(c.mmu.read(0x206), 4) 167 | self.assertEqual(c.mmu.read(0x207), 0) 168 | self.assertEqual(c.mmu.read(0x208), 5) 169 | self.assertEqual(c.mmu.read(0x209), 4) 170 | self.assertEqual(c.mmu.read(0x20a), 4) 171 | self.assertEqual(c.mmu.read(0x20b), 4) 172 | 173 | def test_mul(self): 174 | c = self._compile_and_run("mul.py") 175 | 176 | self.assertEqual(c.mmu.read(0x200), 25) 177 | self.assertEqual(c.mmu.read(0x201), 24) 178 | self.assertEqual(c.mmu.read(0x202), 0) 179 | self.assertEqual(c.mmu.read(0x203), 0) 180 | self.assertEqual(c.mmu.read(0x204), 0x10) 181 | self.assertEqual(c.mmu.read(0x205), 100) 182 | self.assertEqual(c.mmu.read(0x206), 105) 183 | 184 | def test_fib(self): 185 | c = self._compile_and_run("fib.py") 186 | 187 | self.assertEqual(c.mmu.read(0x200), 1) 188 | self.assertEqual(c.mmu.read(0x201), 1) 189 | self.assertEqual(c.mmu.read(0x202), 2) 190 | self.assertEqual(c.mmu.read(0x203), 3) 191 | self.assertEqual(c.mmu.read(0x204), 5) 192 | self.assertEqual(c.mmu.read(0x205), 8) 193 | self.assertEqual(c.mmu.read(0x206), 13) 194 | self.assertEqual(c.mmu.read(0x207), 21) 195 | self.assertEqual(c.mmu.read(0x208), 34) 196 | self.assertEqual(c.mmu.read(0x209), 55) 197 | 198 | self.assertEqual(c.mmu.read(0x20b), 8) 199 | 200 | def test_func(self): 201 | c = self._compile_and_run("func.py") 202 | 203 | print c.mmu.read(0x1ff) 204 | print c.mmu.read(0x1fe) 205 | print c.mmu.read(0x1fd) 206 | print c.mmu.read(0x1fc) 207 | print c.mmu.read(0x1fb) 208 | print c.mmu.read(0x1fa) 209 | print c.mmu.read(0x1f9) 210 | 211 | self.assertEqual(c.mmu.read(0x200), 3) 212 | self.assertEqual(c.mmu.read(0x201), 5) 213 | self.assertEqual(c.mmu.read(0x202), 6) 214 | self.assertEqual(c.mmu.read(0x203), 2) 215 | self.assertEqual(c.mmu.read(0x204), 5) 216 | self.assertEqual(c.mmu.read(0x205), 4) 217 | 218 | self.assertEqual(c.mmu.read(0x206), 9) 219 | self.assertEqual(c.mmu.read(0x207), 20) 220 | 221 | self.assertEqual(c.mmu.read(0x208), 7) 222 | self.assertEqual(c.mmu.read(0x209), 14) 223 | 224 | def test_recur(self): 225 | c = self._compile_and_run("recur.py") 226 | 227 | self.assertEqual(c.mmu.read(0x200), 3) 228 | self.assertEqual(c.mmu.read(0x201), 15) 229 | self.assertEqual(c.mmu.read(0x202), 6) 230 | def tearDown(self): 231 | pass 232 | 233 | if __name__ == '__main__': 234 | unittest.main() -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py26, py27, py33 3 | 4 | [testenv] 5 | setenv = 6 | PYTHONPATH = {toxinidir}:{toxinidir}/py65c 7 | commands = python setup.py test 8 | deps = 9 | -r{toxinidir}/requirements.txt --------------------------------------------------------------------------------