<commit_before>from atom import *
import sys

HDR = new_env('HDR', None)

def out(s):
    env(HDR).write(s)
    if not env(GENOPTS).quiet:
        sys.stdout.write(s)

def write_type(t):
    out(match(t,
        ('TPrim(PInt() or PBool())', lambda: 'int'),
        ('TPrim(PFloat())', lambda: 'float'),
        ('TVoid()', lambda: 'void'),
        ('_', lambda: 'void *')))

def write_params(ps):
    if len(ps) == 0:
        out('(void)')
        return
    first = True
    out('(')
    for p in ps:
        if first:
            first = False
        else:
            out(', ')
        write_type(p)
    out(')')

def write_func_decl(name, params, ret):
    write_type(ret)
    out(' %s' % (name,))
    write_params(params)
    out(';\n')

def write_decls(decls, name):
    guard = name.upper() + '_H'
    out('#ifndef %s\n#define %s\n\n' % (guard, guard))
    for v in decls.funcDecls:
        t = extrinsic(TypeOf, v)
        name = extrinsic(Name, v)
        match(t, ('TFunc(params, ret, _)', lambda params, ret:
                    write_func_decl(name, params, ret)))
    out('\n#endif /* %s */\n' % (guard,))

def write_export_header(mod, header, cname):
    f = open(header, 'wb')
    in_env(HDR, f, lambda: write_decls(mod.root, cname))
    f.close()

# vi: set sw=4 ts=4 sts=4 tw=79 ai et nocindent:
<commit_msg>Output env params in generated C headers<commit_after>from atom import *
import sys

HDR = new_env('HDR', None)

def out(s):
    env(HDR).write(s)
    if not env(GENOPTS).quiet:
        sys.stdout.write(s)

def write_type(t):
    out(match(t,
        ('TPrim(PInt() or PBool())', lambda: 'int'),
        ('TPrim(PFloat())', lambda: 'float'),
        ('TVoid()', lambda: 'void'),
        ('_', lambda: 'void *')))

def write_params(ps, meta):
    if len(ps) == 0 and not meta.takesEnv:
        out('(void)')
        return
    first = True
    out('(')
    for p in ps:
        if first:
            first = False
        else:
            out(', ')
        write_type(p)

    if meta.takesEnv:
        if first:
            first = False
        else:
            out(', ')
        out('void *')

    out(')')

def write_func_decl(name, params, ret, meta):
    write_type(ret)
    out(' %s' % (name,))
    write_params(params, meta)
    out(';\n')

def write_decls(decls, name):
    guard = name.upper() + '_H'
    out('#ifndef %s\n#define %s\n\n' % (guard, guard))
    for v in decls.funcDecls:
        t = extrinsic(TypeOf, v)
        name = extrinsic(Name, v)
        match(t, ('TFunc(params, ret, meta)', lambda params, ret, meta:
                    write_func_decl(name, params, ret, meta)))
    out('\n#endif /* %s */\n' % (guard,))

def write_export_header(mod, header, cname):
    f = open(header, 'wb')
    in_env(HDR, f, lambda: write_decls(mod.root, cname))
    f.close()

# vi: set sw=4 ts=4 sts=4 tw=79 ai et nocindent:

{'fraction_non_alphanumeric': 0.17657342657342656, 'fraction_numerical': 0.004195804195804196, 'mean_word_length': 2.3461988304093566, 'pattern_counts': {'":': 0, '<': 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': '7500821', 'n_tokens_mistral': 1066, 'n_tokens_neox': 1051, 'n_words': 262}