<commit_before>#!/usr/bin/env python
#-*- coding: utf-8 -*-

from __future__ import print_function
import argparse
import os
import sys
import ply.lex as lex
import ply.yacc as yacc
import asmtokens
import asmgrammar
import asmsemantic
import asmcodegen

def read_file(filename):
    "Read the contents of a file into memory."
    if not os.path.isfile(filename):
        print("The file '{}' does not exists.".format(filename), file=sys.stderr)
        sys.exit(1)
    try:
        with open(filename) as f:
            contents = f.read()
        return contents
    except IOError:
        print("Error reading file '{}'.".format(filename), file=sys.stderr)
        sys.exit(1)

def compiler(input_file, output_file, var_name):
    "Run the compiler on the source file."
    const_table = {}
    data_table = {}
    inst_table = {}

    asmlexer = lex.lex(module=asmtokens)
    asmparser = yacc.yacc(module=asmgrammar, write_tables=0, debug=0)

    input_string = read_file(input_file)
    ast = asmparser.parse(input_string, lexer=asmlexer)
    asmsemantic.semantic_analysis(ast, const_table, data_table, inst_table)

    with open(output_file, 'w+') as f:
        asmcodegen.codegen(ast, data_table, inst_table, var_name=var_name, outfile=f)

def main():
    "Parse the command line arguments and invoke the compiler."
    parser = argparse.ArgumentParser(
            description='Assembler for an extended 68HC11 clone.',
            epilog='As private parts to the gods are we, they play with us for their sport.')
    parser.add_argument('file', help='the source file')
    parser.add_argument('-o', '--output-file',
            help='the output file')
    parser.add_argument('-n', '--name', default='memory',
            help='the name of the memory variable')
    args = parser.parse_args()

    output_file = args.output_file
    if output_file is None:
        filename, ext = os.path.splitext(args.file)
        output_file = filename + '.vhd'

    compiler(args.file, output_file, args.name)

if __name__ == '__main__':
    main()
<commit_msg>Version option in CLI<commit_after>#!/usr/bin/env python
#-*- coding: utf-8 -*-

from __future__ import print_function
import argparse
import pkg_resources
import os
import sys
import ply.lex as lex
import ply.yacc as yacc
import asmtokens
import asmgrammar
import asmsemantic
import asmcodegen

def read_file(filename):
    "Read the contents of a file into memory."
    if not os.path.isfile(filename):
        print("The file '{}' does not exists.".format(filename), file=sys.stderr)
        sys.exit(1)
    try:
        with open(filename) as f:
            contents = f.read()
        return contents
    except IOError:
        print("Error reading file '{}'.".format(filename), file=sys.stderr)
        sys.exit(1)

def compiler(input_file, output_file, var_name):
    "Run the compiler on the source file."
    const_table = {}
    data_table = {}
    inst_table = {}

    asmlexer = lex.lex(module=asmtokens)
    asmparser = yacc.yacc(module=asmgrammar, write_tables=0, debug=0)

    input_string = read_file(input_file)
    ast = asmparser.parse(input_string, lexer=asmlexer)
    asmsemantic.semantic_analysis(ast, const_table, data_table, inst_table)

    with open(output_file, 'w+') as f:
        asmcodegen.codegen(ast, data_table, inst_table, var_name=var_name, outfile=f)

def main():
    "Parse the command line arguments and invoke the compiler."
    parser = argparse.ArgumentParser(
            description='Assembler for an extended 68HC11 clone.',
            epilog='As private parts to the gods are we, they play with us for their sport.')
    parser.add_argument('file', help='the source file')
    parser.add_argument('-o', '--output-file',
            help='the output file')
    parser.add_argument('-n', '--name', default='memory',
            help='the name of the memory variable')
    version = 'asm2d {}'.format(pkg_resources.require('asm2d')[0].version)
    parser.add_argument('-v', '--version', action='version', version=version)
    args = parser.parse_args()

    output_file = args.output_file
    if output_file is None:
        filename, ext = os.path.splitext(args.file)
        output_file = filename + '.vhd'

    compiler(args.file, output_file, args.name)

if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.11604651162790698, 'fraction_numerical': 0.004883720930232558, 'mean_word_length': 3.279601990049751, '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': '1954817', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1346, 'n_words': 370}