public static Assignment Assignment( this Random random, IReadOnlyCollection <Variable> declaredVariables, IReadOnlyCollection <FunctionDeclaration> declaredFunctions) { var to = random.From(declaredVariables); // prevent assigning to function parameter while (to.IsFunctionParameter) { to = random.From(declaredVariables); } var expr = random.Expression(declaredVariables, declaredFunctions); // prevent variable self-assignments e.g. 'x0 = x0' while (expr is Variable v && v.VariableName == to.VariableName) { expr = random.Expression(declaredVariables, declaredFunctions); } // ensure all initializers are used; e.g. no 'int a = 10; a = 20;' expr = new BinaryOperation(to, random.Operator(), expr); return(new Assignment(to, expr)); }
public static Return Return( this Random random, IReadOnlyCollection <Variable> declaredVariables, IReadOnlyCollection <FunctionDeclaration> declaredFunctions) { IExpr expr = random.Expression(declaredVariables, declaredFunctions); // function return prevents unused variable warnings foreach (var v in declaredVariables) { expr = new BinaryOperation(expr, random.Operator(), v); } return(new Return(expr)); }