annotate python/c3/astnodes.py @ 225:1c7364bd74c7

Fixed pointer deref
author Windel Bouwman
date Thu, 11 Jul 2013 07:42:30 +0200
parents c3f1ce8b638f
children 240111e0456f
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
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
10 class Node:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
11 pass
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
13 # Modules
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
14 class Package(Node):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
15 def __init__(self, name, loc):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
16 self.name = name
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
17 self.loc = loc
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
18 self.declarations = []
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
19 def __repr__(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
20 return 'PACKAGE {}'.format(self.name)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
21
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
22 class Designator(Node):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
23 def __init__(self, tname, loc):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
24 self.tname = tname
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
25 self.loc = loc
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
26 def __repr__(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
27 return 'DESIGNATOR {}'.format(self.tname)
148
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 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30 Type classes
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
31
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
32 types must be comparable.
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
33
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
34 There are the following types:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
35 - base type
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
36 - struct type
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
37 - pointer type
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
38 - typedef type
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
39 - function type
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
42 class Type(Node):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
43 def isType(self, b):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
44 return isType(self, b)
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 class BaseType(Type):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
47 def __init__(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
48 self.name = name
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
49 def __repr__(self):
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 186
diff changeset
50 return '{}'.format(self.name)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
51
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
52 class FunctionType(Type):
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
53 def __init__(self, parametertypes, returntype):
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
54 self.parametertypes = parametertypes
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
55 self.returntype = returntype
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
56 def __repr__(self):
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
57 params = ', '.join([str(v) for v in self.parametertypes])
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
58 return '{1} f({0})'.format(params, self.returntype)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
59
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
60 class PointerType(Type):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
61 def __init__(self, ptype):
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
62 assert isinstance(ptype, Type) or isinstance(ptype, Designator)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
63 self.ptype = ptype
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
64 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
65 return '({}*)'.format(self.ptype)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
66
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
67 class StructureType(Type):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
68 def __init__(self, mems):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
69 self.mems = mems
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
70 def hasField(self, name):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
71 for fn, ft in self.mems:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
72 if name == fn:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
73 return True
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
74 return False
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
75 def fieldType(self, name):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
76 for fn, ft in self.mems:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
77 if name == fn:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
78 return ft
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
79 raise Exception()
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
80
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
81
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
82 class DefinedType(Type):
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
83 def __init__(self, name, typ, loc):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
84 assert isinstance(name, str)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
85 self.name = name
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
86 self.typ = typ
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
87 self.loc = loc
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
88 def __repr__(self):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
89 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
90
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
91 class TypeCast(Node):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
92 def __init__(self, to_type, x):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
93 self.to_type = to_type
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
94 self.a = x
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
95 def __repr__(self):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
96 return 'TYPECAST'
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
97
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
98 # Variables, parameters, local variables, constants:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
99 class Symbol(Node):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
100 def __init__(self, name):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
101 self.name = name
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
102 self.refs = []
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
103 def addRef(self, r):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
104 self.refs.append(r)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
105 @property
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
106 def References(self):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
107 return self.refs
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
108
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
109 class Constant(Symbol):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
110 def __init__(self, name, typ, value):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
111 super().__init__(name)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
112 self.typ = typ
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
113 self.value = value
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
114 def __repr__(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
115 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
116
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
117 class Variable(Symbol):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
118 def __init__(self, name, typ):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
119 super().__init__(name)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
120 self.typ = typ
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
121 self.ival = None
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
122 self.isLocal = False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
123 self.isReadOnly = False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
124 self.isParameter = False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
125 def __repr__(self):
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
126 return 'Var {} [{}]'.format(self.name, self.typ)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
127
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
128 # Procedure types
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
129 class Function(Symbol):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
130 """ Actual implementation of a function """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
131 def __init__(self, name, loc):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
132 super().__init__(name)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
133 self.loc = loc
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
134 self.declarations = []
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
135
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
136 def __repr__(self):
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 215
diff changeset
137 return 'Func {}'.format(self.name)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
138
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
139 # Operations / Expressions:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
140 class Expression(Node):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
141 pass
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
142
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
143 class Deref(Expression):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
144 def __init__(self, ptr, loc):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
145 assert isinstance(ptr, Expression)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
146 self.ptr = ptr
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
147 self.loc = loc
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
148 def __repr__(self):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
149 return 'DEREF {}'.format(self.ptr)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
150
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
151 class FieldRef(Expression):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
152 def __init__(self, base, field, loc):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
153 assert isinstance(base, Expression)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
154 assert isinstance(field, str)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
155 self.base = base
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
156 self.field = field
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
157 self.loc = loc
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
158 def __repr__(self):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
159 return 'FIELD {}.{}'.format(self.base, self.field)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
160
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
161 class Unop(Expression):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
162 def __init__(self, op, a, loc):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
163 assert isinstance(a, Expression)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
164 assert isinstance(op, str)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
165 self.a = a
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
166 self.op = op
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
167 self.loc = loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
168 def __repr__(self):
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 186
diff changeset
169 return 'UNOP {}'.format(self.op)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
170
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
171 class Binop(Expression):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
172 def __init__(self, a, op, b, loc):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
173 assert isinstance(a, Expression), type(a)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
174 assert isinstance(b, Expression)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
175 assert isinstance(op, str)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
176 self.a = a
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
177 self.b = b
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
178 self.op = op # Operation: '+', '-', '*', '/', 'mod'
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
179 self.loc = loc
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
180
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
181 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
182 return 'BINOP {}'.format(self.op)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
183
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
184 class VariableUse(Expression):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
185 def __init__(self, target, loc):
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
186 self.target = target
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
187 self.loc = loc
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
188 def __repr__(self):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
189 nm = self.target.name if hasattr(self.target, 'name') else ''
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 186
diff changeset
190 return 'VAR USE {}'.format(nm)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
191
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
192 class Literal(Expression):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
193 def __init__(self, val, loc):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
194 self.val = val
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
195 self.loc = loc
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
196 def __repr__(self):
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 186
diff changeset
197 return 'LITERAL {}'.format(self.val)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
198
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
199 class FunctionCall(Expression):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
200 def __init__(self, proc, args, loc):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
201 self.proc = proc
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
202 self.args = args
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
203 self.loc = loc
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
204 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
205 return 'CALL {0} '.format(self.proc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
206
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
207 # Statements
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
208 class Statement(Node):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
209 pass
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
210
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
211 class CompoundStatement(Statement):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
212 def __init__(self, statements):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
213 self.statements = statements
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
214
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
215 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
216 return 'COMPOUND STATEMENT'
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
217
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
218 class EmptyStatement(Statement):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
219 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
220 return 'NOP'
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
221
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
222 class ReturnStatement(Statement):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
223 def __init__(self, expr, loc):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
224 self.expr = expr
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
225 self.loc = loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
226 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
227 return 'RETURN STATEMENT'
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
228
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
229 class Assignment(Statement):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
230 def __init__(self, lval, rval, loc):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
231 assert isinstance(lval, Node)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
232 assert isinstance(rval, Node)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
233 assert isinstance(loc, SourceLocation)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
234 self.lval = lval
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
235 self.rval = rval
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
236 self.loc = loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
237
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
238 def __repr__(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
239 return 'ASSIGNMENT'
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
240
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
241 class ExpressionStatement(Statement):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
242 def __init__(self, ex, loc):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
243 self.ex = ex
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
244 self.loc = loc
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
245 assert isinstance(loc, SourceLocation)
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
246 def __repr__(self):
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
247 return 'Epression'
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
248
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
249 class IfStatement(Statement):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
250 def __init__(self, condition, truestatement, falsestatement, loc):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
251 self.condition = condition
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
252 self.truestatement = truestatement
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
253 self.falsestatement = falsestatement
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
254 self.loc = loc
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
255 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
256 return 'IF-statement'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
257
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
258 class WhileStatement(Statement):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
259 def __init__(self, condition, statement, loc):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
260 self.condition = condition
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
261 self.statement = statement
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 212
diff changeset
262 self.loc = loc
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
263 def __repr__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
264 return 'WHILE-statement'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
265