Mercurial > lcfOS
annotate python/c3/analyse.py @ 222:c3f1ce8b638f
Fixup of parser
author | Windel Bouwman |
---|---|
date | Tue, 09 Jul 2013 17:36:31 +0200 |
parents | 3f6c30a5d234 |
children | 1c7364bd74c7 |
rev | line source |
---|---|
163 | 1 from .visitor import Visitor |
2 from .astnodes import * | |
215 | 3 from .scope import Scope, topScope |
150 | 4 |
5 class Analyzer: | |
215 | 6 """ |
7 Context handling is done here. | |
8 Scope is attached to the correct modules. | |
9 This class checks names and references | |
10 """ | |
11 def __init__(self, diag): | |
12 self.diag = diag | |
186 | 13 |
215 | 14 def analyzePackage(self, pkg): |
186 | 15 self.ok = True |
215 | 16 visitor = Visitor() |
17 # Prepare top level scope: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
18 self.scopeStack = [topScope] |
215 | 19 visitor.visit(pkg, self.enterScope, self.quitScope) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
20 del self.scopeStack |
215 | 21 visitor.visit(pkg, self.findRefs) |
22 visitor.visit(pkg, self.sanity) | |
186 | 23 return self.ok |
215 | 24 |
25 def error(self, msg, loc=None): | |
26 self.ok = False | |
27 self.diag.error(msg, loc) | |
28 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
29 @property |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
30 def currentScope(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
31 return self.scopeStack[-1] |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
32 |
215 | 33 # Scope creation: |
34 def addSymbol(self, sym): | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
35 if self.currentScope.hasSymbol(sym.name): |
215 | 36 self.error('Redefinition of {0}'.format(sym.name), sym.loc) |
37 else: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
38 self.currentScope.addSymbol(sym) |
215 | 39 |
40 def enterScope(self, sym): | |
41 # Distribute the scope: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
42 sym.scope = self.currentScope |
215 | 43 |
44 # Add symbols to current scope: | |
45 if isinstance(sym, Symbol): | |
46 self.addSymbol(sym) | |
47 | |
48 # Create subscope: | |
49 if type(sym) in [Package, Function]: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
50 newScope = Scope(self.currentScope) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
51 self.scopeStack.append(newScope) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
52 sym.innerScope = self.currentScope |
215 | 53 |
54 def quitScope(self, sym): | |
55 # Pop out of scope: | |
56 if type(sym) in [Package, Function]: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
57 self.scopeStack.pop(-1) |
150 | 58 |
215 | 59 # Reference fixups: |
60 def resolveDesignator(self, d, scope): | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
61 assert type(d) is Designator, type(d) |
215 | 62 assert type(scope) is Scope |
63 if scope.hasSymbol(d.tname): | |
64 s = scope.getSymbol(d.tname) | |
65 if hasattr(s, 'addRef'): | |
66 # TODO: make this nicer | |
67 s.addRef(None) | |
68 return s | |
69 else: | |
70 self.ok = False | |
71 msg = 'Cannot resolve name {0}'.format(d.tname) | |
72 self.diag.error(msg, d.loc) | |
73 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
74 def resolveType(self, t, scope): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
75 # TODO: what about structs? |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
76 if type(t) is PointerType: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
77 t.ptype = self.resolveType(t.ptype, scope) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
78 return t |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
79 elif type(t) is Designator: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
80 return self.resolveDesignator(t, scope) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
81 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
82 print('ERR resolve type!') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
83 |
215 | 84 def findRefs(self, sym): |
85 if type(sym) in [Variable, Constant]: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
86 sym.typ = self.resolveType(sym.typ, sym.scope) |
222 | 87 elif type(sym) is TypeCast: |
88 sym.to_type = self.resolveType(sym.to_type, sym.scope) | |
215 | 89 elif type(sym) is VariableUse: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
90 sym.target = self.resolveDesignator(sym.target, sym.scope) |
215 | 91 elif type(sym) is FunctionCall: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
92 varuse = sym.proc |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
93 sym.proc = self.resolveDesignator(varuse.target, sym.scope) |
215 | 94 elif type(sym) is Function: |
95 # Checkup function type: | |
96 ft = sym.typ | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
97 ft.returntype = self.resolveType(ft.returntype, sym.scope) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
98 ft.parametertypes = [self.resolveType(pt, sym.scope) for pt in ft.parametertypes] |
215 | 99 |
100 def sanity(self, sym): | |
101 if type(sym) is FunctionType: | |
102 pass | |
103 elif type(sym) is Function: | |
104 pass | |
105 |