comparison python/c3/astnodes.py @ 230:88a1e0baef65

Added some tests for IR-code
author Windel Bouwman
date Sat, 13 Jul 2013 19:53:44 +0200
parents 7f18ed9b6b7e
children e41e4109addd
comparison
equal deleted inserted replaced
229:51d5ed1bd503 230:88a1e0baef65
63 assert isinstance(ptype, Type) or isinstance(ptype, Designator) 63 assert isinstance(ptype, Type) or isinstance(ptype, Designator)
64 self.ptype = ptype 64 self.ptype = ptype
65 def __repr__(self): 65 def __repr__(self):
66 return '({}*)'.format(self.ptype) 66 return '({}*)'.format(self.ptype)
67 67
68
68 class StructField: 69 class StructField:
69 def __init__(self, name, typ): 70 def __init__(self, name, typ):
70 self.name = name 71 self.name = name
71 self.typ = typ 72 self.typ = typ
73 self.offset = 0
74
72 75
73 class StructureType(Type): 76 class StructureType(Type):
74 def __init__(self, mems): 77 def __init__(self, mems):
75 self.mems = mems 78 self.mems = mems
76 for mem in mems: 79 for mem in mems:
77 assert type(mem) is StructField 80 assert type(mem) is StructField
78 assert type(mem.name) is str 81 assert type(mem.name) is str
82
79 def hasField(self, name): 83 def hasField(self, name):
80 for mem in self.mems: 84 for mem in self.mems:
81 if name == mem.name: 85 if name == mem.name:
82 return True 86 return True
83 return False 87 return False
88
84 def fieldType(self, name): 89 def fieldType(self, name):
90 return self.findField(name).typ
91
92 def fieldOffset(self, name):
93 return self.findField(name).offset
94
95 def findField(self, name):
85 for mem in self.mems: 96 for mem in self.mems:
86 if name == mem.name: 97 if name == mem.name:
87 return mem.typ 98 return mem
88 raise Exception() 99 raise KeyError(name)
89 100
101 def __repr__(self):
102 return 'STRUCT'
90 103
91 class DefinedType(Type): 104 class DefinedType(Type):
92 def __init__(self, name, typ, loc): 105 def __init__(self, name, typ, loc):
93 assert isinstance(name, str) 106 assert isinstance(name, str)
94 self.name = name 107 self.name = name
95 self.typ = typ 108 self.typ = typ
96 self.loc = loc 109 self.loc = loc
110
97 def __repr__(self): 111 def __repr__(self):
98 return 'Named type {0} of type {1}'.format(self.name, self.typ) 112 return 'Named type {0} of type {1}'.format(self.name, self.typ)
99 113
100 class TypeCast(Node):
101 def __init__(self, to_type, x):
102 self.to_type = to_type
103 self.a = x
104 def __repr__(self):
105 return 'TYPECAST'
106 114
107 # Variables, parameters, local variables, constants: 115 # Variables, parameters, local variables, constants:
108 class Symbol(Node): 116 class Symbol(Node):
109 def __init__(self, name): 117 def __init__(self, name):
110 self.name = name 118 self.name = name
145 def __repr__(self): 153 def __repr__(self):
146 return 'Func {}'.format(self.name) 154 return 'Func {}'.format(self.name)
147 155
148 # Operations / Expressions: 156 # Operations / Expressions:
149 class Expression(Node): 157 class Expression(Node):
150 pass 158 def __init__(self, loc):
159 self.loc = loc
160
151 161
152 class Deref(Expression): 162 class Deref(Expression):
153 def __init__(self, ptr, loc): 163 def __init__(self, ptr, loc):
164 super().__init__(loc)
154 assert isinstance(ptr, Expression) 165 assert isinstance(ptr, Expression)
155 self.ptr = ptr 166 self.ptr = ptr
156 self.loc = loc
157 def __repr__(self): 167 def __repr__(self):
158 return 'DEREF {}'.format(self.ptr) 168 return 'DEREF {}'.format(self.ptr)
169
170
171 class TypeCast(Expression):
172 def __init__(self, to_type, x, loc):
173 super().__init__(loc)
174 self.to_type = to_type
175 self.a = x
176 def __repr__(self):
177 return 'TYPECAST {}'.format(self.to_type)
178
159 179
160 class FieldRef(Expression): 180 class FieldRef(Expression):
161 def __init__(self, base, field, loc): 181 def __init__(self, base, field, loc):
182 super().__init__(loc)
162 assert isinstance(base, Expression) 183 assert isinstance(base, Expression)
163 assert isinstance(field, str) 184 assert isinstance(field, str)
164 self.base = base 185 self.base = base
165 self.field = field 186 self.field = field
166 self.loc = loc
167 def __repr__(self): 187 def __repr__(self):
168 return 'FIELD {}.{}'.format(self.base, self.field) 188 return 'FIELD {}.{}'.format(self.base, self.field)
189
169 190
170 class Unop(Expression): 191 class Unop(Expression):
171 def __init__(self, op, a, loc): 192 def __init__(self, op, a, loc):
193 super().__init__(loc)
172 assert isinstance(a, Expression) 194 assert isinstance(a, Expression)
173 assert isinstance(op, str) 195 assert isinstance(op, str)
174 self.a = a 196 self.a = a
175 self.op = op 197 self.op = op
176 self.loc = loc
177 def __repr__(self): 198 def __repr__(self):
178 return 'UNOP {}'.format(self.op) 199 return 'UNOP {}'.format(self.op)
179 200
180 class Binop(Expression): 201 class Binop(Expression):
181 def __init__(self, a, op, b, loc): 202 def __init__(self, a, op, b, loc):
203 super().__init__(loc)
182 assert isinstance(a, Expression), type(a) 204 assert isinstance(a, Expression), type(a)
183 assert isinstance(b, Expression) 205 assert isinstance(b, Expression)
184 assert isinstance(op, str) 206 assert isinstance(op, str)
185 self.a = a 207 self.a = a
186 self.b = b 208 self.b = b
187 self.op = op # Operation: '+', '-', '*', '/', 'mod' 209 self.op = op # Operation: '+', '-', '*', '/', 'mod'
188 self.loc = loc
189 210
190 def __repr__(self): 211 def __repr__(self):
191 return 'BINOP {}'.format(self.op) 212 return 'BINOP {}'.format(self.op)
192 213
193 class VariableUse(Expression): 214 class VariableUse(Expression):
194 def __init__(self, target, loc): 215 def __init__(self, target, loc):
216 super().__init__(loc)
195 self.target = target 217 self.target = target
196 self.loc = loc
197 def __repr__(self): 218 def __repr__(self):
198 nm = self.target 219 nm = self.target
199 return 'VAR USE {}'.format(nm) 220 return 'VAR USE {}'.format(nm)
200 221
201 class Literal(Expression): 222 class Literal(Expression):
202 def __init__(self, val, loc): 223 def __init__(self, val, loc):
224 super().__init__(loc)
203 self.val = val 225 self.val = val
204 self.loc = loc
205 def __repr__(self): 226 def __repr__(self):
206 return 'LITERAL {}'.format(self.val) 227 return 'LITERAL {}'.format(self.val)
207 228
208 class FunctionCall(Expression): 229 class FunctionCall(Expression):
209 def __init__(self, proc, args, loc): 230 def __init__(self, proc, args, loc):
231 super().__init__(loc)
210 self.proc = proc 232 self.proc = proc
211 self.args = args 233 self.args = args
212 self.loc = loc
213 def __repr__(self): 234 def __repr__(self):
214 return 'CALL {0} '.format(self.proc) 235 return 'CALL {0} '.format(self.proc)
215 236
216 # Statements 237 # Statements
217 class Statement(Node): 238 class Statement(Node):