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)
Ejemplo n.º 2
0
def main(filename):
    try:
        file_stream = open(filename, 'r')
        the_lexer = lexer.Lexer(file_stream)
        the_parser = parser.Parser(the_lexer)
        stmt_list = the_parser.parse()
        checker = type_checker.TypeChecker()
        stmt_list.accept(checker)
        interpreter = mypl_interpreter.Interpreter()
        stmt_list.accept(interpreter)

    except IOError as e:
        print "error: unable to open file '" + filename + "'"
        sys.exit(1)
    except error.Error as e:
        print e
        sys.exit(1)
Ejemplo n.º 3
0
def main(filename):
    """ Opens a file and begins parsing the file.
    :param filename: The path of a file the user would like to parse
    :return: nothing if file is parsed without errors
    """
    try:
        file_stream = open(filename, 'r')
        the_lexer = lexer.Lexer(file_stream)
        the_parser = parser.Parser(the_lexer)
        stmt_list = the_parser.parse()
        stmt_list.accept(type_checker.TypeChecker())
        stmt_list.accept(interpreter.Interpreter())
    except IOError as e:
        print "error: unable to open file '" + filename + "'"
        sys.exit(1)
    except error.Error as e:
        print e
        sys.exit(1)
Ejemplo n.º 4
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()
Ejemplo n.º 5
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))