comparison python/c3/astnodes.py @ 225:1c7364bd74c7

Fixed pointer deref
author Windel Bouwman
date Thu, 11 Jul 2013 07:42:30 +0200
parents c3f1ce8b638f
children 240111e0456f
comparison
equal deleted inserted replaced
224:5af52987f5bd 225:1c7364bd74c7
57 params = ', '.join([str(v) for v in self.parametertypes]) 57 params = ', '.join([str(v) for v in self.parametertypes])
58 return '{1} f({0})'.format(params, self.returntype) 58 return '{1} f({0})'.format(params, self.returntype)
59 59
60 class PointerType(Type): 60 class PointerType(Type):
61 def __init__(self, ptype): 61 def __init__(self, ptype):
62 assert isinstance(ptype, Type) or isinstance(ptype, Designator)
62 self.ptype = ptype 63 self.ptype = ptype
63 def __repr__(self): 64 def __repr__(self):
64 return '({}*)'.format(self.ptype) 65 return '({}*)'.format(self.ptype)
65 66
66 class StructureType(Type): 67 class StructureType(Type):
67 def __init__(self, mems): 68 def __init__(self, mems):
68 self.mems = mems 69 self.mems = mems
70 def hasField(self, name):
71 for fn, ft in self.mems:
72 if name == fn:
73 return True
74 return False
75 def fieldType(self, name):
76 for fn, ft in self.mems:
77 if name == fn:
78 return ft
79 raise Exception()
80
69 81
70 class DefinedType(Type): 82 class DefinedType(Type):
71 def __init__(self, name, typ): 83 def __init__(self, name, typ, loc):
72 self.name = name 84 assert isinstance(name, str)
73 self.typ = typ 85 self.name = name
74 def __repr__(self): 86 self.typ = typ
75 return 'Named type {0} of type {1}'.format(self.name, self.typ) 87 self.loc = loc
88 def __repr__(self):
89 return 'Named type {0} of type {1}'.format(self.name, self.typ)
76 90
77 class TypeCast(Node): 91 class TypeCast(Node):
78 def __init__(self, to_type, x): 92 def __init__(self, to_type, x):
79 self.to_type = to_type 93 self.to_type = to_type
80 self.a = x 94 self.a = x
107 self.ival = None 121 self.ival = None
108 self.isLocal = False 122 self.isLocal = False
109 self.isReadOnly = False 123 self.isReadOnly = False
110 self.isParameter = False 124 self.isParameter = False
111 def __repr__(self): 125 def __repr__(self):
112 return 'Var {}'.format(self.name) 126 return 'Var {} [{}]'.format(self.name, self.typ)
113 127
114 # Procedure types 128 # Procedure types
115 class Function(Symbol): 129 class Function(Symbol):
116 """ Actual implementation of a function """ 130 """ Actual implementation of a function """
117 def __init__(self, name, loc): 131 def __init__(self, name, loc):
123 return 'Func {}'.format(self.name) 137 return 'Func {}'.format(self.name)
124 138
125 # Operations / Expressions: 139 # Operations / Expressions:
126 class Expression(Node): 140 class Expression(Node):
127 pass 141 pass
142
143 class Deref(Expression):
144 def __init__(self, ptr, loc):
145 assert isinstance(ptr, Expression)
146 self.ptr = ptr
147 self.loc = loc
148 def __repr__(self):
149 return 'DEREF {}'.format(self.ptr)
150
151 class FieldRef(Expression):
152 def __init__(self, base, field, loc):
153 assert isinstance(base, Expression)
154 assert isinstance(field, str)
155 self.base = base
156 self.field = field
157 self.loc = loc
158 def __repr__(self):
159 return 'FIELD {}.{}'.format(self.base, self.field)
128 160
129 class Unop(Expression): 161 class Unop(Expression):
130 def __init__(self, op, a, loc): 162 def __init__(self, op, a, loc):
131 assert isinstance(a, Expression) 163 assert isinstance(a, Expression)
132 assert isinstance(op, str) 164 assert isinstance(op, str)