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