148
|
1 """
|
213
|
2 AST (abstract syntax tree) nodes for the c3 language.
|
|
3 The tree is build by the parser.
|
|
4 Then it is checked
|
|
5 Finally code is generated from it.
|
148
|
6 """
|
|
7
|
|
8 class Node:
|
163
|
9 pass
|
148
|
10
|
213
|
11 # Modules
|
|
12 class Package(Node):
|
|
13 def __init__(self, name, loc):
|
|
14 self.name = name
|
|
15 self.loc = loc
|
|
16 self.declarations = []
|
|
17 def __repr__(self):
|
|
18 return 'PACKAGE {}'.format(self.name)
|
|
19
|
150
|
20 class Designator(Node):
|
213
|
21 def __init__(self, tname, loc):
|
|
22 self.tname = tname
|
|
23 self.loc = loc
|
|
24 def __repr__(self):
|
|
25 return 'DESIGNATOR {}'.format(self.tname)
|
148
|
26
|
|
27 """
|
|
28 Type classes
|
213
|
29
|
|
30 types must be comparable.
|
|
31
|
|
32 There are the following types:
|
|
33 - base type
|
|
34 - struct type
|
|
35 - pointer type
|
|
36 - typedef type
|
|
37 - function type
|
148
|
38 """
|
|
39
|
149
|
40 class Type(Node):
|
148
|
41 def isType(self, b):
|
|
42 return isType(self, b)
|
|
43
|
|
44 class BaseType(Type):
|
|
45 def __init__(self, name):
|
|
46 self.name = name
|
|
47 def __repr__(self):
|
212
|
48 return '{}'.format(self.name)
|
148
|
49
|
|
50 class FunctionType(Type):
|
167
|
51 def __init__(self, parametertypes, returntype):
|
|
52 self.parametertypes = parametertypes
|
148
|
53 self.returntype = returntype
|
|
54 def __repr__(self):
|
167
|
55 params = ', '.join([str(v) for v in self.parametertypes])
|
165
|
56 return '{1} f({0})'.format(params, self.returntype)
|
148
|
57
|
213
|
58 class PointerType(Type):
|
|
59 def __init__(self, ptype):
|
|
60 self.ptype = ptype
|
|
61
|
|
62 class StructureType(Type):
|
|
63 def __init__(self, mems):
|
|
64 self.mems = mems
|
|
65
|
148
|
66 class DefinedType(Type):
|
|
67 def __init__(self, name, typ):
|
|
68 self.name = name
|
|
69 self.typ = typ
|
|
70 def __repr__(self):
|
|
71 return 'Named type {0} of type {1}'.format(self.name, self.typ)
|
|
72
|
|
73 # Variables, parameters, local variables, constants:
|
|
74 class Symbol(Node):
|
213
|
75 def __init__(self, name):
|
163
|
76 self.name = name
|
|
77 self.refs = []
|
213
|
78 def addRef(self, r):
|
163
|
79 self.refs.append(r)
|
213
|
80 @property
|
|
81 def References(self):
|
163
|
82 return self.refs
|
148
|
83
|
|
84 class Constant(Symbol):
|
213
|
85 def __init__(self, name, typ, value):
|
163
|
86 super().__init__(name)
|
|
87 self.typ = typ
|
148
|
88 self.value = value
|
213
|
89 def __repr__(self):
|
148
|
90 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
|
|
91
|
|
92 class Variable(Symbol):
|
213
|
93 def __init__(self, name, typ):
|
163
|
94 super().__init__(name)
|
148
|
95 self.typ = typ
|
213
|
96 self.ival = None
|
148
|
97 self.isLocal = False
|
|
98 self.isReadOnly = False
|
|
99 self.isParameter = False
|
|
100 def __repr__(self):
|
212
|
101 return '{}'.format(self.name)
|
148
|
102
|
150
|
103 # Procedure types
|
|
104 class Function(Symbol):
|
213
|
105 """ Actual implementation of a function """
|
|
106 def __init__(self, name, loc):
|
|
107 super().__init__(name)
|
|
108 self.loc = loc
|
|
109
|
|
110 def __repr__(self):
|
|
111 return '{}'.format(self.name)
|
148
|
112
|
163
|
113 # Operations / Expressions:
|
148
|
114 class Unop(Node):
|
|
115 def __init__(self, a, op):
|
|
116 self.a = a
|
|
117 self.op = op
|
|
118 def __repr__(self):
|
212
|
119 return 'UNOP {}'.format(self.op)
|
148
|
120
|
|
121 class Binop(Node):
|
213
|
122 def __init__(self, a, op, b, loc):
|
148
|
123 self.a = a
|
|
124 self.b = b
|
|
125 self.op = op # Operation: '+', '-', '*', '/', 'mod'
|
213
|
126 self.loc = loc
|
148
|
127 def __repr__(self):
|
212
|
128 return 'BINOP {}'.format(self.op)
|
148
|
129
|
150
|
130 class VariableUse(Node):
|
213
|
131 def __init__(self, target, loc):
|
150
|
132 self.target = target
|
213
|
133 self.loc = loc
|
150
|
134 def __repr__(self):
|
163
|
135 nm = self.target.name if hasattr(self.target, 'name') else ''
|
212
|
136 return 'VAR USE {}'.format(nm)
|
163
|
137
|
|
138 class Literal(Node):
|
213
|
139 def __init__(self, val, loc):
|
163
|
140 self.val = val
|
213
|
141 self.loc = loc
|
163
|
142 def __repr__(self):
|
212
|
143 return 'LITERAL {}'.format(self.val)
|
150
|
144
|
148
|
145
|
|
146 # Statements
|
|
147 class CompoundStatement(Node):
|
|
148 def __init__(self, statements):
|
|
149 self.statements = statements
|
|
150 def __repr__(self):
|
|
151 return 'COMPOUND STATEMENT'
|
|
152
|
155
|
153 class EmptyStatement(Node):
|
158
|
154 def __repr__(self):
|
|
155 return 'NOP'
|
155
|
156
|
149
|
157 class ReturnStatement(Node):
|
|
158 def __init__(self, expr):
|
|
159 self.expr = expr
|
148
|
160 def __repr__(self):
|
149
|
161 return 'RETURN STATEMENT'
|
148
|
162
|
|
163 class Assignment(Node):
|
213
|
164 def __init__(self, lval, rval, loc):
|
148
|
165 self.lval = lval
|
|
166 self.rval = rval
|
213
|
167 self.loc = loc
|
148
|
168 def __repr__(self):
|
|
169 return 'ASSIGNMENT'
|
|
170
|
167
|
171 class FunctionCall(Node):
|
213
|
172 def __init__(self, proc, args, loc):
|
148
|
173 self.proc = proc
|
|
174 self.args = args
|
213
|
175 self.loc = loc
|
148
|
176 def __repr__(self):
|
|
177 return 'CALL {0} '.format(self.proc)
|
|
178
|
|
179 class IfStatement(Node):
|
213
|
180 def __init__(self, condition, truestatement, falsestatement, loc):
|
148
|
181 self.condition = condition
|
|
182 self.truestatement = truestatement
|
|
183 self.falsestatement = falsestatement
|
213
|
184 self.loc = loc
|
148
|
185 def __repr__(self):
|
|
186 return 'IF-statement'
|
|
187
|
|
188 class WhileStatement(Node):
|
213
|
189 def __init__(self, condition, statement, loc):
|
148
|
190 self.condition = condition
|
213
|
191 self.statement = statement
|
|
192 self.loc = loc
|
148
|
193 def __repr__(self):
|
|
194 return 'WHILE-statement'
|
|
195
|