annotate python/c3/astnodes.py @ 193:f091e7d70996

Added even more checks
author Windel Bouwman
date Sun, 26 May 2013 23:58:59 +0200
parents 46d62dadd61b
children 62386bcee1ba
rev   line source
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
1 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
2 AST nodes for the c3 language.
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
3 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
4
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
5 class Node:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
6 pass
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
7
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
8 class Designator(Node):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
9 def __init__(self, tname):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
10 self.tname = tname
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
11 def __repr__(self):
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
12 return 'DESIGNATOR {0}'.format(self.tname)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 Type classes
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
18 class Type(Node):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19 def isType(self, b):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
20 return isType(self, b)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
21
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
22 class BaseType(Type):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
23 def __init__(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
24 self.name = name
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
25 def __repr__(self):
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
26 return '{0}'.format(self.name)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28 class FunctionType(Type):
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
29 def __init__(self, parametertypes, returntype):
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
30 self.parametertypes = parametertypes
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
31 self.returntype = returntype
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32 def __repr__(self):
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
33 params = ', '.join([str(v) for v in self.parametertypes])
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
34 return '{1} f({0})'.format(params, self.returntype)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
35
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
36 class DefinedType(Type):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
37 def __init__(self, name, typ):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
38 self.name = name
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
39 self.typ = typ
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41 return 'Named type {0} of type {1}'.format(self.name, self.typ)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
43 # Variables, parameters, local variables, constants:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
44 class Symbol(Node):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
45 def __init__(self, name):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
46 self.name = name
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
47 self.refs = []
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
48 def addRef(self, r):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
49 self.refs.append(r)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
50 @property
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
51 def References(self):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
52 return self.refs
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
53
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
54 class Constant(Symbol):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
55 def __init__(self, name, typ, value):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
56 super().__init__(name)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
57 self.typ = typ
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
58 self.value = value
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
59 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
60 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
61
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
62 class Variable(Symbol):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
63 def __init__(self, name, typ, ival=None):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
64 super().__init__(name)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
65 self.typ = typ
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
66 self.ival = ival
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
67 self.isLocal = False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
68 self.isReadOnly = False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
69 self.isParameter = False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
70 def __repr__(self):
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
71 return '{0}'.format(self.name)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
72
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
73 # Procedure types
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
74 class Function(Symbol):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
75 """ Actual implementation of a function """
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 176
diff changeset
76 def __init__(self, name):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
77 super().__init__(name)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
78 def __repr__(self):
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
79 return '{0}'.format(self.name)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
80
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
81 # Operations / Expressions:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
82 class Unop(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
83 def __init__(self, a, op):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
84 self.a = a
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
85 self.op = op
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
86 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
87 return 'UNOP {0}'.format(self.op)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
88
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
89 class Binop(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
90 def __init__(self, a, op, b):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
91 self.a = a
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
92 self.b = b
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
93 self.op = op # Operation: '+', '-', '*', '/', 'mod'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
94 def __repr__(self):
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
95 return 'BINOP {0}'.format(self.op)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
96
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
97 class VariableUse(Node):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
98 def __init__(self, target):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
99 self.target = target
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
100 def __repr__(self):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
101 nm = self.target.name if hasattr(self.target, 'name') else ''
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
102 return 'VAR USE {0}'.format(nm)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
103
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
104 class Literal(Node):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
105 def __init__(self, val):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
106 self.val = val
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
107 def __repr__(self):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
108 return 'LITERAL {0}'.format(self.val)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
109
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
110 # Modules
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
111 class Package(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
112 def __init__(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
113 self.name = name
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
114 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
115 return 'PACKAGE {0}'.format(self.name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
116
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
117 # Statements
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
118 class CompoundStatement(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
119 def __init__(self, statements):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
120 self.statements = statements
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
121 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
122 return 'COMPOUND STATEMENT'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
123
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 150
diff changeset
124 class EmptyStatement(Node):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 155
diff changeset
125 def __repr__(self):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 155
diff changeset
126 return 'NOP'
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 150
diff changeset
127
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
128 class ReturnStatement(Node):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
129 def __init__(self, expr):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
130 self.expr = expr
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
131 def __repr__(self):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
132 return 'RETURN STATEMENT'
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
133
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
134 class Assignment(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
135 def __init__(self, lval, rval):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
136 self.lval = lval
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
137 self.rval = rval
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
138 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
139 return 'ASSIGNMENT'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
140
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
141 class FunctionCall(Node):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
142 def __init__(self, proc, args):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
143 self.proc = proc
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
144 self.args = args
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
145 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
146 return 'CALL {0} '.format(self.proc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
147
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
148 class IfStatement(Node):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 155
diff changeset
149 def __init__(self, condition, truestatement, falsestatement):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
150 self.condition = condition
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
151 self.truestatement = truestatement
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
152 self.falsestatement = falsestatement
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
153 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
154 return 'IF-statement'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
155
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
156 class WhileStatement(Node):
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 167
diff changeset
157 def __init__(self, condition, statement):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
158 self.condition = condition
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 167
diff changeset
159 self.dostatement = statement
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
160 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
161 return 'WHILE-statement'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
162