Esempio n. 1
0
	def __init__(self, source):
		self.numTemps = 0
		self.macros = []
		self.mlMacros = []
		for mem in dir(self):
			mem = getattr(self, mem)
			if isinstance(mem, type) and issubclass(mem, Macro):
				if issubclass(mem, MLMacro):
					self.mlMacros.append(mem(self))
				else:
					self.macros.append(mem(self))
		self.macros.sort()
		self.mlMacros.sort()
		
		tokens = tokenizer.tokenize(source)
		pprint.pprint(tokens)
		
		code = self.compile(tokens)
		pprint.pprint(code)
		
		code = Module(
				None,
				Stmt(code)
			)
		
		set_filename('<macropy>', code)
		self.compiled = ModuleCodeGenerator(code).getCode()
Esempio n. 2
0
    def _compile_call(self, text, attribute_handlers=None):
        import compiler
        import types
        from compiler import ast, misc, pycodegen

        raise NotImplementedError('Incomplete')

        # TODO Make this work?
        def _generate(node):
            if node.type == node.TERM:
                return ast.Compare(ast.Const(node.value),
                                   [('in', ast.Name('text'))])
            elif node.type == node.AND:
                return ast.And([_generate(node.left), _generate(node.right)])
            elif node.type == node.OR:
                return ast.Or([_generate(node.left), _generate(node.right)])
            elif node.type == node.NOT:
                return ast.Not(_generate(node.left))
            elif node.type == node.ATTR:
                raise NotImplementedError

        qast = ast.Expression(ast.Lambda(['self', 'text',
                                          'attribute_handlers'],
                                         [ast.Name('None')],
                                         0,
                                         _generate(self)))
        misc.set_filename('<%s compiled query>' % self.__class__.__name__,
                          qast)
        gen = pycodegen.ExpressionCodeGenerator(qast)
        self.__call__ = types.MethodType(eval(gen.getCode()), self, Query)
        return self.__call__(text)
Esempio n. 3
0
    def getCode(self, ast, mode='single', filename='<Subol>'):
        #print "ast is", ast
        misc.set_filename(filename, ast)
        # worklist = [ast]
        # while worklist:
        #     node = worklist.pop(0)
        #     node.filename = filename
        #     worklist.extend(node.getChildNodes())
        #     print "NODE CHILDREN ARE", node, worklist

        syntax.check(ast)
        d = {'single': pycodegen.InteractiveCodeGenerator,
             'exec': pycodegen.ModuleCodeGenerator,
             'eval': pycodegen.ExpressionCodeGenerator}
        try:
            gen = d[mode](ast)
            gen.graph.setFlag(consts.CO_GENERATOR_ALLOWED)
            code = gen.getCode()
            return code
        except:
            print "THERE WAS AN ERROR DURING COMPILATION."
            print "The ast with the problem was"
            print
            print ast
            print
            print "A diagnostic traceback follows."
            raise
Esempio n. 4
0
 def compile(self, code, filename="<string>"):
     tree = self.parser.parse(code)
     misc.set_filename(filename, tree)
     syntax.check(tree)
     gen = LucyModuleCodeGenerator(tree)
     code = gen.getCode()
     return code
Esempio n. 5
0
 def _get_tree(self):
     tree = self.parse()
     MutatingWalker.walk(tree, self.rm)
     if self.rm.errors:
         raise SyntaxError, self.rm.errors[0]
     misc.set_filename(self.filename, tree)
     syntax.check(tree)
     return tree
Esempio n. 6
0
 def compile(self, code, filename="<string>"):
     tree = self.parser.parse(code)
     #print  tree
     misc.set_filename(filename, tree)
     syntax.check(tree)
     
     #gen = pycodegen.ModuleCodeGenerator(tree)
     #pprint.pprint(tree)
     #code = gen.getCode()
     return tree
Esempio n. 7
0
 def compile(self, source, filename="<string>"):
   """
   Compile the template source into a code object suitable for execution
   with exec or eval.
   
   source - template (Python) source to compile.
   filename - the filename used in the compiled code.  This name will be
              used in tracebacks.
   """
   mod = compiler.parse(source)
   misc.set_filename(filename, mod)
   self.preorder(mod, self)
   generator = pycodegen.ModuleCodeGenerator(mod)
   code = generator.getCode()
   return code
Esempio n. 8
0
    def calculate(self, event, expression):
        for term in self.banned:
            if term in expression:
                return

        try:
            # We need to remove all power operators and replace with our limited pow
            # ast is the new method (Python >=2.6) compiler is the old
            ast = parse(expression, mode="eval")
            if transform_method == "ast":
                ast = PowSubstitutionTransformer().visit(ast)
                code = compile(ast, "<string>", "eval")
            else:
                misc.set_filename("<string>", ast)
                walk(ast, PowSubstitutionWalker())
                code = FD_ExpressionCodeGenerator(ast).getCode()

            result = eval(code, {"__builtins__": None}, self.safe)

        except ZeroDivisionError, e:
            event.addresponse(u"I can't divide by zero.")
            return
Esempio n. 9
0
 def _get_tree(self):
     tree = parse(self.source, self.mode)
     misc.set_filename(self.filename, tree)
     syntax.check(tree)
     return tree
Esempio n. 10
0
 def _get_tree(self):
     misc.set_filename(self.filename, self.tree)
     syntax.check(self.tree)
     return self.tree
Esempio n. 11
0
 def _get_tree(self):
     from compiler import misc, syntax
     tree = self.__get_ast()
     misc.set_filename(self.filename, tree)
     syntax.check(tree)
     return tree
Esempio n. 12
0
    text = open("sample.py").read()
    my_output = None
    try:
        import time
        t1 = time.time()
        my_tree = python_yacc.parse(text)
        t2 = time.time()
        print "Parse time", t2-t1
        my_output = str(my_tree)
    except NotImplementedError, x:
        import traceback
        traceback.print_exc()
    if my_output is None:
        raise SystemExit()

    import compiler
    from compiler import misc
    py_tree = compiler.parse(text)
    misc.set_filename(my_tree.filename, py_tree)
    py_output = str(py_tree)
    if my_output != py_output:
        print "*" * 60
        for i in range(len(py_output)):
            if py_output[i] != my_output[i]:
                print "Bad at", i
                print "my", repr(my_output[max(0, i-30):i]), repr(my_output[i:i+40])
                print "py", repr(py_output[max(0, i-30):i]), repr(py_output[i:i+40])
                raise SystemExit("fix it")
                break
    compare_trees("root", py_tree, my_tree, Output())
Esempio n. 13
0
 def _get_tree(self, transformer=None):
     tree = parse(self.source, self.mode, transformer=transformer)
     misc.set_filename(self.filename, tree)
     syntax.check(tree)
     return tree
Esempio n. 14
0
import imp
Esempio n. 15
0
 def _get_tree(self):
     from compiler import misc, syntax
     tree = self.__get_ast()
     misc.set_filename(self.filename, tree)
     syntax.check(tree)
     return tree
Esempio n. 16
0
 def _get_tree(self):
     tree = parse(self.source, self.filename)
     misc.set_filename(self.filename, tree)
     syntax.check(tree)
     return tree
Esempio n. 17
0
import imp