def check_returns(line_number, returns, func_def, declared_local_vars, declared_global_vars): for single_return in returns: if line_number is single_return[1]: current_func = find_current_function(line_number, func_def) if current_func is None: continue line_of_code = single_return[0] identifiers = find_expressions(line_of_code) const_ints = find_const_ints(line_of_code) const_floats = find_const_floats(line_of_code) var_ints, var_floats = find_vars(identifiers, declared_local_vars, declared_global_vars, current_func) type_needed = find_type(func_def, current_func) if type_needed == 'int' and (const_floats or var_floats): error = 'Type error on line ' + str( line_number) + ': trying to return "' + line_of_code.split( )[1] + '" which contains floats' error = add_to_error(error, const_floats, var_floats) error = error + 'but should be of type int' print error elif type_needed == 'float' and (const_ints or var_ints): error = 'Type error on line ' + str( line_number) + ': trying to return "' + line_of_code.split( )[1] + '" which contains ints' error = add_to_error(error, const_ints, var_ints) error = error + ' but should be of type float' print error
def check_assingments(line_number, assignments, func_def, declared_local_vars, declared_global_vars): for assignment in assignments: if line_number is assignment[1]: assignment = assignment[0].split('=') lhs = assignment[0] rhs = find_expressions(assignment[1]) current_func = find_current_function(line_number, func_def) lhs_type = find_local(declared_local_vars, current_func, lhs) lhs_line_dec = get_local_line(declared_local_vars, current_func, lhs) for var in rhs: rhs_type = find_local(declared_local_vars, current_func, var) rhs_type = find_global(rhs_type, declared_global_vars, var) rhs_line_dec = get_local_line(declared_local_vars, current_func, var) rhs_line_dec = get_global_line(rhs_line_dec, declared_global_vars, var) if lhs_type != rhs_type: print 'Type error on line ' + str(line_number) + ': trying to assign "' + lhs + '" of type ' + lhs_type + ' (declared on line ' + str(lhs_line_dec) + ' ) with "' + var + '" of type ' + rhs_type + ' (declared on line ' + str(rhs_line_dec) + ')' break
def check_assingments(line_number, assignments, func_def, declared_local_vars, declared_global_vars): for assignment in assignments: if line_number is assignment[1]: assignment = assignment[0].split('=') lhs = assignment[0] lhs = lhs.replace(' ', '') rhs = find_expressions(assignment[1]) current_func = find_current_function(line_number, func_def) lhs_type = find_local(declared_local_vars, current_func, lhs) lhs_line_dec = get_local_line(declared_local_vars, current_func, lhs) try: for var in rhs: rhs_type = find_local(declared_local_vars, current_func, var) rhs_type = find_global(rhs_type, declared_global_vars, var) rhs_line_dec = get_local_line(declared_local_vars, current_func, var) rhs_line_dec = get_global_line(rhs_line_dec, declared_global_vars, var) if lhs_type != rhs_type: if lhs_type is None: print 'Trying to assign to variable ' + lhs + ' on line ' + str( line_number) + ' but was declared incorrectly' elif rhs_type is None: print 'Type error on line ' + str( line_number ) + ': trying to assign "' + lhs + '" of type ' + lhs_type + ' (declared on line ' + str( lhs_line_dec ) + ' ) with "' + var + '" which was declared incorrectly.' else: print 'Type error on line ' + str( line_number ) + ': trying to assign "' + lhs + '" of type ' + lhs_type + ' (declared on line ' + str( lhs_line_dec ) + ' ) with "' + var + '" of type ' + rhs_type + ' (declared on line ' + str( rhs_line_dec) + ')' break except TypeError as err: continue
def check_math(line_number, math_operations, func_def, declared_local_vars, declared_global_vars): for operation in math_operations: if line_number is operation[1]: current_func = find_current_function(line_number, func_def) line_of_code = operation[0] identifiers = find_expressions(line_of_code) const_ints = find_const_ints(line_of_code) const_floats = find_const_floats(line_of_code) var_ints, var_floats = find_vars(identifiers, declared_local_vars, declared_global_vars, current_func) # If there are some combo of ints and floats if not ((const_floats is None and var_floats is None) or (const_ints is None and var_ints is None)): error = 'Type error on line ' + str( line_number ) + ': trying to do math operation on two different types: ' + line_of_code + ' where the ints are:' print report_problem(error, const_ints, const_floats, var_ints, var_floats)