forked from marcelogdeandrade/PythonCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.py
57 lines (43 loc) · 1.5 KB
/
ast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from llvmlite import ir
class Number():
def __init__(self, builder, module, value):
self.builder = builder
self.module = module
self.value = value
def eval(self):
i = ir.Constant(ir.IntType(8), int(self.value))
return i
class BinaryOp():
def __init__(self, builder, module, left, right):
self.builder = builder
self.module = module
self.left = left
self.right = right
class Sum(BinaryOp):
def eval(self):
i = self.builder.add(self.left.eval(), self.right.eval())
return i
class Sub(BinaryOp):
def eval(self):
i = self.builder.sub(self.left.eval(), self.right.eval())
return i
class Print():
def __init__(self, builder, module, printf, value):
self.builder = builder
self.module = module
self.printf = printf
self.value = value
def eval(self):
value = self.value.eval()
# Declare argument list
voidptr_ty = ir.IntType(8).as_pointer()
fmt = "%i \n\0"
c_fmt = ir.Constant(ir.ArrayType(ir.IntType(8), len(fmt)),
bytearray(fmt.encode("utf8")))
global_fmt = ir.GlobalVariable(self.module, c_fmt.type, name="fstr")
global_fmt.linkage = 'internal'
global_fmt.global_constant = True
global_fmt.initializer = c_fmt
fmt_arg = self.builder.bitcast(global_fmt, voidptr_ty)
# Call Print Function
self.builder.call(self.printf, [fmt_arg, value])