コード例 #1
0
ファイル: hw2.py プロジェクト: zmckee2/CPSC326
def hw2(file_stream):
    the_lexer = lexer.Lexer(file_stream)
    the_token = the_lexer.next_token()
    while the_token.tokentype != token.EOS:
        print(the_token)
        the_token = the_lexer.next_token()
    print(the_token)
コード例 #2
0
def main(file_stream):
    the_lexer = lexer.Lexer(file_stream)
    the_parser = parser.Parser(the_lexer)
    stmt_list = the_parser.parse()
    the_type_checker = type_checker.TypeChecker()
    stmt_list.accept(the_type_checker)
    the_interpreter = interpreter.Interpreter()
    the_interpreter.run(stmt_list)
コード例 #3
0
def main(filename):
    try:
        file_stream = open(filename, 'r')
        the_lexer = lexer.Lexer(file_stream)
        the_parser = parser.Parser(the_lexer)
        hw3(the_parser)
        file_stream.close()
    except FileNotFoundError:
        sys.exit('invalid filename %s' % filename)
    except error.MyPLError as e:
        file_stream.close()
        sys.exit(e)
コード例 #4
0
ファイル: hw7.py プロジェクト: k-davis/MyPL-CPP-Translator
def hw7(file_stream, print_stream, temp_stream):
    the_lexer = lexer.Lexer(file_stream)

    the_parser = parser.Parser(the_lexer)
    stmt_list = the_parser.parse()

    the_type_checker = type_checker.TypeChecker()
    stmt_list.accept(the_type_checker)

    the_name_mangler = name_mangler.NameMangler()
    stmt_list.accept(the_name_mangler)

    the_translator = translator.TranslationVisitor(print_stream, temp_stream)
    stmt_list.accept(the_translator)

    finish_file(print_stream)
コード例 #5
0
def load(filename):
    try:
        f = open(filename, "r")
    except FileNotFoundError as e:
        print('Unable to find file "%s"' % filename)
        return

    print('loading "%s" into REPL' % filename)
    global type_table
    global value_table
    global repl_heap
    global total_stmt

    try:
        current_total_stmt = copy.deepcopy(total_stmt)

        file_lexer = lexer.Lexer(f)
        file_parser = parser.Parser(file_lexer)
        file_stmt_list = file_parser.parse()

        current_total_stmt.stmts.append(file_stmt_list)

        # copy storage so as to not modify REPL if error
        file_type_table = type_table
        file_value_table = value_table
        file_repl_heap = repl_heap

        file_type_checker = type_checker.TypeChecker(file_type_table)
        file_stmt_list.accept(file_type_checker)
        file_interpreter = interpreter.Interpreter(file_value_table,
                                                   file_repl_heap)
        file_interpreter.run(current_total_stmt)

        # set actual storage to the new ones if no error has been caught
        type_table = file_type_table
        value_table = file_value_table
        repl_heap = file_repl_heap
        total_stmt = copy.deepcopy(current_total_stmt)

    except error.MyPLError as e:
        print('Error: %s' % e.message)

    f.close()
コード例 #6
0
def run_stmt(cur_stmt):
    try:
        the_lexer = lexer.Lexer(StringIO(cur_stmt))
        the_parser = parser.Parser(the_lexer)
        stmt_list = the_parser.parse()
        the_type_checker = type_checker.TypeChecker(type_table)
        stmt_list.accept(the_type_checker)
        the_interpreter = interpreter.Interpreter(value_table, repl_heap)
        the_interpreter.run(stmt_list)
        total_stmt.stmts.append(stmt_list)
        # Check to see if someone is trying to call a function w/o parameters
        # or a struct by name instead of a declared object
        struct_or_func_decl = the_interpreter.current_value
        if (isinstance(struct_or_func_decl, list)):
            if (isinstance(struct_or_func_decl[1], ast.FunDeclStmt)):
                print('Missing parentheses on function call "%s"' %
                      cur_stmt[:-1])
            elif (isinstance(struct_or_func_decl[1], ast.StructDeclStmt)):
                print('Attempting to call un-instantiated struct "%s"' %
                      cur_stmt[:-1])
            else:
                print('No one should be here')
                print(struct_or_func_decl)
        else:
            if ('(' not in cur_stmt and 'struct' not in cur_stmt
                    and 'new' not in cur_stmt):
                print(the_interpreter.current_value)
    except error.MyPLError as e:
        print('Error: %s' % e.message)
        if 'fun' in cur_stmt:
            funName = cur_stmt.split()[2]
            funName = funName.split('(')[0]
            if type_table.id_exists(funName):
                type_table.remove_id(funName)

    except TypeError as e:
        if ('unhashable type' in str(e)):
            print('Error: Cannot access elements in undeclared struct')
        else:
            print('Error: %s' % str(e))
コード例 #7
0
ファイル: hw4.py プロジェクト: zmckee2/CPSC326
def hw4(file_stream):
    the_lexer = lexer.Lexer(file_stream)
    the_parser = parser.Parser(the_lexer)
    stmt_list = the_parser.parse()
    print_visitor = ast_printer.PrintVisitor(sys.stdout)
    stmt_list.accept(print_visitor)
コード例 #8
0
def hw5(file_stream):
	the_lexer = lexer.Lexer(file_stream)
	the_parser = parser.Parser(the_lexer)
	stmt_list = the_parser.parse()
	the_type_checker = type_checker.TypeChecker()
	stmt_list.accept(the_type_checker)