annotate python/c3/astnodes.py @ 158:9683a4cd848f

Added some functions for code generation
author Windel Bouwman
date Fri, 08 Mar 2013 16:52:44 +0100
parents b28a11c01dbe
children 8104fc8b5e90
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:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
6 location = None
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
7 def getChildren(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
8 children = []
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
9 members = dir(self)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
10 for member in members:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
11 member = getattr(self, member)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12 if isinstance(member, Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13 children.append(member)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 elif type(member) is list:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 for mi in member:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 if isinstance(mi, Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 children.append(mi)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
18 return children
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
20 class Designator(Node):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
21 def __init__(self, tname):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
22 self.tname = tname
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
23 def __repr__(self):
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
24 return 'DESIGNATOR {0}'.format(self.tname)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
25
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
26 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27 Type classes
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
29 def isType(a, b):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30 """ Compare types a and b and check if they are equal """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
31 if type(a) is type(b):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32 if type(a) is BaseType:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
33 return (a.name == b.name) and (a.size == b.size)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
34 elif type(a) is ProcedureType:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
35 if len(a.parameters) != len(b.parameters):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
36 print('Number of parameters does not match')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
37 return False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
38 for aparam, bparam in zip(a.parameters, b.parameters):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
39 if not isType(aparam.typ, bparam.typ):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 print('Parameter {0} does not match parameter {1}'.format(aparam, bparam))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41 return False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42 if a.result is None:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
43 # TODO: how to handle a None return type??
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
44 pass
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
45 if not isType(a.result, b.result):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
46 print('Procedure return value mismatch {0} != {1}'.format(a.result, b.result))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
47 return False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
48 return True
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
49 else:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
50 print(a)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
51 print(b)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
52 Error('Not implemented {0}'.format(a))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
53 else:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
54 return False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
55
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
56 class Type(Node):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
57 def isType(self, b):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
58 return isType(self, b)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
59
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
60 class BaseType(Type):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
61 def __init__(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
62 self.name = name
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
63 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
64 return '[TYPE {0}]'.format(self.name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
65
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
66 class FunctionType(Type):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
67 def __init__(self, parameters, returntype):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
68 self.parameters = parameters
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
69 self.returntype = returntype
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
70 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
71 return '[PROCTYPE {0} RET {1}]'.format(self.parameters, self.returntype)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
72
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
73 class DefinedType(Type):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
74 def __init__(self, name, typ):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
75 self.name = name
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
76 self.typ = typ
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
77 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
78 return 'Named type {0} of type {1}'.format(self.name, self.typ)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
79
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
80 # Variables, parameters, local variables, constants:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
81 class Symbol(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
82 pass
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
83
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
84 class Constant(Symbol):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
85 def __init__(self, value, name=None):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
86 self.name = name
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
87 self.value = value
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
88 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
89 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
90
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
91 class Variable(Symbol):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
92 def __init__(self, name, typ):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
93 self.name = name
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
94 self.typ = typ
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
95 self.isLocal = False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
96 self.isReadOnly = False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
97 self.isParameter = False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
98 def __repr__(self):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
99 return 'VAR {0} : {1}'.format(self.name, self.typ)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
100
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
101 # Procedure types
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
102 class Function(Symbol):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
103 """ Actual implementation of a function """
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
104 def __init__(self, name, typ=None, block=None):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
105 self.name = name
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
106 self.body = block
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
107 self.typ = typ
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
108 def __repr__(self):
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
109 return 'PROCEDURE {0} {1}'.format(self.name, self.typ)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
110
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
111 # Operations:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
112 class Unop(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
113 def __init__(self, a, op):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
114 self.a = a
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
115 self.op = op
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
116 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
117 return 'UNOP {0}'.format(self.op)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
118
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
119 class Binop(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
120 def __init__(self, a, op, b):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
121 self.a = a
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
122 self.b = b
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
123 self.op = op # Operation: '+', '-', '*', '/', 'mod'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
124 def __repr__(self):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
125 return 'BINOP {0}'.format(self.op)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
126
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
127 class VariableUse(Node):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
128 def __init__(self, target):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
129 self.target = target
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
130 def __repr__(self):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
131 return 'VAR USE {0}'.format(self.target)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
132
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
133 # Modules
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
134 class Package(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
135 def __init__(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
136 self.name = name
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
137 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
138 return 'PACKAGE {0}'.format(self.name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
139
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
140 # Statements
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
141 class CompoundStatement(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
142 def __init__(self, statements):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
143 self.statements = statements
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
144 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
145 return 'COMPOUND STATEMENT'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
146
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 150
diff changeset
147 class EmptyStatement(Node):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 155
diff changeset
148 def __repr__(self):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 155
diff changeset
149 return 'NOP'
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 150
diff changeset
150
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
151 class ReturnStatement(Node):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
152 def __init__(self, expr):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
153 self.expr = expr
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
154 def __repr__(self):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
155 return 'RETURN STATEMENT'
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
156
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
157 class Assignment(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
158 def __init__(self, lval, rval):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
159 self.lval = lval
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
160 self.rval = rval
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
161 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
162 return 'ASSIGNMENT'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
163
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
164 class ProcedureCall(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
165 def __init__(self, proc, args):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
166 self.proc = proc
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
167 self.args = args
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
168 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
169 return 'CALL {0} '.format(self.proc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
170
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
171 class IfStatement(Node):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 155
diff changeset
172 def __init__(self, condition, truestatement, falsestatement):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
173 self.condition = condition
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
174 self.truestatement = truestatement
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
175 self.falsestatement = falsestatement
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
176 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
177 return 'IF-statement'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
178
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
179 class WhileStatement(Node):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
180 def __init__(self, condition, statements):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
181 self.condition = condition
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
182 self.dostatements = statements
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
183 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
184 return 'WHILE-statement'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
185