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 """
|
|
76 def __init__(self, name, typ=None, block=None):
|
163
|
77 super().__init__(name)
|
150
|
78 self.body = block
|
148
|
79 self.typ = typ
|
|
80 def __repr__(self):
|
165
|
81 return '{0}'.format(self.name)
|
148
|
82
|
163
|
83 # Operations / Expressions:
|
148
|
84 class Unop(Node):
|
|
85 def __init__(self, a, op):
|
|
86 self.a = a
|
|
87 self.op = op
|
|
88 def __repr__(self):
|
|
89 return 'UNOP {0}'.format(self.op)
|
|
90
|
|
91 class Binop(Node):
|
|
92 def __init__(self, a, op, b):
|
|
93 self.a = a
|
|
94 self.b = b
|
|
95 self.op = op # Operation: '+', '-', '*', '/', 'mod'
|
|
96 def __repr__(self):
|
166
|
97 return 'BINOP {0}'.format(self.op)
|
148
|
98
|
150
|
99 class VariableUse(Node):
|
|
100 def __init__(self, target):
|
|
101 self.target = target
|
|
102 def __repr__(self):
|
163
|
103 nm = self.target.name if hasattr(self.target, 'name') else ''
|
|
104 return 'VAR USE {0}'.format(nm)
|
|
105
|
|
106 class Literal(Node):
|
|
107 def __init__(self, val):
|
|
108 self.val = val
|
|
109 def __repr__(self):
|
|
110 return 'LITERAL {0}'.format(self.val)
|
150
|
111
|
148
|
112 # Modules
|
|
113 class Package(Node):
|
|
114 def __init__(self, name):
|
|
115 self.name = name
|
|
116 def __repr__(self):
|
|
117 return 'PACKAGE {0}'.format(self.name)
|
|
118
|
|
119 # Statements
|
|
120 class CompoundStatement(Node):
|
|
121 def __init__(self, statements):
|
|
122 self.statements = statements
|
|
123 def __repr__(self):
|
|
124 return 'COMPOUND STATEMENT'
|
|
125
|
155
|
126 class EmptyStatement(Node):
|
158
|
127 def __repr__(self):
|
|
128 return 'NOP'
|
155
|
129
|
149
|
130 class ReturnStatement(Node):
|
|
131 def __init__(self, expr):
|
|
132 self.expr = expr
|
148
|
133 def __repr__(self):
|
149
|
134 return 'RETURN STATEMENT'
|
148
|
135
|
|
136 class Assignment(Node):
|
|
137 def __init__(self, lval, rval):
|
|
138 self.lval = lval
|
|
139 self.rval = rval
|
|
140 def __repr__(self):
|
|
141 return 'ASSIGNMENT'
|
|
142
|
167
|
143 class FunctionCall(Node):
|
148
|
144 def __init__(self, proc, args):
|
|
145 self.proc = proc
|
|
146 self.args = args
|
|
147 def __repr__(self):
|
|
148 return 'CALL {0} '.format(self.proc)
|
|
149
|
|
150 class IfStatement(Node):
|
158
|
151 def __init__(self, condition, truestatement, falsestatement):
|
148
|
152 self.condition = condition
|
|
153 self.truestatement = truestatement
|
|
154 self.falsestatement = falsestatement
|
|
155 def __repr__(self):
|
|
156 return 'IF-statement'
|
|
157
|
|
158 class WhileStatement(Node):
|
|
159 def __init__(self, condition, statements):
|
|
160 self.condition = condition
|
|
161 self.dostatements = statements
|
|
162 def __repr__(self):
|
|
163 return 'WHILE-statement'
|
|
164
|