<commit_before>#!/usr/bin/env python3
import basic
import expression
import statement
import preprocessor
import program

# This look-up table, initialised upon the first call to `from_json`, maps CGum
# AST types to their representative Python classes, using the type ID.
__CODE_CLASS_LOOKUP = {}

# Constructs the AST class look-up table. Used by `lookup_table` to build the
# look-up table upon its first request.
def __build_lookup_table(typ):
    if hasattr(typ, 'CODE'):
        print("Registering AST type into lookup table [%d]: %s" % (typ.CODE, typ.__name__))
        __CODE_CLASS_LOOKUP[typ.CODE] = typ
    for sub_typ in typ.__subclasses__():
        __build_lookup_table(sub_typ)

# Used to return the type ID to Python class look-up table.
# Responsible for overseeing the construction of the look-up table on its first
# invocation.
def lookup_table():
    if not __CODE_CLASS_LOOKUP:
        __build_lookup_table(basic.Node)
    return __CODE_CLASS_LOOKUP

# Parses a JSON CGum AST into an equivalent, Python-based C AST
def from_json(jsn):
    pass

if __name__ == "__main__":
    print(lookup_table())<commit_msg>more work on loader<commit_after>#!/usr/bin/env python3
import os.path
import json
import basic
import expression
import statement
import preprocessor
import program

# This look-up table, initialised upon the first call to `from_json`, maps CGum
# AST types to their representative Python classes, using the type ID.
__CODE_CLASS_LOOKUP = {}

# Debugging flag
__DEBUGGING = True

# Constructs the AST class look-up table. Used by `lookup_table` to build the
# look-up table upon its first request.
def __build_lookup_table(typ):
    if hasattr(typ, 'CODE'):
        print("Registering AST type [%d]: %s" % (typ.CODE, typ.__name__))
        __CODE_CLASS_LOOKUP[typ.CODE] = typ
    for sub_typ in typ.__subclasses__():
        __build_lookup_table(sub_typ)

# Used to return the type ID to Python class look-up table.
# Responsible for overseeing the construction of the look-up table on its first
# invocation.
def lookup_table():
    if not __CODE_CLASS_LOOKUP:
        __build_lookup_table(basic.Node)
    return __CODE_CLASS_LOOKUP

# Parses a JSON CGum AST into an equivalent, Python representation
def from_json(jsn):
    assert 'type' in jsn, "expected 'type' property in AST node"
    typid = int(jsn['type'])

    typ = lookup_table()[typid]
    assert not typ is None, \
        ("no Python representation of AST node with type %d found" % typid)
    print("Converting AST node of (Python) type: %s" % typ.__name__)

# Parses a JSON CGum AST, stored in a file at a specified location, into an
# equivalent, Python representation
def from_file(fn):
    print("Attempting to read CGum AST from a JSON file: %s" % fn)
    assert os.path.isfile(fn), "file not found"
    with open(fn, 'r') as f:
        from_json(json.load(f)['root'])
    print("Finished converting CGum AST from JSON into Python")

if __name__ == "__main__":

    # Let's try and build an example AST
    from_file("example.cgum.json")
{'fraction_non_alphanumeric': 0.08752879236590984, 'fraction_numerical': 0.0006581112207963146, 'mean_word_length': 3.9673202614379086, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3556485', 'n_tokens_mistral': 967, 'n_tokens_neox': 931, 'n_words': 352}