annotate python/c3/astnodes.py @ 167:0b5b2ee6b435

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