comparison python/c3/astnodes.py @ 149:74241ca312cc

Fixes on parser and semantics
author Windel Bouwman
date Fri, 01 Mar 2013 11:43:52 +0100
parents e5263f74b287
children 4ae0e02599de
comparison
equal deleted inserted replaced
148:e5263f74b287 149:74241ca312cc
62 print(b) 62 print(b)
63 Error('Not implemented {0}'.format(a)) 63 Error('Not implemented {0}'.format(a))
64 else: 64 else:
65 return False 65 return False
66 66
67 class Type: 67 class Type(Node):
68 def isType(self, b): 68 def isType(self, b):
69 return isType(self, b) 69 return isType(self, b)
70 70
71 class BaseType(Type): 71 class BaseType(Type):
72 def __init__(self, name): 72 def __init__(self, name):
91 # Variables, parameters, local variables, constants: 91 # Variables, parameters, local variables, constants:
92 class Symbol(Node): 92 class Symbol(Node):
93 pass 93 pass
94 94
95 class Constant(Symbol): 95 class Constant(Symbol):
96 def __init__(self, value, typ, name=None, public=False): 96 def __init__(self, value, name=None):
97 self.name = name 97 self.name = name
98 self.value = value 98 self.value = value
99 self.typ = typ
100 self.public = public
101 def __repr__(self): 99 def __repr__(self):
102 return 'CONSTANT {0} = {1}'.format(self.name, self.value) 100 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
103 101
104 class Variable(Symbol): 102 class Variable(Symbol):
105 def __init__(self, name, typ, public): 103 def __init__(self, name, typ):
106 self.name = name 104 self.name = name
107 self.typ = typ 105 self.typ = typ
108 self.public = public
109 self.isLocal = False 106 self.isLocal = False
110 self.isReadOnly = False 107 self.isReadOnly = False
111 self.isParameter = False 108 self.isParameter = False
112 def __repr__(self): 109 def __repr__(self):
113 txt = '[public] ' if self.public else '' 110 return 'VAR {0} : {1}'.format(self.name, self.typ)
114 return '{2}VAR {0} : {1}'.format(self.name, self.typ, txt)
115 111
116 class Parameter(Node): 112 class Parameter(Node):
117 """ A parameter has a passing method, name and typ """ 113 """ A parameter has a passing method, name and typ """
118 def __init__(self, name, typ): 114 def __init__(self, name, typ):
119 self.name = name 115 self.name = name
133 def __init__(self, a, op, b): 129 def __init__(self, a, op, b):
134 self.a = a 130 self.a = a
135 self.b = b 131 self.b = b
136 self.op = op # Operation: '+', '-', '*', '/', 'mod' 132 self.op = op # Operation: '+', '-', '*', '/', 'mod'
137 def __repr__(self): 133 def __repr__(self):
138 return 'BINOP {0} {1}'.format(self.op, self.typ) 134 return 'BINOP {0}'.format(self.op)
139 135
140 # Modules 136 # Modules
141 class Package(Node): 137 class Package(Node):
142 def __init__(self, name): 138 def __init__(self, name):
143 self.name = name 139 self.name = name
145 return 'PACKAGE {0}'.format(self.name) 141 return 'PACKAGE {0}'.format(self.name)
146 142
147 # Procedure types 143 # Procedure types
148 class Procedure(Symbol): 144 class Procedure(Symbol):
149 """ Actual implementation of a function """ 145 """ Actual implementation of a function """
150 def __init__(self, name, typ, block): 146 def __init__(self, name, typ=None, block=None):
151 self.name = name 147 self.name = name
152 self.block = block 148 self.body = block
153 self.typ = typ 149 self.typ = typ
154 def __repr__(self): 150 def __repr__(self):
155 return 'PROCEDURE {0} {1}'.format(self.name, self.typ) 151 return 'PROCEDURE {0} {1}'.format(self.name, self.typ)
156 152
157 # Statements 153 # Statements
159 def __init__(self, statements): 155 def __init__(self, statements):
160 self.statements = statements 156 self.statements = statements
161 def __repr__(self): 157 def __repr__(self):
162 return 'COMPOUND STATEMENT' 158 return 'COMPOUND STATEMENT'
163 159
164 class EmptyStatement(Node): 160 class ReturnStatement(Node):
161 def __init__(self, expr):
162 self.expr = expr
165 def __repr__(self): 163 def __repr__(self):
166 return 'EMPTY STATEMENT' 164 return 'RETURN STATEMENT'
167 165
168 class Assignment(Node): 166 class Assignment(Node):
169 def __init__(self, lval, rval): 167 def __init__(self, lval, rval):
170 self.lval = lval 168 self.lval = lval
171 self.rval = rval 169 self.rval = rval