annotate python/c3/astnodes.py @ 293:6aa721e7b10b

Try to improve build sequence
author Windel Bouwman
date Thu, 28 Nov 2013 20:39:37 +0100
parents bd2593de3ff8
children
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:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
12 pass
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
13
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
15 # Modules
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
16 class Package(Node):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
17 def __init__(self, name, loc):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
18 self.name = name
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
19 self.loc = loc
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
20 self.declarations = []
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
21 self.imports = []
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
22
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
23 def __repr__(self):
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
24 return 'MODULE {}'.format(self.name)
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
25
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
26
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
27 class Designator(Node):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
28 def __init__(self, tname, loc):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
29 self.tname = tname
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
30 self.loc = loc
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
31
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
32 def __repr__(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
33 return 'DESIGNATOR {}'.format(self.tname)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
34
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
35
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
36 class ImportDesignator(Designator):
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
37 def __init__(self, tname, vname, loc):
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
38 super().__init__(tname, loc)
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
39 self.vname = vname
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
40
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
41 def __repr__(self):
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
42 return 'IMPORT DESIGNATOR {}:{}'.format(self.tname, self.vname)
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
43
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
44
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
45 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
46 Type classes
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
47
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
48 types must be comparable.
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
49
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
50 There are the following types:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
51 - base type -> basic type (built in)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
52 - struct type -> a composite type that contains a list of named fields
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
53 of other types
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
54 - function type
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
55 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
56
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
57
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
58 class Type(Node):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
59 pass
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
60
148
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 BaseType(Type):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
63 def __init__(self, name):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
64 self.name = name
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
65
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
66 def __repr__(self):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
67 return '{}'.format(self.name)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
68
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
69
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
70 class FunctionType(Type):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
71 def __init__(self, parametertypes, returntype):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
72 self.parametertypes = parametertypes
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
73 self.returntype = returntype
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
74
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
75 def __repr__(self):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
76 params = ', '.join([str(v) for v in self.parametertypes])
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
77 return '{1} f({0})'.format(params, self.returntype)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
78
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
79
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
80 class PointerType(Type):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
81 """ A type that points to data of some other type """
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
82 def __init__(self, ptype):
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
83 assert isinstance(ptype, Type) or isinstance(ptype, Designator)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
84 self.ptype = ptype
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
85
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
86 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
87 return '({}*)'.format(self.ptype)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
88
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
89
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
90 class StructField:
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
91 def __init__(self, name, typ):
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
92 self.name = name
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
93 self.typ = typ
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
94 self.offset = 0
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
95
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
96
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
97 class StructureType(Type):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
98 def __init__(self, mems):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
99 self.mems = mems
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
100 for mem in mems:
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
101 assert type(mem) is StructField
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
102 assert type(mem.name) is str
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
103
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
104 def hasField(self, name):
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
105 for mem in self.mems:
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
106 if name == mem.name:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
107 return True
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
108 return False
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
109
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
110 def fieldType(self, name):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
111 return self.findField(name).typ
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
112
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
113 def fieldOffset(self, name):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
114 return self.findField(name).offset
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
115
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
116 def findField(self, name):
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
117 for mem in self.mems:
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
118 if name == mem.name:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
119 return mem
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
120 raise KeyError(name)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
121
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
122 def __repr__(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
123 return 'STRUCT'
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
124
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
125
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
126 class DefinedType(Type):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
127 """ A named type indicating another type """
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
128 def __init__(self, name, typ, loc):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
129 assert isinstance(name, str)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
130 self.name = name
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
131 self.typ = typ
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
132 self.loc = loc
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
133
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
134 def __repr__(self):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
135 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
136
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
137
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
138 # Variables, parameters, local variables, constants:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
139 class Symbol(Node):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
140 def __init__(self, name):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
141 self.name = name
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
142 self.refs = []
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
143
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
144 def addRef(self, r):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
145 self.refs.append(r)
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 @property
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
148 def References(self):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
149 return self.refs
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
150
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
151
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
152 class Constant(Symbol):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
153 def __init__(self, name, typ, value):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
154 super().__init__(name)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
155 self.typ = typ
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
156 self.value = value
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
157
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
158 def __repr__(self):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
159 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
160
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
161
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
162 class Variable(Symbol):
272
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)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
165 self.typ = typ
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
166 self.ival = None
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
167 self.isLocal = False
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
168 self.isReadOnly = False
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
169 self.isParameter = False
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
170
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
171 def __repr__(self):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
172 return 'Var {} [{}]'.format(self.name, self.typ)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
173
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
174
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
175 class LocalVariable(Variable):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
176 def __init__(self, name, typ):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
177 super().__init__(name, typ)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
178 self.isLocal = True
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
179
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
180
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
181 class FormalParameter(Variable):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
182 def __init__(self, name, typ):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
183 super().__init__(name, typ)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
184 self.isParameter = True
e64bae57cda8 refactor ir
Windel Bouwman
parents: 251
diff changeset
185
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
186
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
187 # Procedure types
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
188 class Function(Symbol):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
189 """ Actual implementation of a function """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
190 def __init__(self, name, loc):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
191 super().__init__(name)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
192 self.loc = loc
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
193 self.declarations = []
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
194
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
195 def __repr__(self):
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 215
diff changeset
196 return 'Func {}'.format(self.name)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
197
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
198
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
199 # Operations / Expressions:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
200 class Expression(Node):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
201 def __init__(self, loc):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
202 self.loc = loc
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
203
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
204
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
205 class Deref(Expression):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
206 def __init__(self, ptr, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
207 super().__init__(loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
208 assert isinstance(ptr, Expression)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
209 self.ptr = ptr
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
210
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
211 def __repr__(self):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
212 return 'DEREF {}'.format(self.ptr)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
213
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
214
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
215 class TypeCast(Expression):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
216 def __init__(self, to_type, x, loc):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
217 super().__init__(loc)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
218 self.to_type = to_type
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
219 self.a = x
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
220
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
221 def __repr__(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
222 return 'TYPECAST {}'.format(self.to_type)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
223
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
224
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
225 class FieldRef(Expression):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
226 def __init__(self, base, field, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
227 super().__init__(loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
228 assert isinstance(base, Expression)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
229 assert isinstance(field, str)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
230 self.base = base
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
231 self.field = field
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
232
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
233 def __repr__(self):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
234 return 'FIELD {}.{}'.format(self.base, self.field)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
235
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
236
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
237 class Unop(Expression):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
238 def __init__(self, op, a, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
239 super().__init__(loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
240 assert isinstance(a, Expression)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
241 assert isinstance(op, str)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
242 self.a = a
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
243 self.op = op
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
244
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
245 def __repr__(self):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
246 return 'UNOP {}'.format(self.op)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
247
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
248
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
249 class Binop(Expression):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
250 def __init__(self, a, op, b, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
251 super().__init__(loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
252 assert isinstance(a, Expression), type(a)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
253 assert isinstance(b, Expression)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
254 assert isinstance(op, str)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
255 self.a = a
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
256 self.b = b
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
257 self.op = op # Operation: '+', '-', '*', '/', 'mod'
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
258
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
259 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
260 return 'BINOP {}'.format(self.op)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
261
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
262
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
263 class VariableUse(Expression):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
264 def __init__(self, target, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
265 super().__init__(loc)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
266 self.target = target
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
267
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
268 def __repr__(self):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
269 return 'VAR USE {}'.format(self.target)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
270
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
271
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
272 class Literal(Expression):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
273 def __init__(self, val, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
274 super().__init__(loc)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
275 self.val = val
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
276
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
277 def __repr__(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
278 return 'LITERAL {}'.format(self.val)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
279
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
280
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
281 class FunctionCall(Expression):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
282 def __init__(self, proc, args, loc):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
283 super().__init__(loc)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
284 self.proc = proc
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
285 self.args = args
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
286
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
287 def __repr__(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
288 return 'CALL {0} '.format(self.proc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
289
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
290
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
291 # Statements
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
292 class Statement(Node):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
293 def __init__(self, loc):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
294 self.loc = loc
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
295
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
296
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
297 class CompoundStatement(Statement):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
298 def __init__(self, statements):
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 230
diff changeset
299 super().__init__(None)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
300 self.statements = statements
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
301 for s in self.statements:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
302 assert isinstance(s, Statement)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
303
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
304 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
305 return 'COMPOUND STATEMENT'
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
306
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
307
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
308 class ReturnStatement(Statement):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
309 def __init__(self, expr, loc):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
310 super().__init__(loc)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
311 self.expr = expr
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
312
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
313 def __repr__(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
314 return 'RETURN STATEMENT'
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
315
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 272
diff changeset
316
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
317 class Assignment(Statement):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
318 def __init__(self, lval, rval, loc):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
319 super().__init__(loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
320 assert isinstance(lval, Node)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
321 assert isinstance(rval, Node)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
322 self.lval = lval
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
323 self.rval = rval
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
324
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
325 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
326 return 'ASSIGNMENT'
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
327
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
328
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
329 class ExpressionStatement(Statement):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
330 def __init__(self, ex, loc):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
331 super().__init__(loc)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
332 self.ex = ex
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
333
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
334 def __repr__(self):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
335 return 'Epression'
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
336
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
337
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
338 class IfStatement(Statement):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
339 def __init__(self, condition, truestatement, falsestatement, loc):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
340 super().__init__(loc)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
341 self.condition = condition
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
342 self.truestatement = truestatement
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
343 self.falsestatement = falsestatement
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
344
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
345 def __repr__(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
346 return 'IF-statement'
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
347
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
348
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
349 class WhileStatement(Statement):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
350 def __init__(self, condition, statement, loc):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
351 super().__init__(loc)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
352 self.condition = condition
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
353 self.statement = statement
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
354
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
355 def __repr__(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
356 return 'WHILE-statement'