def random(scope: Scope): s = Scope(scope) pre = AssignmentStatement.random(s) check = BinaryStatement( pre.variable, BinaryOperators.get_random_comparison(), s.get_random_value(pre.variable.datatype, blacklist=[pre.variable])) post = None post_type = random_with_prob('unary', 50, 'binary', 50) if post_type == 'unary': post = UnaryStatement(UnaryOperator.random(exp=pre.variable), pre.variable) elif post_type == 'binary': op = random_with_prob('+', 10, '-', 10, '*', 5, '', 2) post = AssignmentStatement(pre.variable, s.get_random_value( pre.variable.datatype, blacklist=[pre.variable]), op=op) return ForStatement(s, pre, check, post)
def get_random_comparison(): t = random_with_prob('lt', 50, 'eq', 30, 'gt', 20) if t == 'lt': return BinaryOperators.LESS_THAN elif t == 'eq': return BinaryOperators.GREATER_THAN elif t == 'gt': return BinaryOperators.GREATER_THAN
def get_random(exp1 = None, exp2 = None, return_type = None): probs = None if exp1 == None and exp2 == None: probs = ['bit_and', 10, 'bit_or', 10, 'bit_xor', 10, 'left_shift', 10, 'right_shift', 10, 'add', 10, 'subtract', 10, 'multiply', 10, 'divide', 10, 'modulo', 10, 'lt', 10, 'eq', 10, 'gt', 10, 'and', 10, 'or', 10] elif exp1.get_return_type().is_int() and exp2.get_return_type().is_int(): probs = ['bit_and', 10, 'bit_or', 10, 'bit_xor', 10, 'left_shift', 10, 'right_shift', 10, 'add', 10, 'subtract', 10, 'multiply', 10, 'divide', 10, 'modulo', 10, 'lt', 10, 'eq', 10, 'gt', 10, 'and', 10, 'or', 10] else: probs = ['add', 10, 'subtract', 10, 'multiply', 10, 'divide', 10, 'lt', 10, 'eq', 10, 'gt', 10, 'and', 10, 'or', 10] d = random_with_prob(probs) if d == 'bit_and': return BinaryOperators.BITWISE_AND if d == 'bit_or': return BinaryOperators.BITWISE_OR if d == 'bit_xor': return BinaryOperators.BITWISE_XOR if d == 'left_shift': return BinaryOperators.LEFT_SHIFT if d == 'right_shift': return BinaryOperators.RIGHT_SHIFT if d == 'add': return BinaryOperators.ADD if d == 'subtract': return BinaryOperators.SUBTRACT if d == 'multiply': return BinaryOperators.MULTIPLY if d == 'divide': return BinaryOperators.DIVIDE if d == 'modulo': return BinaryOperators.MODULO if d == 'lt': return BinaryOperators.LESS_THAN if d == 'eq': return BinaryOperators.EQUAL if d == 'gt': return BinaryOperators.GREATER_THAN if d == 'and': return BinaryOperators.LOGICAL_AND if d == 'or': return BinaryOperators.LOGICAL_OR return BinaryOperators.ADD
def random(type = None, exp = None): from variable import Variable probs = None if exp == None: probs = ['minus', 10, 'prefix_decrement', 10, 'postfix_decrement', 10, 'prefix_increment', 10, 'postfix_increment', 10, 'not', 10, 'bit_not', 10, 'sizeof', 10] elif isinstance(exp, Variable): if exp.get_return_type().is_float(): probs = ['minus', 10, 'prefix_decrement', 10, 'postfix_decrement', 10, 'prefix_increment', 10, 'postfix_increment', 10, 'not', 10, 'sizeof', 10] else: probs = ['minus', 10, 'prefix_decrement', 10, 'postfix_decrement', 10, 'prefix_increment', 10, 'postfix_increment', 10, 'not', 10, 'bit_not', 10, 'sizeof', 10] elif exp.get_return_type().is_float(): probs = ['minus', 10, 'not', 10] else: probs = ['minus', 10, 'not', 10, 'bit_not', 10] p = random_with_prob(probs) if p == 'minus': return UnaryOperator.MINUS if p == 'prefix_decrement': return UnaryOperator.PREFIX_DECREMENT if p == 'postfix_decrement': return UnaryOperator.POSTFIX_DECREMENT if p == 'prefix_increment': return UnaryOperator.PREFIX_INCREMENT if p == 'postfix_increment': return UnaryOperator.POSTFIX_INCREMENT if p == 'not': return UnaryOperator.NOT if p == 'address_of': return UnaryOperator.ADDRESS_OF if p == 'bit_not': return UnaryOperator.BITWISE_NOT if p == 'sizeof': return UnaryOperator.SIZE_OF return UnaryOperator.MINUS
def random(scope: Scope): s = Scope(scope) var = s.get_random_value() cond = None d = random_with_prob('var', 10, 'unary', 2, 'binary', 20) if d == 'var': cond = var elif d == 'unary': cond = UnaryStatement(UnaryOperator.random(exp=var), var) elif d == 'binary': cond = BinaryStatement(var, BinaryOperators.get_random_comparison(), s.get_random_value(var.get_return_type())) return IfStatement(s, cond)
def random(scope: Scope): v: Variable = None var_type = random_with_prob('new', 60, 'existing', 40) if var_type == 'new': v = Variable("var" + str(scope.get_num_variables()), DataType.get_random_data_type()) scope.add_to_scope(v) else: v = scope.get_random_variable() if v == None: v = Variable("var" + str(scope.get_num_variables()), DataType.get_random_data_type()) scope.add_to_scope(v) right = scope.get_random_value(v.datatype, blacklist=[v]) return AssignmentStatement(v, right)
def get_random_value(self, of_type = None, blacklist = [], max_depth = 5, current_depth = 0, no_consts = False): from statements import Statement if no_consts: value_type = random_with_prob('variable', 35, 'statement', 30, 'function', 40) else: value_type = random_with_prob('constant', 35, 'variable', 35, 'statement', 30, 'function', 40) if value_type == 'constant': return Constant.get_random(of_type) elif value_type == 'variable': val = self.get_random_variable(of_type, blacklist=blacklist) if val == None: p = ['statement', 50] if not no_consts: p.extend(['constant', 50]) value_type = random_with_prob(p) if value_type == 'constant': return Constant.get_random(of_type) elif value_type == 'statement': val = Statement.random(self, of_type, max_depth=max_depth, current_depth = current_depth + 1) if val == None: return Constant.get_random(of_type) return val elif value_type == 'statement': val = Statement.random(self, of_type, max_depth=max_depth, current_depth = current_depth + 1) if val == None: p = ['variable', 50] if not no_consts: p.extend(['constant', 50]) value_type = random_with_prob(p) if value_type == 'constant': return Constant.get_random(of_type) elif value_type == 'variable': val = self.get_random_variable(of_type) if val == None: return Constant.get_random(of_type) return val elif value_type == 'function': val = self.get_random_function(of_type) if val == None: p = ['variable', 50] if not no_consts: p.extend(['constant', 50]) value_type = random_with_prob(p) if value_type == 'constant': return Constant.get_random(of_type) elif value_type == 'variable': val = self.get_random_variable(of_type) if val == None: return Constant.get_random(of_type) else: for parameter in val.parameters: parameter.value = self.get_random_value(parameter.datatype, max_depth=max_depth, current_depth=current_depth+1, blacklist=[val]) return val
def random(scope, return_type: DataType = None, max_depth=5, current_depth=0, whitelist=None): if whitelist == None: statement_type = random_with_prob('binary', 60, 'unary', 40) if statement_type == 'binary': return BinaryStatement.random(scope, return_type, max_depth=max_depth, current_depth=current_depth) elif statement_type == 'unary': return UnaryStatement.random(scope, return_type, max_depth=max_depth, current_depth=current_depth) else: probs = [] if BinaryStatement in whitelist: probs.extend(['binary', 10]) if UnaryStatement in whitelist: probs.extend(['unary', 10]) if AssignmentStatement in whitelist: probs.extend(['assignment', 40]) if ForStatement in whitelist: probs.extend(['for', 10]) if WhileStatement in whitelist: probs.extend(['while', 10]) if IfStatement in whitelist: probs.extend(['if', 10]) ''' if ReturnStatement in whitelist: probs.extend(['return', 10]) ''' p = random_with_prob(probs) if p == 'binary': return BinaryStatement.random(scope, return_type, max_depth=max_depth, current_depth=current_depth) if p == 'unary': return UnaryStatement.random(scope, return_type, max_depth=max_depth, current_depth=current_depth) if p == 'assignment': return AssignmentStatement.random(scope) if p == 'for': return ForStatement.random(scope) if p == 'while': return WhileStatement.random(scope) if p == 'if': return IfStatement.random(scope) if p == 'return': return ReturnStatement.random(scope, return_type)