comparison python/c3/astnodes.py @ 213:003c8a976fff

Merge of semantics and parser again ..
author Windel Bouwman
date Fri, 05 Jul 2013 11:18:48 +0200
parents 62386bcee1ba
children c1ccb1cb4cef
comparison
equal deleted inserted replaced
212:62386bcee1ba 213:003c8a976fff
1 """ 1 """
2 AST nodes for the c3 language. 2 AST (abstract syntax tree) nodes for the c3 language.
3 The tree is build by the parser.
4 Then it is checked
5 Finally code is generated from it.
3 """ 6 """
4 7
5 class Node: 8 class Node:
6 pass 9 pass
7 10
11 # Modules
12 class Package(Node):
13 def __init__(self, name, loc):
14 self.name = name
15 self.loc = loc
16 self.declarations = []
17 def __repr__(self):
18 return 'PACKAGE {}'.format(self.name)
19
8 class Designator(Node): 20 class Designator(Node):
9 def __init__(self, tname): 21 def __init__(self, tname, loc):
10 self.tname = tname 22 self.tname = tname
11 def __repr__(self): 23 self.loc = loc
12 return 'DESIGNATOR {}'.format(self.tname) 24 def __repr__(self):
25 return 'DESIGNATOR {}'.format(self.tname)
13 26
14 """ 27 """
15 Type classes 28 Type classes
29
30 types must be comparable.
31
32 There are the following types:
33 - base type
34 - struct type
35 - pointer type
36 - typedef type
37 - function type
16 """ 38 """
17 39
18 class Type(Node): 40 class Type(Node):
19 def isType(self, b): 41 def isType(self, b):
20 return isType(self, b) 42 return isType(self, b)
31 self.returntype = returntype 53 self.returntype = returntype
32 def __repr__(self): 54 def __repr__(self):
33 params = ', '.join([str(v) for v in self.parametertypes]) 55 params = ', '.join([str(v) for v in self.parametertypes])
34 return '{1} f({0})'.format(params, self.returntype) 56 return '{1} f({0})'.format(params, self.returntype)
35 57
58 class PointerType(Type):
59 def __init__(self, ptype):
60 self.ptype = ptype
61
62 class StructureType(Type):
63 def __init__(self, mems):
64 self.mems = mems
65
36 class DefinedType(Type): 66 class DefinedType(Type):
37 def __init__(self, name, typ): 67 def __init__(self, name, typ):
38 self.name = name 68 self.name = name
39 self.typ = typ 69 self.typ = typ
40 def __repr__(self): 70 def __repr__(self):
41 return 'Named type {0} of type {1}'.format(self.name, self.typ) 71 return 'Named type {0} of type {1}'.format(self.name, self.typ)
42 72
43 # Variables, parameters, local variables, constants: 73 # Variables, parameters, local variables, constants:
44 class Symbol(Node): 74 class Symbol(Node):
45 def __init__(self, name): 75 def __init__(self, name):
46 self.name = name 76 self.name = name
47 self.refs = [] 77 self.refs = []
48 def addRef(self, r): 78 def addRef(self, r):
49 self.refs.append(r) 79 self.refs.append(r)
50 @property 80 @property
51 def References(self): 81 def References(self):
52 return self.refs 82 return self.refs
53 83
54 class Constant(Symbol): 84 class Constant(Symbol):
55 def __init__(self, name, typ, value): 85 def __init__(self, name, typ, value):
56 super().__init__(name) 86 super().__init__(name)
57 self.typ = typ 87 self.typ = typ
58 self.value = value 88 self.value = value
59 def __repr__(self): 89 def __repr__(self):
60 return 'CONSTANT {0} = {1}'.format(self.name, self.value) 90 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
61 91
62 class Variable(Symbol): 92 class Variable(Symbol):
63 def __init__(self, name, typ, ival=None): 93 def __init__(self, name, typ):
64 super().__init__(name) 94 super().__init__(name)
65 self.typ = typ 95 self.typ = typ
66 self.ival = ival 96 self.ival = None
67 self.isLocal = False 97 self.isLocal = False
68 self.isReadOnly = False 98 self.isReadOnly = False
69 self.isParameter = False 99 self.isParameter = False
70 def __repr__(self): 100 def __repr__(self):
71 return '{}'.format(self.name) 101 return '{}'.format(self.name)
72 102
73 # Procedure types 103 # Procedure types
74 class Function(Symbol): 104 class Function(Symbol):
75 """ Actual implementation of a function """ 105 """ Actual implementation of a function """
76 def __init__(self, name): 106 def __init__(self, name, loc):
77 super().__init__(name) 107 super().__init__(name)
78 def __repr__(self): 108 self.loc = loc
79 return '{}'.format(self.name) 109
110 def __repr__(self):
111 return '{}'.format(self.name)
80 112
81 # Operations / Expressions: 113 # Operations / Expressions:
82 class Unop(Node): 114 class Unop(Node):
83 def __init__(self, a, op): 115 def __init__(self, a, op):
84 self.a = a 116 self.a = a
85 self.op = op 117 self.op = op
86 def __repr__(self): 118 def __repr__(self):
87 return 'UNOP {}'.format(self.op) 119 return 'UNOP {}'.format(self.op)
88 120
89 class Binop(Node): 121 class Binop(Node):
90 def __init__(self, a, op, b): 122 def __init__(self, a, op, b, loc):
91 self.a = a 123 self.a = a
92 self.b = b 124 self.b = b
93 self.op = op # Operation: '+', '-', '*', '/', 'mod' 125 self.op = op # Operation: '+', '-', '*', '/', 'mod'
126 self.loc = loc
94 def __repr__(self): 127 def __repr__(self):
95 return 'BINOP {}'.format(self.op) 128 return 'BINOP {}'.format(self.op)
96 129
97 class VariableUse(Node): 130 class VariableUse(Node):
98 def __init__(self, target): 131 def __init__(self, target, loc):
99 self.target = target 132 self.target = target
133 self.loc = loc
100 def __repr__(self): 134 def __repr__(self):
101 nm = self.target.name if hasattr(self.target, 'name') else '' 135 nm = self.target.name if hasattr(self.target, 'name') else ''
102 return 'VAR USE {}'.format(nm) 136 return 'VAR USE {}'.format(nm)
103 137
104 class Literal(Node): 138 class Literal(Node):
105 def __init__(self, val): 139 def __init__(self, val, loc):
106 self.val = val 140 self.val = val
141 self.loc = loc
107 def __repr__(self): 142 def __repr__(self):
108 return 'LITERAL {}'.format(self.val) 143 return 'LITERAL {}'.format(self.val)
109 144
110 # Modules
111 class Package(Node):
112 def __init__(self, name):
113 self.name = name
114 def __repr__(self):
115 return 'PACKAGE {}'.format(self.name)
116 145
117 # Statements 146 # Statements
118 class CompoundStatement(Node): 147 class CompoundStatement(Node):
119 def __init__(self, statements): 148 def __init__(self, statements):
120 self.statements = statements 149 self.statements = statements
130 self.expr = expr 159 self.expr = expr
131 def __repr__(self): 160 def __repr__(self):
132 return 'RETURN STATEMENT' 161 return 'RETURN STATEMENT'
133 162
134 class Assignment(Node): 163 class Assignment(Node):
135 def __init__(self, lval, rval): 164 def __init__(self, lval, rval, loc):
136 self.lval = lval 165 self.lval = lval
137 self.rval = rval 166 self.rval = rval
167 self.loc = loc
138 def __repr__(self): 168 def __repr__(self):
139 return 'ASSIGNMENT' 169 return 'ASSIGNMENT'
140 170
141 class FunctionCall(Node): 171 class FunctionCall(Node):
142 def __init__(self, proc, args): 172 def __init__(self, proc, args, loc):
143 self.proc = proc 173 self.proc = proc
144 self.args = args 174 self.args = args
175 self.loc = loc
145 def __repr__(self): 176 def __repr__(self):
146 return 'CALL {0} '.format(self.proc) 177 return 'CALL {0} '.format(self.proc)
147 178
148 class IfStatement(Node): 179 class IfStatement(Node):
149 def __init__(self, condition, truestatement, falsestatement): 180 def __init__(self, condition, truestatement, falsestatement, loc):
150 self.condition = condition 181 self.condition = condition
151 self.truestatement = truestatement 182 self.truestatement = truestatement
152 self.falsestatement = falsestatement 183 self.falsestatement = falsestatement
184 self.loc = loc
153 def __repr__(self): 185 def __repr__(self):
154 return 'IF-statement' 186 return 'IF-statement'
155 187
156 class WhileStatement(Node): 188 class WhileStatement(Node):
157 def __init__(self, condition, statement): 189 def __init__(self, condition, statement, loc):
158 self.condition = condition 190 self.condition = condition
159 self.dostatement = statement 191 self.statement = statement
192 self.loc = loc
160 def __repr__(self): 193 def __repr__(self):
161 return 'WHILE-statement' 194 return 'WHILE-statement'
162 195