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):
|
|
29 def __init__(self, parameters, returntype):
|
|
30 self.parameters = parameters
|
|
31 self.returntype = returntype
|
|
32 def __repr__(self):
|
165
|
33 params = ','.join([str(v) for v in self.parameters])
|
|
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):
|
163
|
97 typ = self.typ if hasattr(self, 'typ') else ''
|
|
98 return 'BINOP {0} {1}'.format(self.op, typ)
|
148
|
99
|
150
|
100 class VariableUse(Node):
|
|
101 def __init__(self, target):
|
|
102 self.target = target
|
|
103 def __repr__(self):
|
163
|
104 nm = self.target.name if hasattr(self.target, 'name') else ''
|
|
105 return 'VAR USE {0}'.format(nm)
|
|
106
|
|
107 class Literal(Node):
|
|
108 def __init__(self, val):
|
|
109 self.val = val
|
|
110 def __repr__(self):
|
|
111 return 'LITERAL {0}'.format(self.val)
|
150
|
112
|
148
|
113 # Modules
|
|
114 class Package(Node):
|
|
115 def __init__(self, name):
|
|
116 self.name = name
|
|
117 def __repr__(self):
|
|
118 return 'PACKAGE {0}'.format(self.name)
|
|
119
|
|
120 # Statements
|
|
121 class CompoundStatement(Node):
|
|
122 def __init__(self, statements):
|
|
123 self.statements = statements
|
|
124 def __repr__(self):
|
|
125 return 'COMPOUND STATEMENT'
|
|
126
|
155
|
127 class EmptyStatement(Node):
|
158
|
128 def __repr__(self):
|
|
129 return 'NOP'
|
155
|
130
|
149
|
131 class ReturnStatement(Node):
|
|
132 def __init__(self, expr):
|
|
133 self.expr = expr
|
148
|
134 def __repr__(self):
|
149
|
135 return 'RETURN STATEMENT'
|
148
|
136
|
|
137 class Assignment(Node):
|
|
138 def __init__(self, lval, rval):
|
|
139 self.lval = lval
|
|
140 self.rval = rval
|
|
141 def __repr__(self):
|
|
142 return 'ASSIGNMENT'
|
|
143
|
|
144 class ProcedureCall(Node):
|
|
145 def __init__(self, proc, args):
|
|
146 self.proc = proc
|
|
147 self.args = args
|
|
148 def __repr__(self):
|
|
149 return 'CALL {0} '.format(self.proc)
|
|
150
|
|
151 class IfStatement(Node):
|
158
|
152 def __init__(self, condition, truestatement, falsestatement):
|
148
|
153 self.condition = condition
|
|
154 self.truestatement = truestatement
|
|
155 self.falsestatement = falsestatement
|
|
156 def __repr__(self):
|
|
157 return 'IF-statement'
|
|
158
|
|
159 class WhileStatement(Node):
|
|
160 def __init__(self, condition, statements):
|
|
161 self.condition = condition
|
|
162 self.dostatements = statements
|
|
163 def __repr__(self):
|
|
164 return 'WHILE-statement'
|
|
165
|