OILS / asdl / pybase.py View on Github | oils.pub

39 lines, 18 significant
1#!/usr/bin/env python2
2"""asdl/pybase.py is a runtime library for ASDL in Python"""
3from __future__ import print_function
4
5from mycpp import mylib
6
7from typing import TYPE_CHECKING
8if TYPE_CHECKING:
9 from _devbuild.gen.hnode_asdl import hnode_t
10 from asdl.runtime import TraversalState
11
12
13class SimpleObj(int):
14 """Base type of simple sum types, which are integers."""
15 # TODO: Get rid of this class? mycpp uses it to tell if it should generate
16 # h8_id or h8_id*.
17 pass
18
19
20class CompoundObj(object):
21 # The tag is set for variant types, which are subclasses of sum types.
22 # It's not set for product types.
23 _type_tag = 0 # Starts at 1. Zero is invalid
24
25 def PrettyTree(self, do_abbrev, trav=None):
26 # type: (bool, TraversalState) -> hnode_t
27 raise NotImplementedError(self.__class__.__name__)
28
29 def __repr__(self):
30 # type: () -> str
31 """Print this ASDL object nicely."""
32
33 # TODO: Break this circular dependency.
34 from asdl import format as fmt
35
36 f = mylib.BufWriter()
37 tree = self.PrettyTree(False)
38 fmt.HNodePrettyPrint(tree, f)
39 return f.getvalue()