Mercurial > lcfOS
comparison python/c3/typecheck.py @ 226:240111e0456f
Work on named types
author | Windel Bouwman |
---|---|
date | Fri, 12 Jul 2013 17:25:31 +0200 |
parents | 1c7364bd74c7 |
children | 82dfe6a32717 |
comparison
equal
deleted
inserted
replaced
225:1c7364bd74c7 | 226:240111e0456f |
---|---|
1 from .astnodes import * | 1 from .astnodes import * |
2 from .scope import * | 2 from .scope import * |
3 from .visitor import Visitor | 3 from .visitor import Visitor |
4 | 4 |
5 def equalTypes(a, b): | 5 def equalTypes(a, b): |
6 """ Compare types a and b for equality. Not equal until proven otherwise. """ | 6 """ Compare types a and b for equality. Not equal until proven otherwise. """ |
7 if type(a) is type(b): | 7 # Recurse into named types: |
8 if type(a) is BaseType: | 8 if type(a) is DefinedType: |
9 return a.name == b.name | 9 return equalTypes(a.typ, b) |
10 elif type(a) is PointerType: | 10 if type(b) is DefinedType: |
11 return equalTypes(a.ptype, b.ptype) | 11 return equalTypes(a, b.typ) |
12 return False | 12 # Compare for structural equivalence: |
13 if type(a) is type(b): | |
14 if type(a) is BaseType: | |
15 return a.name == b.name | |
16 elif type(a) is PointerType: | |
17 return equalTypes(a.ptype, b.ptype) | |
18 return False | |
13 | 19 |
14 def canCast(fromT, toT): | 20 def canCast(fromT, toT): |
15 if isinstance(fromT, PointerType) and isinstance(toT, PointerType): | 21 if isinstance(fromT, PointerType) and isinstance(toT, PointerType): |
16 return True | 22 return True |
17 elif fromT is intType and isinstance(toT, PointerType): | 23 elif fromT is intType and isinstance(toT, PointerType): |
87 raise Exception('Unknown unop {0}'.format(sym.op)) | 93 raise Exception('Unknown unop {0}'.format(sym.op)) |
88 elif type(sym) is Deref: | 94 elif type(sym) is Deref: |
89 # pointer deref | 95 # pointer deref |
90 sym.lvalue = True | 96 sym.lvalue = True |
91 # check if the to be dereferenced variable is a pointer type: | 97 # check if the to be dereferenced variable is a pointer type: |
92 if type(sym.ptr.typ) is PointerType: | 98 ptype = sym.ptr.typ |
93 sym.typ = sym.ptr.typ.ptype | 99 if type(ptype) is DefinedType: |
100 ptype = ptype.typ | |
101 if type(ptype) is PointerType: | |
102 sym.typ = ptype.ptype | |
94 else: | 103 else: |
95 self.error('Cannot dereference non-pointer type {}'.format(sym.ptr.typ), sym.loc) | 104 self.error('Cannot dereference non-pointer type {}'.format(ptype), sym.loc) |
96 sym.typ = intType | 105 sym.typ = intType |
97 elif type(sym) is FieldRef: | 106 elif type(sym) is FieldRef: |
98 basetype = sym.base.typ | 107 basetype = sym.base.typ |
99 sym.lvalue = True | 108 sym.lvalue = True |
109 if type(basetype) is DefinedType: | |
110 basetype = basetype.typ | |
100 if type(basetype) is StructureType: | 111 if type(basetype) is StructureType: |
101 if basetype.hasField(sym.field): | 112 if basetype.hasField(sym.field): |
102 sym.typ = basetype.fieldType(sym.field) | 113 sym.typ = basetype.fieldType(sym.field) |
103 else: | 114 else: |
104 self.error('{} does not contain field {}'.format(basetype, symfield), sym.loc) | 115 self.error('{} does not contain field {}'.format(basetype, sym.field), sym.loc) |
105 sym.typ = intType | 116 sym.typ = intType |
106 else: | 117 else: |
107 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc) | 118 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc) |
108 sym.typ = intType | 119 sym.typ = intType |
109 elif type(sym) is Binop: | 120 elif type(sym) is Binop: |