Ejemplo n.º 1
0
 def test_def_args(self):
     self.parser = Parser()
     ast = self.parser.parse("def foo(x y) x + foo(y, 4.0)")
     self.assertIsInstance(ast, FunctionNode)
     self.assertEqual(ast.prototype.name, 'foo')
     self.assertEqual(ast.prototype.arguments, ['x', 'y'])
     # self.assertIsInstance(function.body, BinaryOperatorExpression)
     print(self.parser._flatten(ast))
Ejemplo n.º 2
0
 def test_basic(self):
     self.parser = Parser()
     number = 19
     ast = self.parser.parse(str(number))
     self.assertIsInstance(ast, FunctionNode)
     self.assertIsInstance(ast.body, NumberExpression)
     self.assertEqual(ast.body.value, number)
     print(self.parser._flatten(ast))
Ejemplo n.º 3
0
 def test_xyz(self):
     self.parser = Parser()
     ast = self.parser.parse("x+y*z")
     self.assertIsInstance(ast, FunctionNode)
     self.assertIsInstance(ast.body, BinaryOperatorExpression)
     self.assertEqual(self.parser._flatten(ast),
                      ['Function', ['Prototype', 'anonymous_2', ''],
                       ['Binop', '+', ['Variable', 'x'],
                        ['Binop', '*', ['Variable', 'y'], ['Variable', 'z']]]])
     print(self.parser._flatten(ast))
Ejemplo n.º 4
0
    def __init__(self):
        llvm.initialize()
        llvm.initialize_native_target()
        llvm.initialize_native_asmprinter()

        self.parser = Parser()
        self.generator = LLVMGenerator()
        self._add_builtins(self.generator.module)

        self.target = llvm.Target.from_default_triple()
Ejemplo n.º 5
0
class Evaluator:
    def __init__(self):
        llvm.initialize()
        llvm.initialize_native_target()
        llvm.initialize_native_asmprinter()

        self.parser = Parser()
        self.generator = LLVMGenerator()
        self._add_builtins(self.generator.module)

        self.target = llvm.Target.from_default_triple()

    def _add_builtins(self, module):
        import llvmlite.ir as ir
        # Add the declaration of putchar
        putchar_ty = ir.FunctionType(ir.IntType(32), [ir.IntType(32)])
        putchar = ir.Function(module, putchar_ty, 'putchar')

        # Add putchard
        putchard_ty = ir.FunctionType(ir.DoubleType(), [ir.DoubleType()])
        putchard = ir.Function(module, putchard_ty, 'putchard')
        irbuilder = ir.IRBuilder(putchard.append_basic_block('entry'))
        ival = irbuilder.fptoui(putchard.args[0], ir.IntType(32), 'intcast')
        irbuilder.call(putchar, [ival])
        irbuilder.ret(ir.Constant(ir.DoubleType(), 0))

    def evaluate(self, string, optimize=True):
        ast = self.parser.parse(string)
        self.generator.generate_llvm(ast)
        if not (isinstance(ast, FunctionNode) and ast.is_anonymous()):
            return None

        # print("-------------- Generated -------------------")
        # print(str(self.generator.module))
        llvm_mod = llvm.parse_assembly(str(self.generator.module))

        if optimize:
            pmb = llvm.create_pass_manager_builder()
            pmb.opt_level = 2
            pm = llvm.create_module_pass_manager()
            pmb.populate(pm)
            pm.run(llvm_mod)
            # print("-------------- Optimized -------------------")
            # print(str(llvm_mod))

        target_machine = self.target.create_target_machine()
        with llvm.create_mcjit_compiler(llvm_mod, target_machine) as ee:
            ee.finalize_object()

            func = llvm_mod.get_function(ast.prototype.name)
            fptr = CFUNCTYPE(c_double)(ee.get_pointer_to_function(func))

            result = fptr()
            return result
Ejemplo n.º 6
0
class TestParser(unittest.TestCase):
    def test_basic(self):
        self.parser = Parser()
        number = 19
        ast = self.parser.parse(str(number))
        self.assertIsInstance(ast, FunctionNode)
        self.assertIsInstance(ast.body, NumberExpression)
        self.assertEqual(ast.body.value, number)
        print(self.parser._flatten(ast))

    def test_xyz(self):
        self.parser = Parser()
        ast = self.parser.parse("x+y*z")
        self.assertIsInstance(ast, FunctionNode)
        self.assertIsInstance(ast.body, BinaryOperatorExpression)
        self.assertEqual(self.parser._flatten(ast),
                         ['Function', ['Prototype', 'anonymous_2', ''],
                          ['Binop', '+', ['Variable', 'x'],
                           ['Binop', '*', ['Variable', 'y'], ['Variable', 'z']]]])
        print(self.parser._flatten(ast))

    def test_def_args(self):
        self.parser = Parser()
        ast = self.parser.parse("def foo(x y) x + foo(y, 4.0)")
        self.assertIsInstance(ast, FunctionNode)
        self.assertEqual(ast.prototype.name, 'foo')
        self.assertEqual(ast.prototype.arguments, ['x', 'y'])
        # self.assertIsInstance(function.body, BinaryOperatorExpression)
        print(self.parser._flatten(ast))

    def test_extern(self):
        self.parser = Parser()
        ast = self.parser.parse("extern sin(arg)")
        self.assertIsInstance(ast, PrototypeNode)
        self.assertEqual(len(ast.arguments), 1)
        print(self.parser._flatten(ast))
Ejemplo n.º 7
0
 def test_extern(self):
     self.parser = Parser()
     ast = self.parser.parse("extern sin(arg)")
     self.assertIsInstance(ast, PrototypeNode)
     self.assertEqual(len(ast.arguments), 1)
     print(self.parser._flatten(ast))