git @ Cat's Eye Technologies Dieter / master src / dieter / ast.py
master

Tree @master (Download .tar.gz)

ast.py @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# -*- coding: utf-8 -*-

"""
ast.py -- Abstract Syntax Trees for the Dieter programming language.
$Id: ast.py 398 2010-02-04 00:20:41Z cpressey $
"""

import logging

import dieter.type as type

logger = logging.getLogger("ast")


class AST:
    """Class representing nodes in an abstract syntax tree.
    
    Each node has a type.  Initially this is None, meaning
    no type has been assigned.  This is not the same as
    VoidType.
    
    Calling the typecheck method of a node attempts to
    compute the types of that node, and all of its children,
    in the given typing context.

    """

    type = None

    def typecheck(self, context):
        """Checks that this part of the AST is well-typed, and set the type
        attribute of the AST to the type.

        Returns nothing.  Raises an exception if AST is ill-typed.
        (This method is abstract, and is only included on AST for the sake
        of documentation.)

        """
        raise NotImplementedError


class Program(AST):
    def __init__(self):
        self.forwards = []
        self.modules = []
        self.orderings = []

    def add_forward(self, forward):
        self.forwards.append(forward)

    def add_module(self, module):
        self.modules.append(module)

    def add_ordering(self, ordering):
        self.orderings.append(ordering)

    def dump(self, indent):
        d = ('  ' * indent) + "program\n"
        for forward in self.forwards:
            d += forward.dump(indent + 1)
        for ordering in self.orderings:
            d += ordering.dump(indent + 1)
        for module in self.modules:
            d += module.dump(indent + 1)
        return d

    def typecheck(self, context):
        for forward in self.forwards:
            forward.typecheck(context)
        for ordering in self.orderings:
            ordering.typecheck(context)
        for module in self.modules:
            if module.fails:
                logger.info("typechecking module " + module.name +  " (intends to fail)")
                failed = False
                try:
                    module.typecheck(context)
                except context.TypingError as e:
                    logger.info("caught TypingError " + str(e))
                    failed = True
                except Exception as e:
                    logger.info("caught Exception " + str(e))
                finally:
                    if not failed:
                        raise context.TypingError("module claimed to fail typechecking but didn't")
            else:
                logger.info("typechecking module " + module.name +  " (intends to succeed)")
                module.typecheck(context)


class Ordering(AST):
    def __init__(self, before, after):
        self.before = before
        self.after = after

    def dump(self, indent):
        d = ('  ' * indent) + "order " + self.before + " < " + self.after + "\n"
        return d

    def typecheck(self, context):
        # XXX context.add_ordering(self.before, self.after)
        pass


class FwdDecl(AST):
    def __init__(self, name, type_expr):
        self.name = name
        self.type_expr = type_expr

    def dump(self, indent):
        d = ('  ' * indent) + 'forward ' + self.name + " : \n" + self.type_expr.dump(indent+1) + "\n"
        return d

    def typecheck(self, context):
        self.type_expr.typecheck(context)
        self.type = self.type_expr.type
        context.associate(self.name, self.type)


### Modules...

class Module(AST):
    def __init__(self, name, fails):
        self.name = name
        self.fails = fails
        self.locals = []
        self.procs = []

    def add_local(self, local):
        self.locals.append(local)

    def add_proc(self, proc):
        self.procs.append(proc)

    def dump(self, indent):
        d = ('  ' * indent) + 'module ' + self.name + "\n"
        for local in self.locals:
            d += local.dump(indent + 1)
        for proc in self.procs:
            d += proc.dump(indent + 1)
        return d

    def typecheck(self, context):
        logger.info("typechecking module " + self.name)
        context.associate_qualifier(self.name)
        # make a new context for this module, to contain module-local variables
        my_context = context.new_module(self)
        for local in self.locals:
            local.typecheck(my_context)
        for proc in self.procs:
            proc.typecheck(my_context)


class VarDecl(AST):
    def __init__(self, name, type_expr):
        self.name = name
        self.type_expr = type_expr

    def dump(self, indent):
        d = ('  ' * indent) + ('var ' + self.name + ' : ' +
                               self.type_expr.dump(0) + "\n")
        return d

    def typecheck(self, context):
        self.type_expr.typecheck(context)
        self.type = self.type_expr.type
        context.associate(self.name, self.type)


class ProcDecl(AST):
    def __init__(self, name):
        self.name = name
        self.args = []
        self.locals = []
        self.return_type_expr = None
        self.body = None

    def add_arg(self, arg):
        self.args.append(arg)

    def add_local(self, local):
        self.locals.append(local)

    def set_body(self, body):
        self.body = body

    def set_return_type_expr(self, type_expr):
        self.return_type_expr = type_expr

    def dump(self, indent):
        d = ('  ' * indent) + ('procedure ' + self.name + " : " +
                               self.return_type_expr.dump(0) + "\n")
        for arg in self.args:
            d += arg.dump(indent + 1)
        for local in self.locals:
            d += local.dump(indent + 1)
        d += self.body.dump(indent + 1)
        return d

    def typecheck(self, context):
        self.return_type_expr.typecheck(context)
        return_type = self.return_type_expr.type
        proc_type = type.TypeProc(return_type)
        # make a new context for this procedure, to contain args and local variables
        my_context = context.new_procedure(self)
        for arg in self.args:
            arg.typecheck(my_context)
            proc_type.add_arg_type(arg.type)
        for local in self.locals:
            local.typecheck(my_context)
        context.global_context().associate(self.name, proc_type)
        self.body.typecheck(my_context)


### ... statements ...

class CompoundStatement(AST):
    def __init__(self):
        self.steps = []

    def add_step(self, step):
        self.steps.append(step)

    def dump(self, indent):
        d = ('  ' * indent) + "begin\n"
        for step in self.steps:
            d += step.dump(indent + 1)
        d += ('  ' * indent) + "end\n"
        return d

    def typecheck(self, context):
        for step in self.steps:
            step.typecheck(context)

class IfStatement(AST):
    def __init__(self, test, then_stmt, else_stmt):
        self.test = test
        self.then_stmt = then_stmt
        self.else_stmt = else_stmt

    def dump(self, indent):
        d = ('  ' * indent) + "if\n"
        d += self.test.dump(indent + 1)
        d += ('  ' * indent) + "then\n"
        d += self.then_stmt.dump(indent + 1)
        if self.else_stmt != None:
            d += ('  ' * indent) + "else\n"
            d += self.else_stmt.dump(indent + 1)
        return d

    def typecheck(self, context):
        self.test.typecheck(context)
        context.assert_equiv("if", type.TypeBool(), self.test.type)
        self.then_stmt.typecheck(context)
        self.else_stmt.typecheck(context)


class WhileStatement(AST):
    def __init__(self, test, body):
        self.test = test
        self.body = body

    def dump(self, indent):
        d = ('  ' * indent) + "while\n"
        d += self.test.dump(indent + 1)
        d = ('  ' * indent) + "do\n"
        d += self.body.dump(indent + 1)
        return d

    def typecheck(self, context):
        self.test.typecheck(context)
        context.assert_equiv("while", self.test.type, type.TypeBool())
        self.body.typecheck(context)


class ReturnStatement(AST):
    def __init__(self, expr):
        self.expr = expr

    def dump(self, indent):
        d = ('  ' * indent) + "return\n"
        d += self.expr.dump(indent + 1)
        return d

    def typecheck(self, context):
        self.expr.typecheck(context)
        return_type = self.expr.type
        context.assert_equiv("return", return_type, context.get_procedure().return_type_expr.type)


class CallStatement(AST):
    def __init__(self, name):
        self.name = name
        self.args = []

    def add_arg(self, arg):
        self.args.append(arg)

    def dump(self, indent):
        d = ('  ' * indent) + self.name + "(\n"
        for arg in self.args:
            d += arg.dump(indent + 1)
        d += ('  ' * indent) + ")\n"
        return d

    def typecheck(self, context):
        logger.info("typechecking procedure call to " + self.name)
        arg_types = []
        for arg in self.args:
            arg.typecheck(context)
            arg_types.append(arg.type)
        return_type = context.check_call(self.name, arg_types)
        self.type = return_type


class AssignStatement(AST):
    def __init__(self, name):
        self.name = name
        self.index = None
        self.expr = None

    def set_index(self, index):
        self.index = index

    def set_expr(self, expr):
        self.expr = expr

    def dump(self, indent):
        d = ('  ' * indent) + self.name + " "
        if self.index != None:
            d += ('  ' * indent) + "[\n"
            d += self.index.dump(indent + 1)
            d += ('  ' * indent) + "] "
        d += ":=\n"
        d += self.expr.dump(indent + 1)
        return d

    def typecheck(self, context):
        lhs_type = context.get_type(self.name)
        # if this variable is a map type then make sure its index is the right type
        if isinstance(lhs_type, type.TypeMap):
            assert self.index != None
            self.index.typecheck(context)
            index_type = self.index.type
            if lhs_type.from_type is not None:
                context.assert_equiv("index", lhs_type.from_type, index_type)
            lhs_type = lhs_type.to_type
        self.expr.typecheck(context)
        rhs_type = self.expr.type
        context.assert_equiv("assignment", lhs_type, rhs_type)


### ... expressions ...

class IntConstExpr(AST):
    def __init__(self, value):
        self.value = value

    def dump(self, indent):
        d = ('  ' * indent) + str(self.value) + "\n"
        return d

    def typecheck(self, context):
        self.type = type.TypeInt()


class StringConstExpr(AST):
    def __init__(self, value):
        self.value = value

    def dump(self, indent):
        d = ('  ' * indent) + "\"" + self.value + "\"\n"
        return d

    def typecheck(self, context):
        self.type = type.TypeString()


class VarRefExpr(AST):
    def __init__(self, name):
        self.name = name
        self.index = None

    def set_index(self, index):
        self.index = index

    def dump(self, indent):
        d = ('  ' * indent) + str(self.name) + " "
        if self.index != None:
            d += ('  ' * indent) + "[\n"
            d += self.index.dump(indent + 1)
            d += ('  ' * indent) + "]"
        d += "\n"
        return d

    def typecheck(self, context):
        self.type = context.get_type(self.name)
        # if this variable is a map type then make sure its index is the right type
        if isinstance(self.type, type.TypeMap):
            assert self.index != None
            self.index.typecheck(context)
            index_type = self.index.type
            if self.type.from_type is not None:
                context.assert_equiv("index", self.type.from_type, index_type)
            self.type = self.type.to_type


class SuperExpr(AST):
    def __init__(self):
        pass

    def dump(self, indent):
        d = ('  ' * indent) + "super\n"
        return d

    def typecheck(self, context):
        # same as the return type of the current procedure
        self.type = context.get_procedure().return_type


class BestowExpr(AST):
    def __init__(self, qual, expr):
        self.qual = qual
        self.expr = expr

    def dump(self, indent):
        d = ('  ' * indent) + "bestow " + self.qual + "\n"
        d += self.expr.dump(indent + 1)
        return d

    def typecheck(self, context):
        if self.qual != context.get_module().name:
            raise context.TypingError("type operation on %s used outside of its module (in module %s)" %
              (self.qual, context.get_module().name))
        else:
            self.expr.typecheck(context)
            self.type = self.expr.type.qualify(self.qual)


class CallExpr(AST):
    def __init__(self, name):
        self.name = name
        self.args = []

    def add_arg(self, arg):
        self.args.append(arg)

    def dump(self, indent):
        d = ('  ' * indent) + self.name + "(\n"
        for arg in self.args:
            d += arg.dump(indent + 1)
        d += ('  ' * indent) + ")\n"
        return d

    def typecheck(self, context):
        logger.info("typechecking function call to " + self.name)
        arg_types = []
        for arg in self.args:
            arg.typecheck(context)
            arg_types.append(arg.type)
        return_type = context.check_call(self.name, arg_types)
        self.type = return_type


### ... type expressions ...

class PrimitiveTypeExpr(AST):
    def __init__(self, token):
        self.token = token

    def __str__(self):
        return self.token

    def dump(self, indent):
        d = ('  ' * indent) + self.token
        return d

    def typecheck(self, context):
        map = {
            'void': type.TypeVoid,
            'bool': type.TypeBool,
            'int': type.TypeInt,
            'rat': type.TypeRat,
            'string': type.TypeString,
            'ref': type.TypeRef
        }
        self.type = map[self.token]()


class MapTypeExpr(AST):
    def __init__(self, range, domain):
        self.range = range
        self.domain = domain

    def dump(self, indent):
        d = ('  ' * indent) + "map from " + self.range.dump(0)
        if self.domain is not None:
            d = d + " to " + self.domain.dump(0)
        return d

    def typecheck(self, context):
        self.range.typecheck(context)
        if self.domain is None:
            self.type = type.TypeMap(self.range.type, None)
        else:
            self.domain.typecheck(context)
            self.type = type.TypeMap(self.range.type, self.domain.type)


class ProcTypeExpr(AST):
    def __init__(self, arg_type_exprs, return_type_expr):
        self.arg_type_exprs = arg_type_exprs
        self.return_type_expr = return_type_expr

    def dump(self, indent):
        d = ('  ' * indent) + 'proc('
        for arg_type_expr in self.arg_type_exprs:
            d += arg_type_expr.dump(0) + ","
        d = d[:-1] + "): " + self.return_type_expr.dump(0)
        return d

    def add_arg_type_expr(self, arg_type_expr):
        self.arg_type_exprs.append(arg_type_expr)

    def typecheck(self, context):
        for arg_type_expr in self.arg_type_exprs:
            arg_type_expr.typecheck(context)
        self.return_type_expr.typecheck(context)
        self.type = type.TypeProc(self.return_type_expr.type)
        for arg_type_expr in self.arg_type_exprs:
            self.type.add_arg_type(arg_type_expr.type)


class QualifiedTypeExpr(AST):
    def __init__(self, qual, type_expr):
        self.qual = qual
        self.type_expr = type_expr

    def dump(self, indent):
        return self.qual + " " + self.type_expr.dump(0)

    def typecheck(self, context):
        self.type_expr.typecheck(context)
        self.type = self.type_expr.type.qualify(self.qual)


class TypeVariableExpr(AST):
    def __init__(self, name):
        self.name = name

    def dump(self, indent):
        return "*" + self.name

    def typecheck(self, context):
        self.type = type.TypeVar(self.name)