comparison python/c3/astnodes.py @ 148:e5263f74b287

Added c3 language frontend initial parser
author Windel Bouwman
date Fri, 01 Mar 2013 10:24:01 +0100
parents
children 74241ca312cc
comparison
equal deleted inserted replaced
147:4e79484a9d47 148:e5263f74b287
1 """
2 AST nodes for the c3 language.
3 """
4
5 class Node:
6 location = None
7 def getChildren(self):
8 children = []
9 members = dir(self)
10 for member in members:
11 member = getattr(self, member)
12 if isinstance(member, Node):
13 children.append(member)
14 elif type(member) is list:
15 for mi in member:
16 if isinstance(mi, Node):
17 children.append(mi)
18 return children
19
20
21 class Id(Node):
22 def __init__(self, tok, pub):
23 self.name = tok.val
24 self.is_public = pub
25 def __repr__(self):
26 return 'ID {0}'.format(self.name)
27
28 # Selectors:
29 class Designator(Node):
30 def __init__(self, obj, selectors, typ):
31 self.obj = obj
32 self.selectors = selectors
33 self.typ = typ
34 def __repr__(self):
35 return 'DESIGNATOR {0}, selectors {1}, type {2}'.format(self.obj, self.selectors, self.typ)
36
37 """
38 Type classes
39 """
40 def isType(a, b):
41 """ Compare types a and b and check if they are equal """
42 if type(a) is type(b):
43 if type(a) is BaseType:
44 return (a.name == b.name) and (a.size == b.size)
45 elif type(a) is ProcedureType:
46 if len(a.parameters) != len(b.parameters):
47 print('Number of parameters does not match')
48 return False
49 for aparam, bparam in zip(a.parameters, b.parameters):
50 if not isType(aparam.typ, bparam.typ):
51 print('Parameter {0} does not match parameter {1}'.format(aparam, bparam))
52 return False
53 if a.result is None:
54 # TODO: how to handle a None return type??
55 pass
56 if not isType(a.result, b.result):
57 print('Procedure return value mismatch {0} != {1}'.format(a.result, b.result))
58 return False
59 return True
60 else:
61 print(a)
62 print(b)
63 Error('Not implemented {0}'.format(a))
64 else:
65 return False
66
67 class Type:
68 def isType(self, b):
69 return isType(self, b)
70
71 class BaseType(Type):
72 def __init__(self, name):
73 self.name = name
74 def __repr__(self):
75 return '[TYPE {0}]'.format(self.name)
76
77 class FunctionType(Type):
78 def __init__(self, parameters, returntype):
79 self.parameters = parameters
80 self.returntype = returntype
81 def __repr__(self):
82 return '[PROCTYPE {0} RET {1}]'.format(self.parameters, self.returntype)
83
84 class DefinedType(Type):
85 def __init__(self, name, typ):
86 self.name = name
87 self.typ = typ
88 def __repr__(self):
89 return 'Named type {0} of type {1}'.format(self.name, self.typ)
90
91 # Variables, parameters, local variables, constants:
92 class Symbol(Node):
93 pass
94
95 class Constant(Symbol):
96 def __init__(self, value, typ, name=None, public=False):
97 self.name = name
98 self.value = value
99 self.typ = typ
100 self.public = public
101 def __repr__(self):
102 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
103
104 class Variable(Symbol):
105 def __init__(self, name, typ, public):
106 self.name = name
107 self.typ = typ
108 self.public = public
109 self.isLocal = False
110 self.isReadOnly = False
111 self.isParameter = False
112 def __repr__(self):
113 txt = '[public] ' if self.public else ''
114 return '{2}VAR {0} : {1}'.format(self.name, self.typ, txt)
115
116 class Parameter(Node):
117 """ A parameter has a passing method, name and typ """
118 def __init__(self, name, typ):
119 self.name = name
120 self.typ = typ
121 def __repr__(self):
122 return 'PARAM {0} {1}'.format(self.name, self.typ)
123
124 # Operations:
125 class Unop(Node):
126 def __init__(self, a, op):
127 self.a = a
128 self.op = op
129 def __repr__(self):
130 return 'UNOP {0}'.format(self.op)
131
132 class Binop(Node):
133 def __init__(self, a, op, b):
134 self.a = a
135 self.b = b
136 self.op = op # Operation: '+', '-', '*', '/', 'mod'
137 def __repr__(self):
138 return 'BINOP {0} {1}'.format(self.op, self.typ)
139
140 # Modules
141 class Package(Node):
142 def __init__(self, name):
143 self.name = name
144 def __repr__(self):
145 return 'PACKAGE {0}'.format(self.name)
146
147 # Procedure types
148 class Procedure(Symbol):
149 """ Actual implementation of a function """
150 def __init__(self, name, typ, block):
151 self.name = name
152 self.block = block
153 self.typ = typ
154 def __repr__(self):
155 return 'PROCEDURE {0} {1}'.format(self.name, self.typ)
156
157 # Statements
158 class CompoundStatement(Node):
159 def __init__(self, statements):
160 self.statements = statements
161 def __repr__(self):
162 return 'COMPOUND STATEMENT'
163
164 class EmptyStatement(Node):
165 def __repr__(self):
166 return 'EMPTY STATEMENT'
167
168 class Assignment(Node):
169 def __init__(self, lval, rval):
170 self.lval = lval
171 self.rval = rval
172 def __repr__(self):
173 return 'ASSIGNMENT'
174
175 class ProcedureCall(Node):
176 def __init__(self, proc, args):
177 self.proc = proc
178 self.args = args
179 def __repr__(self):
180 return 'CALL {0} '.format(self.proc)
181
182 class IfStatement(Node):
183 def __init__(self, condition, truestatement, falsestatement=None):
184 self.condition = condition
185 self.truestatement = truestatement
186 self.falsestatement = falsestatement
187 def __repr__(self):
188 return 'IF-statement'
189
190 class WhileStatement(Node):
191 def __init__(self, condition, statements):
192 self.condition = condition
193 self.dostatements = statements
194 def __repr__(self):
195 return 'WHILE-statement'
196