git @ Cat's Eye Technologies Tamsin / master src / tamsin / 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
# encoding: UTF-8

# Copyright (c)2014 Chris Pressey, Cat's Eye Technologies.
# Distributed under a BSD-style license; see LICENSE for more information.

# Note that __str__ and __repr__ perform very different tasks:
# __str__ : make a string that looks like a Tamsin term (reprify)
# __repr__ : make a string that is valid Python code for constructing the AST


from tamsin.term import Atom, Variable, Constructor


def format_list(l):
    if len(l) == 0:
        return 'nil'
    else:
        return 'list(%s, %s)' % (l[0], format_list(l[1:]))


class AST(object):
    def __unicode__(self):
        raise NotImplementedError(repr(self))


class Program(AST):
    def __init__(self, modlist):
        self.modlist = modlist

    def find_module(self, name):
        for m in self.modlist:
            if m.name == name:
                return m
        return None

    def find_production(self, prodref):
        module_name = prodref.module
        prod_name = prodref.name
        assert module_name != ''
        module = self.find_module(module_name)
        if not module:
            raise KeyError("no '%s' module defined" % module_name)
        production = module.find_production(prod_name)
        if not production:
            raise KeyError("no '%s:%s' production defined" %
                (module_name, prod_name)
            )
        return production

    def incorporate(self, other):
        """Add all Modules from other to self.  Changes self.

        """
        assert isinstance(other, Program)

        for module in other.modlist:
            modname = module.name
            if self.find_module(modname):
                raise KeyError("module '%s' already defined" % modname)
            self.modlist.append(module)

    def __repr__(self):
        return "Program(%r)" % self.modlist

    def __str__(self):
        return "program(%s)" % format_list(self.modlist)


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

    def find_production(self, name):
        prods = []
        for prod in self.prodlist:
            if prod.name == name:
                prods.append(prod)
        assert len(prods) in (0, 1), repr((name, prods))
        if not prods:
            return None
        return prods[0]

    def __repr__(self):
        return "Module(%r, %r)" % (self.name, self.prodlist)

    def __str__(self):
        return "module(%s, %s)" % (self.name, format_list(self.prodlist))


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

    def link(self, other):
        if self.next is None:
            self.next = other
        else:
            self.next.link(other)

    def __repr__(self):
        return "Production(%r, %r)" % (
            self.name,
            self.branches,
        )

    def __str__(self):
        return "production(%s, %s)" % (
            self.name,
            format_list(self.branches),
        )


class ProdBranch(AST):
    def __init__(self, formals, locals_, body):
        self.formals = formals
        self.locals_ = locals_
        self.body = body

    def __repr__(self):
        return u"Prodbranch(%r, %r, %r)" % (
            self.formals,
            self.locals_,
            self.body,
        )

    def __str__(self):
        return "prodbranch(%s, %s, %s)" % (
            format_list(self.formals),
            format_list(self.locals_),
            self.body,
        )


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

    def __repr__(self):
        return u"Prodref(%r, %r)" % (
            self.module,
            self.name
        )

    def __str__(self):
        return "prodref(%s, %s)" % (
            Atom(self.module).repr(),
            Atom(self.name).repr()
        )


class And(AST):
    def __init__(self, lhs, rhs):
        self.lhs = lhs
        self.rhs = rhs

    def __repr__(self):
        return u"And(%r, %r)" % (
            self.lhs,
            self.rhs
        )

    def __str__(self):
        return "and(%s, %s)" % (
            self.lhs,
            self.rhs
        )


class Or(AST):
    def __init__(self, lhs, rhs):
        self.lhs = lhs
        self.rhs = rhs

    def __repr__(self):
        return u"Or(%r, %r)" % (
            self.lhs,
            self.rhs
        )

    def __str__(self):
        return "or(%s, %s)" % (
            self.lhs,
            self.rhs
        )


class Not(AST):
    def __init__(self, rule):
        self.rule = rule

    def __repr__(self):
        return u"Not(%r)" % (
            self.rule
        )

    def __str__(self):
        return "not(%s)" % (
            self.rule
        )


class While(AST):
    def __init__(self, rule):
        self.rule = rule

    def __repr__(self):
        return u"While(%r)" % (
            self.rule
        )

    def __str__(self):
        return "while(%s)" % (
            self.rule
        )


class Call(AST):
    def __init__(self, prodref, args):
        self.prodref = prodref
        for a in args:
            assert isinstance(a, AST)
        self.args = args

    def __repr__(self):
        return u"Call(%r, %r)" % (
            self.prodref,
            self.args,
        )

    def __str__(self):        
        return "call(%s, %s)" % (
            self.prodref,
            format_list(self.args)
        )


class Send(AST):
    def __init__(self, rule, pattern):
        self.rule = rule
        self.pattern = pattern

    def __repr__(self):
        return u"Send(%r, %r)" % (self.rule, self.pattern)

    def __str__(self):
        return "send(%s, %s)" % (self.rule, self.pattern)


class Set(AST):
    def __init__(self, variable, texpr):
        self.variable = variable
        self.texpr = texpr

    def __repr__(self):
        return u"Set(%r, %r)" % (self.variable, self.texpr)

    def __str__(self):
        return "set(%s, %s)" % (self.variable, self.texpr)


class Concat(AST):
    def __init__(self, lhs, rhs):
        self.lhs = lhs
        self.rhs = rhs

    def __repr__(self):
        return u"Concat(%r, %r)" % (self.lhs, self.rhs)

    def __str__(self):
        return "concat(%s, %s)" % (self.lhs, self.rhs)


class Using(AST):
    def __init__(self, rule, prodref):
        self.rule = rule
        assert isinstance(prodref, Prodref)
        self.prodref = prodref

    def __repr__(self):
        return u"Using(%r, %r)" % (self.rule, self.prodref)

    def __str__(self):
        return "using(%s, %s)" % (self.rule, self.prodref)


class On(AST):
    def __init__(self, rule, texpr):
        self.rule = rule
        self.texpr = texpr

    def __repr__(self):
        return u"On(%r, %r)" % (self.rule, self.texpr)

    def __str__(self):
        return "on(%s, %s)" % (self.rule, self.texpr)


class Fold(AST):
    def __init__(self, rule, initial, tag):
        self.rule = rule
        self.initial = initial
        self.tag = tag

    def __repr__(self):
        return u"Fold(%r, %r, %r)" % (
            self.rule,
            self.initial,
            self.tag
        )

    def __str__(self):
        return "fold(%s, %s, %s)" % (
            self.rule,
            self.initial,
            self.tag or 'nil',
        )


class TermNode(AST):
    def __init__(self, *args):
        raise NotImplementedError("abstract class!")

    def collect_variables(self, variables):
        raise NotImplementedError

    def to_term(self):
        raise NotImplementedError


class AtomNode(TermNode):
    def __init__(self, text):
        self.text = text

    def __repr__(self):
        return u"AtomNode(%r)" % self.text

    def __str__(self):
        return "atom(%s)" % Atom(self.text).repr()

    def collect_variables(self, variables):
        pass

    def to_term(self):
        return Atom(self.text)


class VariableNode(TermNode):
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return u"VariableNode(%r)" % self.name

    def __str__(self):
        return "variable(%s)" % Atom(self.name).repr()

    def collect_variables(self, variables):
        variables.append(self)

    def to_term(self):
        return Variable(self.name)


class PatternVariableNode(TermNode):
    def __init__(self, name, index):
        self.name = name
        self.index = index

    def __repr__(self):
        return u"PatternVariableNode(%r, %r)" % (self.name, self.index)

    def __str__(self):
        return "patternvariable(%s, %s)" % (Atom(self.name).repr(), self.index)

    def collect_variables(self, variables):
        variables.append(self)

    def to_term(self):
        return Variable(self.name)


class ConstructorNode(TermNode):
    def __init__(self, text, contents):
        self.text = text
        self.contents = contents
        for c in self.contents:
            assert isinstance(c, TermNode)

    def __repr__(self):
        return u"ConstructorNode(%r, %r)" % (self.text, self.contents)

    def __str__(self):
        return "constructor(%s, %s)" % (
            Atom(self.text).repr(),
            format_list(self.contents)
        )

    def collect_variables(self, variables):
        for x in self.contents:
            x.collect_variables(variables)

    def to_term(self):
        return Constructor(self.text, [
            x.to_term() for x in self.contents
        ])