comparison python/c3/astnodes.py @ 272:e64bae57cda8

refactor ir
author Windel Bouwman
date Sat, 31 Aug 2013 17:58:54 +0200
parents 6ed3d3a82a63
children 05184b95fa16
comparison
equal deleted inserted replaced
271:cf7d5fb7d9c8 272:e64bae57cda8
40 - typedef type -> a named type indicating another type 40 - typedef type -> a named type indicating another type
41 - function type 41 - function type
42 """ 42 """
43 43
44 class Type(Node): 44 class Type(Node):
45 def isType(self, b): 45 def isType(self, b):
46 return isType(self, b) 46 return isType(self, b)
47
47 48
48 class BaseType(Type): 49 class BaseType(Type):
49 def __init__(self, name): 50 def __init__(self, name):
50 self.name = name 51 self.name = name
51 def __repr__(self): 52
52 return '{}'.format(self.name) 53 def __repr__(self):
54 return '{}'.format(self.name)
55
53 56
54 class FunctionType(Type): 57 class FunctionType(Type):
55 def __init__(self, parametertypes, returntype): 58 def __init__(self, parametertypes, returntype):
56 self.parametertypes = parametertypes 59 self.parametertypes = parametertypes
57 self.returntype = returntype 60 self.returntype = returntype
58 def __repr__(self): 61 def __repr__(self):
59 params = ', '.join([str(v) for v in self.parametertypes]) 62 params = ', '.join([str(v) for v in self.parametertypes])
60 return '{1} f({0})'.format(params, self.returntype) 63 return '{1} f({0})'.format(params, self.returntype)
61 64
65
62 class PointerType(Type): 66 class PointerType(Type):
63 def __init__(self, ptype): 67 def __init__(self, ptype):
64 assert isinstance(ptype, Type) or isinstance(ptype, Designator) 68 assert isinstance(ptype, Type) or isinstance(ptype, Designator)
65 self.ptype = ptype 69 self.ptype = ptype
66 def __repr__(self): 70 def __repr__(self):
100 raise KeyError(name) 104 raise KeyError(name)
101 105
102 def __repr__(self): 106 def __repr__(self):
103 return 'STRUCT' 107 return 'STRUCT'
104 108
109
105 class DefinedType(Type): 110 class DefinedType(Type):
106 def __init__(self, name, typ, loc): 111 def __init__(self, name, typ, loc):
107 assert isinstance(name, str) 112 assert isinstance(name, str)
108 self.name = name 113 self.name = name
109 self.typ = typ 114 self.typ = typ
114 119
115 120
116 # Variables, parameters, local variables, constants: 121 # Variables, parameters, local variables, constants:
117 class Symbol(Node): 122 class Symbol(Node):
118 def __init__(self, name): 123 def __init__(self, name):
119 self.name = name 124 self.name = name
120 self.refs = [] 125 self.refs = []
126
121 def addRef(self, r): 127 def addRef(self, r):
122 self.refs.append(r) 128 self.refs.append(r)
129
123 @property 130 @property
124 def References(self): 131 def References(self):
125 return self.refs 132 return self.refs
133
126 134
127 class Constant(Symbol): 135 class Constant(Symbol):
128 def __init__(self, name, typ, value): 136 def __init__(self, name, typ, value):
129 super().__init__(name) 137 super().__init__(name)
130 self.typ = typ 138 self.typ = typ
131 self.value = value 139 self.value = value
132 def __repr__(self): 140
133 return 'CONSTANT {0} = {1}'.format(self.name, self.value) 141 def __repr__(self):
142 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
143
134 144
135 class Variable(Symbol): 145 class Variable(Symbol):
136 def __init__(self, name, typ): 146 def __init__(self, name, typ):
137 super().__init__(name) 147 super().__init__(name)
138 self.typ = typ 148 self.typ = typ
139 self.ival = None 149 self.ival = None
140 self.isLocal = False 150 self.isLocal = False
141 self.isReadOnly = False 151 self.isReadOnly = False
142 self.isParameter = False 152 self.isParameter = False
143 def __repr__(self): 153
144 return 'Var {} [{}]'.format(self.name, self.typ) 154 def __repr__(self):
155 return 'Var {} [{}]'.format(self.name, self.typ)
156
157
158 class LocalVariable(Variable):
159 def __init__(self, name, typ):
160 super().__init__(name, typ)
161 self.isLocal = True
162
163
164 class FormalParameter(Variable):
165 def __init__(self, name, typ):
166 super().__init__(name, typ)
167 self.isParameter = True
168
145 169
146 # Procedure types 170 # Procedure types
147 class Function(Symbol): 171 class Function(Symbol):
148 """ Actual implementation of a function """ 172 """ Actual implementation of a function """
149 def __init__(self, name, loc): 173 def __init__(self, name, loc):