comparison python/c3/astnodes.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents 9683a4cd848f
children 598d3888a11c
comparison
equal deleted inserted replaced
162:d8c735dc31f9 163:8104fc8b5e90
1 """ 1 """
2 AST nodes for the c3 language. 2 AST nodes for the c3 language.
3 """ 3 """
4 4
5 class Node: 5 class Node:
6 location = None 6 pass
7 def getChildren(self):
8 children = []
9 members = dir(self)
10 for member in members:
11 member = getattr(self, member)
12 if isinstance(member, Node):
13 children.append(member)
14 elif type(member) is list:
15 for mi in member:
16 if isinstance(mi, Node):
17 children.append(mi)
18 return children
19 7
20 class Designator(Node): 8 class Designator(Node):
21 def __init__(self, tname): 9 def __init__(self, tname):
22 self.tname = tname 10 self.tname = tname
23 def __repr__(self): 11 def __repr__(self):
24 return 'DESIGNATOR {0}'.format(self.tname) 12 return 'DESIGNATOR {0}'.format(self.tname)
25 13
26 """ 14 """
27 Type classes 15 Type classes
28 """ 16 """
29 def isType(a, b):
30 """ Compare types a and b and check if they are equal """
31 if type(a) is type(b):
32 if type(a) is BaseType:
33 return (a.name == b.name) and (a.size == b.size)
34 elif type(a) is ProcedureType:
35 if len(a.parameters) != len(b.parameters):
36 print('Number of parameters does not match')
37 return False
38 for aparam, bparam in zip(a.parameters, b.parameters):
39 if not isType(aparam.typ, bparam.typ):
40 print('Parameter {0} does not match parameter {1}'.format(aparam, bparam))
41 return False
42 if a.result is None:
43 # TODO: how to handle a None return type??
44 pass
45 if not isType(a.result, b.result):
46 print('Procedure return value mismatch {0} != {1}'.format(a.result, b.result))
47 return False
48 return True
49 else:
50 print(a)
51 print(b)
52 Error('Not implemented {0}'.format(a))
53 else:
54 return False
55 17
56 class Type(Node): 18 class Type(Node):
57 def isType(self, b): 19 def isType(self, b):
58 return isType(self, b) 20 return isType(self, b)
59 21
66 class FunctionType(Type): 28 class FunctionType(Type):
67 def __init__(self, parameters, returntype): 29 def __init__(self, parameters, returntype):
68 self.parameters = parameters 30 self.parameters = parameters
69 self.returntype = returntype 31 self.returntype = returntype
70 def __repr__(self): 32 def __repr__(self):
71 return '[PROCTYPE {0} RET {1}]'.format(self.parameters, self.returntype) 33 return '[FUNCTYPE {0} RET {1}]'.format(self.parameters, self.returntype)
72 34
73 class DefinedType(Type): 35 class DefinedType(Type):
74 def __init__(self, name, typ): 36 def __init__(self, name, typ):
75 self.name = name 37 self.name = name
76 self.typ = typ 38 self.typ = typ
77 def __repr__(self): 39 def __repr__(self):
78 return 'Named type {0} of type {1}'.format(self.name, self.typ) 40 return 'Named type {0} of type {1}'.format(self.name, self.typ)
79 41
80 # Variables, parameters, local variables, constants: 42 # Variables, parameters, local variables, constants:
81 class Symbol(Node): 43 class Symbol(Node):
82 pass 44 def __init__(self, name):
45 self.name = name
46 self.refs = []
47 def addRef(self, r):
48 self.refs.append(r)
49 @property
50 def References(self):
51 return self.refs
83 52
84 class Constant(Symbol): 53 class Constant(Symbol):
85 def __init__(self, value, name=None): 54 def __init__(self, name, typ, value):
86 self.name = name 55 super().__init__(name)
56 self.typ = typ
87 self.value = value 57 self.value = value
88 def __repr__(self): 58 def __repr__(self):
89 return 'CONSTANT {0} = {1}'.format(self.name, self.value) 59 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
90 60
91 class Variable(Symbol): 61 class Variable(Symbol):
92 def __init__(self, name, typ): 62 def __init__(self, name, typ, ival=None):
93 self.name = name 63 super().__init__(name)
94 self.typ = typ 64 self.typ = typ
65 self.ival = ival
95 self.isLocal = False 66 self.isLocal = False
96 self.isReadOnly = False 67 self.isReadOnly = False
97 self.isParameter = False 68 self.isParameter = False
98 def __repr__(self): 69 def __repr__(self):
99 return 'VAR {0} : {1}'.format(self.name, self.typ) 70 return 'VAR {0} : {1} usage: {2}'.format(self.name, self.typ, self.References)
100 71
101 # Procedure types 72 # Procedure types
102 class Function(Symbol): 73 class Function(Symbol):
103 """ Actual implementation of a function """ 74 """ Actual implementation of a function """
104 def __init__(self, name, typ=None, block=None): 75 def __init__(self, name, typ=None, block=None):
105 self.name = name 76 super().__init__(name)
106 self.body = block 77 self.body = block
107 self.typ = typ 78 self.typ = typ
108 def __repr__(self): 79 def __repr__(self):
109 return 'PROCEDURE {0} {1}'.format(self.name, self.typ) 80 return 'PROCEDURE {0} {1}'.format(self.name, self.typ)
110 81
111 # Operations: 82 # Operations / Expressions:
112 class Unop(Node): 83 class Unop(Node):
113 def __init__(self, a, op): 84 def __init__(self, a, op):
114 self.a = a 85 self.a = a
115 self.op = op 86 self.op = op
116 def __repr__(self): 87 def __repr__(self):
120 def __init__(self, a, op, b): 91 def __init__(self, a, op, b):
121 self.a = a 92 self.a = a
122 self.b = b 93 self.b = b
123 self.op = op # Operation: '+', '-', '*', '/', 'mod' 94 self.op = op # Operation: '+', '-', '*', '/', 'mod'
124 def __repr__(self): 95 def __repr__(self):
125 return 'BINOP {0}'.format(self.op) 96 typ = self.typ if hasattr(self, 'typ') else ''
97 return 'BINOP {0} {1}'.format(self.op, typ)
126 98
127 class VariableUse(Node): 99 class VariableUse(Node):
128 def __init__(self, target): 100 def __init__(self, target):
129 self.target = target 101 self.target = target
130 def __repr__(self): 102 def __repr__(self):
131 return 'VAR USE {0}'.format(self.target) 103 nm = self.target.name if hasattr(self.target, 'name') else ''
104 return 'VAR USE {0}'.format(nm)
105
106 class Literal(Node):
107 def __init__(self, val):
108 self.val = val
109 def __repr__(self):
110 return 'LITERAL {0}'.format(self.val)
132 111
133 # Modules 112 # Modules
134 class Package(Node): 113 class Package(Node):
135 def __init__(self, name): 114 def __init__(self, name):
136 self.name = name 115 self.name = name