annotate python/ppci/c3/astnodes.py @ 346:3bb7dcfe5529

expanded arm target
author Windel Bouwman
date Fri, 07 Mar 2014 17:05:32 +0100
parents d1ecc493384e
children 5477e499b039
rev   line source
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
1 """
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
2 AST (abstract syntax tree) nodes for the c3 language.
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
3 The tree is build by the parser.
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
4 Then it is checked
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
5 Finally code is generated from it.
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
6 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
7
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
8 from ppci import SourceLocation
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
9
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
10
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
11 class Node:
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
12 """ Base class of all nodes in a AST """
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
13 pass
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
14
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15
300
Windel Bouwman
parents: 289
diff changeset
16 # Variables, parameters, local variables, constants and named types:
Windel Bouwman
parents: 289
diff changeset
17 class Symbol(Node):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
18 """ Symbol is the base class for all named things like variables,
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
19 functions, constants and types and modules """
300
Windel Bouwman
parents: 289
diff changeset
20 def __init__(self, name):
Windel Bouwman
parents: 289
diff changeset
21 self.name = name
Windel Bouwman
parents: 289
diff changeset
22 self.refs = []
Windel Bouwman
parents: 289
diff changeset
23
Windel Bouwman
parents: 289
diff changeset
24 def addRef(self, r):
Windel Bouwman
parents: 289
diff changeset
25 self.refs.append(r)
Windel Bouwman
parents: 289
diff changeset
26
Windel Bouwman
parents: 289
diff changeset
27 @property
Windel Bouwman
parents: 289
diff changeset
28 def References(self):
Windel Bouwman
parents: 289
diff changeset
29 return self.refs
Windel Bouwman
parents: 289
diff changeset
30
Windel Bouwman
parents: 289
diff changeset
31
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
32 # Modules
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
33 class Package(Symbol):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
34 def __init__(self, name, loc):
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
35 super().__init__(name)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
36 self.loc = loc
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
37 self.declarations = []
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
38 self.imports = []
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
39
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 316
diff changeset
40 def add_declaration(self, decl):
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 316
diff changeset
41 self.declarations.append(decl)
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 316
diff changeset
42 if isinstance(decl, Function):
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 316
diff changeset
43 decl.package = self
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 316
diff changeset
44
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
45 def __repr__(self):
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
46 return 'MODULE {}'.format(self.name)
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
47
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
48
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
49 class Type(Node):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
50 """ Base class of all types """
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
51 pass
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
52
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
53
300
Windel Bouwman
parents: 289
diff changeset
54 class NamedType(Type, Symbol):
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
55 """ Some types are named, for example a user defined type (typedef)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
56 and built in types. That is why this class derives from both Type
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
57 and Symbol. """
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
58 def __init__(self, name):
300
Windel Bouwman
parents: 289
diff changeset
59 Symbol.__init__(self, name)
Windel Bouwman
parents: 289
diff changeset
60
Windel Bouwman
parents: 289
diff changeset
61
Windel Bouwman
parents: 289
diff changeset
62 class BaseType(NamedType):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
63 """ Built in type """
300
Windel Bouwman
parents: 289
diff changeset
64 def __init__(self, name):
Windel Bouwman
parents: 289
diff changeset
65 super().__init__(name)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
66
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
67 def __repr__(self):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
68 return '{}'.format(self.name)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
69
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
70
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
71 class FunctionType(Type):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
72 """ Function blueprint, defines argument types and return type """
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
73 def __init__(self, parametertypes, returntype):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
74 self.parametertypes = parametertypes
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
75 self.returntype = returntype
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
76
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
77 def __repr__(self):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
78 params = ', '.join([str(v) for v in self.parametertypes])
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
79 return '{1} f({0})'.format(params, self.returntype)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
80
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
81
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
82 class PointerType(Type):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
83 """ A type that points to data of some other type """
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
84 def __init__(self, ptype):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
85 assert isinstance(ptype, Type) or isinstance(ptype, Expression)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
86 self.ptype = ptype
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
87
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
88 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
89 return '({}*)'.format(self.ptype)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
90
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
91
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
92 class StructField:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
93 """ Field of a struct type """
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
94 def __init__(self, name, typ):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
95 assert type(name) is str
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
96 self.name = name
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
97 self.typ = typ
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
98
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
99
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
100 class StructureType(Type):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
101 """ Struct type consisting of several named members """
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
102 def __init__(self, mems):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
103 self.mems = mems
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
104 assert all(type(mem) is StructField for mem in mems)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
105
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
106 def hasField(self, name):
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
107 for mem in self.mems:
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
108 if name == mem.name:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
109 return True
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
110 return False
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
111
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
112 def fieldType(self, name):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
113 return self.findField(name).typ
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
114
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
115 def fieldOffset(self, name):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
116 return self.findField(name).offset
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
117
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
118 def findField(self, name):
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
119 for mem in self.mems:
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
120 if name == mem.name:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
121 return mem
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
122 raise KeyError(name)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
123
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
124 def __repr__(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
125 return 'STRUCT'
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
126
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
127
300
Windel Bouwman
parents: 289
diff changeset
128 class DefinedType(NamedType):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
129 """ A named type indicating another type """
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
130 def __init__(self, name, typ, loc):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
131 assert isinstance(name, str)
300
Windel Bouwman
parents: 289
diff changeset
132 super().__init__(name)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
133 self.typ = typ
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
134 self.loc = loc
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
135
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
136 def __repr__(self):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
137 return 'Named type {0} of type {1}'.format(self.name, self.typ)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
138
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
139
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
140 class Constant(Symbol):
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 315
diff changeset
141 """ Constant definition """
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
142 def __init__(self, name, typ, value):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
143 super().__init__(name)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
144 self.typ = typ
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
145 self.value = value
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
146
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
147 def __repr__(self):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
148 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
149
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
150
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
151 class Variable(Symbol):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
152 def __init__(self, name, typ):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
153 super().__init__(name)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
154 self.typ = typ
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
155 self.isLocal = False
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
156 self.isParameter = False
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
157
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
158 def __repr__(self):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
159 return 'Var {} [{}]'.format(self.name, self.typ)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
160
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
161
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
162 class LocalVariable(Variable):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
163 def __init__(self, name, typ):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
164 super().__init__(name, typ)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
165 self.isLocal = True
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
166
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
167
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
168 class FormalParameter(Variable):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
169 def __init__(self, name, typ):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
170 super().__init__(name, typ)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
171 self.isParameter = True
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
172
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
173
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
174 # Procedure types
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
175 class Function(Symbol):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
176 """ Actual implementation of a function """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
177 def __init__(self, name, loc):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
178 super().__init__(name)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
179 self.loc = loc
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
180 self.declarations = []
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
181
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 316
diff changeset
182 def add_declaration(self, decl):
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 316
diff changeset
183 self.declarations.append(decl)
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 316
diff changeset
184
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
185 def __repr__(self):
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 215
diff changeset
186 return 'Func {}'.format(self.name)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
187
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
188
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
189 # Operations / Expressions:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
190 class Expression(Node):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
191 def __init__(self, loc):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
192 self.loc = loc
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
193
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
194
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
195 class Deref(Expression):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
196 def __init__(self, ptr, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
197 super().__init__(loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
198 assert isinstance(ptr, Expression)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
199 self.ptr = ptr
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
200
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
201 def __repr__(self):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
202 return 'DEREF {}'.format(self.ptr)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
203
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
204
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
205 class TypeCast(Expression):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
206 def __init__(self, to_type, x, loc):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
207 super().__init__(loc)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
208 self.to_type = to_type
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
209 self.a = x
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
210
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
211 def __repr__(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
212 return 'TYPECAST {}'.format(self.to_type)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
213
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
214
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
215 class Member(Expression):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
216 """ Field reference of some object, can also be package selection """
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
217 def __init__(self, base, field, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
218 super().__init__(loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
219 assert isinstance(base, Expression)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
220 assert isinstance(field, str)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
221 self.base = base
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
222 self.field = field
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
223
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
224 def __repr__(self):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
225 return 'MEMBER {}.{}'.format(self.base, self.field)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
226
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
227
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
228 class Unop(Expression):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
229 """ Operation on one operand """
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
230 def __init__(self, op, a, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
231 super().__init__(loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
232 assert isinstance(a, Expression)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
233 assert isinstance(op, str)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
234 self.a = a
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
235 self.op = op
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
236
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
237 def __repr__(self):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
238 return 'UNOP {}'.format(self.op)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
239
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
240
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
241 class Binop(Expression):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
242 """ Expression taking two operands and one operator """
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
243 def __init__(self, a, op, b, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
244 super().__init__(loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
245 assert isinstance(a, Expression), type(a)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
246 assert isinstance(b, Expression)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
247 assert isinstance(op, str)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
248 self.a = a
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
249 self.b = b
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
250 self.op = op # Operation: '+', '-', '*', '/', 'mod'
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
251
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
252 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
253 return 'BINOP {}'.format(self.op)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
254
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
255
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
256 class Identifier(Expression):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
257 """ Reference to some identifier, can be anything from package, variable
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
258 function or type, any named thing! """
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
259 def __init__(self, target, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
260 super().__init__(loc)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
261 self.target = target
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
262
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
263 def __repr__(self):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
264 return 'ID {}'.format(self.target)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
265
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
266
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
267 class Literal(Expression):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
268 """ Constant value or string """
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
269 def __init__(self, val, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
270 super().__init__(loc)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
271 self.val = val
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
272
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
273 def __repr__(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
274 return 'LITERAL {}'.format(self.val)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
275
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
276
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
277 class FunctionCall(Expression):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
278 """ Call to a some function """
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
279 def __init__(self, proc, args, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
280 super().__init__(loc)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
281 self.proc = proc
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
282 self.args = args
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
283
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
284 def __repr__(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
285 return 'CALL {0} '.format(self.proc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
286
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
287
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
288 # Statements
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
289 class Statement(Node):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
290 """ Base class of all statements """
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
291 def __init__(self, loc):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
292 self.loc = loc
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
293
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
294
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
295 class Empty(Statement):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
296 """ Empty statement which does nothing! """
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
297 def __init__(self):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
298 super().__init__(None)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
299
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
300 def __repr__(self):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
301 return 'NOP'
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
302
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
303
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
304 class Compound(Statement):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
305 """ Statement consisting of a sequence of other statements """
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
306 def __init__(self, statements):
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 230
diff changeset
307 super().__init__(None)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
308 self.statements = statements
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
309 for s in self.statements:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
310 assert isinstance(s, Statement)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
311
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
312 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
313 return 'COMPOUND STATEMENT'
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
314
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
315
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
316 class Return(Statement):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
317 def __init__(self, expr, loc):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
318 super().__init__(loc)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
319 self.expr = expr
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
320
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
321 def __repr__(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
322 return 'RETURN STATEMENT'
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
323
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
324
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
325 class Assignment(Statement):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
326 def __init__(self, lval, rval, loc):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
327 super().__init__(loc)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
328 assert isinstance(lval, Expression)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
329 assert isinstance(rval, Expression)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
330 self.lval = lval
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
331 self.rval = rval
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
332
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
333 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
334 return 'ASSIGNMENT'
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
335
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
336
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
337 class ExpressionStatement(Statement):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
338 def __init__(self, ex, loc):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
339 super().__init__(loc)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
340 self.ex = ex
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
341
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
342 def __repr__(self):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
343 return 'Epression'
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
344
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
345
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
346 class If(Statement):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
347 def __init__(self, condition, truestatement, falsestatement, loc):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
348 super().__init__(loc)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
349 self.condition = condition
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
350 self.truestatement = truestatement
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
351 self.falsestatement = falsestatement
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
352
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
353 def __repr__(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
354 return 'IF-statement'
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
355
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
356
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
357 class While(Statement):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
358 def __init__(self, condition, statement, loc):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
359 super().__init__(loc)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
360 self.condition = condition
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
361 self.statement = statement
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
362
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
363 def __repr__(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
364 return 'WHILE-statement'
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
365
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
366
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
367 class For(Statement):
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
368 def __init__(self, init, condition, final, statement, loc):
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
369 super().__init__(loc)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
370 self.init = init
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
371 self.condition = condition
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
372 self.final = final
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
373 self.statement = statement
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
374
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
375 def __repr__(self):
084cccaa5deb Added console and screen
Windel Bouwman
parents: 308
diff changeset
376 return 'FOR-statement'