Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/artiq
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6bf95397d798
Choose a base ref
...
head repository: m-labs/artiq
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 196acb37f640
Choose a head ref
  • 4 commits
  • 26 files changed
  • 1 contributor

Commits on Jul 2, 2015

  1. Initialize types.TBuiltin's attributes field.

    whitequark committed Jul 2, 2015
    Copy the full SHA
    86cdc84 View commit details
  2. Add region field to types.

    whitequark committed Jul 2, 2015
    Copy the full SHA
    1702251 View commit details
  3. 1
    Copy the full SHA
    7ce9bdf View commit details
  4. Add IntMonomorphizer.

    whitequark committed Jul 2, 2015
    Copy the full SHA
    196acb3 View commit details
Showing with 566 additions and 473 deletions.
  1. +1 −0 artiq/compiler/__init__.py
  2. +36 −0 artiq/compiler/module.py
  3. 0 artiq/compiler/testbench/__init__.py
  4. +75 −0 artiq/compiler/testbench/inferencer.py
  5. +3 −0 artiq/compiler/transforms/__init__.py
  6. +382 −0 artiq/compiler/transforms/asttyped_rewriter.py
  7. +8 −449 artiq/compiler/{typing.py → transforms/inferencer.py}
  8. +28 −0 artiq/compiler/transforms/int_monomorphizer.py
  9. +16 −7 artiq/compiler/types.py
  10. +1 −1 lit-test/compiler/{typing → inferencer}/builtin_calls.py
  11. +1 −1 lit-test/compiler/{typing → inferencer}/coerce.py
  12. +1 −1 lit-test/compiler/{typing → inferencer}/error_builtin_calls.py
  13. +1 −1 lit-test/compiler/{typing → inferencer}/error_call.py
  14. +1 −1 lit-test/compiler/{typing → inferencer}/error_coerce.py
  15. +1 −1 lit-test/compiler/{typing → inferencer}/error_control_flow.py
  16. +1 −1 lit-test/compiler/{typing → inferencer}/error_exception.py
  17. +1 −1 lit-test/compiler/{typing → inferencer}/error_iterable.py
  18. +1 −1 lit-test/compiler/{typing → inferencer}/error_local_unbound.py
  19. +1 −1 lit-test/compiler/{typing → inferencer}/error_locals.py
  20. +1 −1 lit-test/compiler/{typing → inferencer}/error_return.py
  21. +1 −1 lit-test/compiler/{typing → inferencer}/error_unify.py
  22. +1 −1 lit-test/compiler/{typing → inferencer}/exception.py
  23. +1 −1 lit-test/compiler/{typing → inferencer}/gcd.py
  24. +1 −1 lit-test/compiler/{typing → inferencer}/prelude.py
  25. +1 −1 lit-test/compiler/{typing → inferencer}/scoping.py
  26. +1 −1 lit-test/compiler/{typing → inferencer}/unify.py
1 change: 1 addition & 0 deletions artiq/compiler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .module import Module
36 changes: 36 additions & 0 deletions artiq/compiler/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
The :class:`Module` class encapsulates a single Python
"""

import os
from pythonparser import source, diagnostic, parse_buffer
from . import prelude, types, transforms

class Module:
def __init__(self, source_buffer, engine=diagnostic.Engine(all_errors_are_fatal=True)):
asttyped_rewriter = transforms.ASTTypedRewriter(engine=engine)
inferencer = transforms.Inferencer(engine=engine)
int_monomorphizer = transforms.IntMonomorphizer(engine=engine)

parsetree, comments = parse_buffer(source_buffer, engine=engine)
typedtree = asttyped_rewriter.visit(parsetree)
inferencer.visit(typedtree)
int_monomorphizer.visit(typedtree)
inferencer.visit(typedtree)

self.name = os.path.basename(source_buffer.name)
self.globals = asttyped_rewriter.globals

@classmethod
def from_string(klass, source_string, name="input.py", first_line=1):
return klass(source.Buffer(source_string + "\n", name, first_line))

@classmethod
def from_filename(klass, filename):
with open(filename) as f:
return klass(source.Buffer(f.read(), filename, 1))

def __repr__(self):
printer = types.TypePrinter()
globals = ["%s: %s" % (var, printer.name(self.globals[var])) for var in self.globals]
return "<artiq.compiler.Module %s {\n %s\n}>" % (repr(self.name), ",\n ".join(globals))
Empty file.
75 changes: 75 additions & 0 deletions artiq/compiler/testbench/inferencer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import sys, fileinput, os
from pythonparser import source, diagnostic, algorithm, parse_buffer
from .. import prelude, types
from ..transforms import ASTTypedRewriter, Inferencer

class Printer(algorithm.Visitor):
"""
:class:`Printer` prints ``:`` and the node type after every typed node,
and ``->`` and the node type before the colon in a function definition.
In almost all cases (except function definition) this does not result
in valid Python syntax.
:ivar rewriter: (:class:`pythonparser.source.Rewriter`) rewriter instance
"""

def __init__(self, buf):
self.rewriter = source.Rewriter(buf)
self.type_printer = types.TypePrinter()

def rewrite(self):
return self.rewriter.rewrite()

def visit_FunctionDefT(self, node):
super().generic_visit(node)

self.rewriter.insert_before(node.colon_loc,
"->{}".format(self.type_printer.name(node.return_type)))

def visit_ExceptHandlerT(self, node):
super().generic_visit(node)

if node.name_loc:
self.rewriter.insert_after(node.name_loc,
":{}".format(self.type_printer.name(node.name_type)))

def generic_visit(self, node):
super().generic_visit(node)

if hasattr(node, "type"):
self.rewriter.insert_after(node.loc,
":{}".format(self.type_printer.name(node.type)))

def main():
if len(sys.argv) > 1 and sys.argv[1] == '+diag':
del sys.argv[1]
def process_diagnostic(diag):
print("\n".join(diag.render(only_line=True)))
if diag.level == 'fatal':
exit()
else:
def process_diagnostic(diag):
print("\n".join(diag.render()))
if diag.level in ('fatal', 'error'):
exit(1)

engine = diagnostic.Engine()
engine.process = process_diagnostic

buf = source.Buffer("".join(fileinput.input()).expandtabs(),
os.path.basename(fileinput.filename()))
parsed, comments = parse_buffer(buf, engine=engine)
typed = ASTTypedRewriter(globals=prelude.globals(), engine=engine).visit(parsed)
Inferencer(engine=engine).visit(typed)

printer = Printer(buf)
printer.visit(typed)
for comment in comments:
if comment.text.find("CHECK") >= 0:
printer.rewriter.remove(comment.loc)
print(printer.rewrite().source)


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions artiq/compiler/transforms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .asttyped_rewriter import ASTTypedRewriter
from .inferencer import Inferencer
from .int_monomorphizer import IntMonomorphizer
Loading