Skip to content

Commit

Permalink
[refactor] Move more Id tables to build time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Oct 24, 2019
1 parent b37047a commit 27adf26
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 35 deletions.
19 changes: 10 additions & 9 deletions core/id_kind_gen.py
Expand Up @@ -109,15 +109,20 @@ def main(argv):
except IndexError:
raise RuntimeError('Action required')

# NOTE: This initialization must be identical to the one in core/meta.py. We
# do it here to avoid circular dependencies.
# TODO: Remove duplication in core/meta.py
ID_TO_KIND_INTEGERS = {}
BOOL_ARG_TYPES = {}
TEST_UNARY_LOOKUP = {}
TEST_BINARY_LOOKUP = {}
TEST_OTHER_LOOKUP = {}

ID_SPEC = id_kind.IdSpec({}, {})
ID_SPEC = id_kind.IdSpec(ID_TO_KIND_INTEGERS, BOOL_ARG_TYPES)

id_kind.AddKinds(ID_SPEC)
id_kind.AddBoolKinds(ID_SPEC) # must come second

id_kind.SetupTestBuiltin(ID_SPEC, {}, {}, {})
id_kind.SetupTestBuiltin(ID_SPEC, TEST_UNARY_LOOKUP, TEST_BINARY_LOOKUP,
TEST_OTHER_LOOKUP)

ids = ID_SPEC.id_str2int.items()
ids.sort(key=lambda pair: pair[1]) # Sort by ID
Expand Down Expand Up @@ -205,11 +210,7 @@ def main(argv):
# It's kind of weird to use the generated code to generate more code.
# Can we do this instead with the parsed module for "id" and "types.asdl"?

from core.meta import (
REDIR_DEFAULT_FD, REDIR_ARG_TYPES, BOOL_ARG_TYPES,
TEST_UNARY_LOOKUP, TEST_BINARY_LOOKUP, TEST_OTHER_LOOKUP,
ID_TO_KIND_INTEGERS,
)
from core.meta import REDIR_DEFAULT_FD, REDIR_ARG_TYPES
from _devbuild.gen.id_kind_asdl import Id_str, Kind_str
from _devbuild.gen.types_asdl import redir_arg_type_str, bool_arg_type_str

Expand Down
12 changes: 1 addition & 11 deletions core/id_kind_test.py
Expand Up @@ -14,9 +14,7 @@

from _devbuild.gen.id_kind_asdl import Id, Kind
from _devbuild.gen import syntax_asdl
from core.meta import (
IdInstance, ID_SPEC, BOOL_ARG_TYPES, _kind_sizes
)
from core.meta import IdInstance, ID_SPEC, _kind_sizes
from frontend.lexer import LookupKind


Expand Down Expand Up @@ -97,14 +95,6 @@ def testPrintStats(self):
big = [i for i in k if i > 8]
print('%d BIG groups: %s' % (len(big), sorted(big)))

PrintBoolTable()
print('---')


def PrintBoolTable():
for i, arg_type in BOOL_ARG_TYPES.items():
print('%-40s %s' % (IdInstance(i), arg_type))


if __name__ == '__main__':
unittest.main()
17 changes: 2 additions & 15 deletions core/meta.py
Expand Up @@ -20,34 +20,21 @@
from core.pyutil import _ResourceLoader


ID_TO_KIND_INTEGERS = {} # type: Dict[int, int]


# Do NOT create any any more instances of Id. Always use IdInstance().
def IdInstance(i):
# type: (int) -> Id_t
return ID_INSTANCES[i]


BOOL_ARG_TYPES = {} # type: Dict[int, bool_arg_type_t]

# Used by builtin_bracket.py
TEST_UNARY_LOOKUP = {} # type: Dict[str, int]
TEST_BINARY_LOOKUP = {} # type: Dict[str, int]
TEST_OTHER_LOOKUP = {} # type: Dict[str, int]


#
# Initialize Id and Kind
#

ID_SPEC = id_kind.IdSpec(ID_TO_KIND_INTEGERS, BOOL_ARG_TYPES)
ID_SPEC = id_kind.IdSpec({}, {})

id_kind.AddKinds(ID_SPEC)
id_kind.AddBoolKinds(ID_SPEC) # must come second
id_kind.SetupTestBuiltin(ID_SPEC,
TEST_UNARY_LOOKUP, TEST_BINARY_LOOKUP,
TEST_OTHER_LOOKUP)
id_kind.SetupTestBuiltin(ID_SPEC, {}, {}, {})

# Debug Stats
_kind_sizes = ID_SPEC.kind_sizes
Expand Down

0 comments on commit 27adf26

Please sign in to comment.